Click here to Skip to main content
15,887,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Guys why is my image not loading on my JFrame??
BTW I am using eclipse on my mac.

Code for Board :
Java
package ourgame;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class Board extends JPanel implements ActionListener {
        Dude p;
        public Image img;
        Timer time;
 
        public Board() {
                p = new Dude();
                addKeyListener(new AL());
                setFocusable(true);
                ImageIcon i = new ImageIcon("C:/character.png");
                img = i.getImage();
                time = new Timer(5, this);
                time.start();
        }
 
        public void actionPerformed(ActionEvent e) {
                p.move();
                repaint();
        }
 
        public void paint(Graphics g) {
                super.paint(g);
                Graphics2D g2d = (Graphics2D) g;
 
                g2d.drawImage(img, 0, 0, null);
                g2d.drawImage(p.getImage(), p.getX(), p.getY(), null);
        }
 
        private class AL extends KeyAdapter {
                public void keyReleased(KeyEvent e) {
                        p.keyReleased(e);
                }
 
                public void keyPressed(KeyEvent e) {
                        p.keyPressed(e);
                }
        }
}


Code for Dude :
Java
package ourgame;
 
import java.awt.*;
import java.awt.event.KeyEvent;
 
import javax.swing.ImageIcon;
 
public class Dude {
        int x, dx, y;
        Image still;
 
        public Dude() {
                ImageIcon i = new ImageIcon("C:/background.jpg");
                still = i.getImage();
                x = 10;
                y = 172;
        }
 
        public void move() {
                x = x + dx;
        }
 
        public int getX() {
                return x;
        }
 
        public int getY() {
                return y;
        }
 
        public Image getImage() {
                return still;
        }
 
        public void keyPressed(KeyEvent e) {
                int key = e.getKeyCode();
                if (key == KeyEvent.VK_LEFT)
                        dx = -1;
 
                if (key == KeyEvent.VK_RIGHT)
                        dx = 1;
        }
 
        public void keyReleased(KeyEvent e) {
                int key = e.getKeyCode();
 
                if (key == KeyEvent.VK_LEFT)
                        dx = 0;
 
                if (key == KeyEvent.VK_RIGHT)
                        dx = 0;
        }
 
}


Code for Frame:
Java
package ourgame;
 
import javax.swing.*;
 
public class Frame {
 
        public Frame(){
                JFrame frame = new JFrame();
                frame.add(new Board());
                frame.setTitle("2-D Test Game");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(700,365);
                frame.setVisible(true);
                frame.setLocationRelativeTo(null);
        }
        public static void main(String[] args){
                new Frame();
        }
}

Thanks in advance!
Posted

1 solution

you are running your app on your mac, but your image path is a windows path.
ImageIcon i = new ImageIcon("C:/character.png");
. The path on Mac should be "/User/xxx/your_projects/background.jpg". This is an absolute path. You can check the image path by keyboard shortcut "command + i". And you can also use relative path to your project if you have the image file in your project.
ImageIcon i = new ImageIcon("background.jpg");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900