Guys why is my image not loading on my JFrame??
BTW I am using eclipse on my mac.
Code for Board :
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 :
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:
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!