I've been trying to use graphics for this game I'm making with some friends in
Java. My friend had already used graphics for a smaller game he made for a project in our computer programming class. So I asked him to send me the thing he made that worked. This is trying to draw ImageIcons on a JLabel. But I keep getting this error that my friend didn't. Note that I'm using the newest version of Eclipse to write this and he used Codewarrior JDK 1.4.
public class KeyImage extends JFrame implements ImageObserver, KeyListener {
private static Container contentPane;
private static Graphics g;
static int xLoc;
static int yLoc;
static JLabel label;
public KeyImage() {
super();
setSize(400,400);
contentPane=getContentPane();
this.setVisible(true);
xLoc=200;
yLoc=200;
g=contentPane.getGraphics();
label = new JLabel();
addKeyListener(this);
contentPane.add(label);
}
/* (non-Javadoc)
* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
*/
public boolean imageUpdate(Image arg0, int arg1, int arg2, int arg3,
int arg4, int arg5) {
// TODO Auto-generated method stub
return false;
}
/* (non-Javadoc)
* @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
*/
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
*/
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
*/
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public static void main(
String[] args) {
KeyImage c = new KeyImage();
c.setVisible(true);
String filepath = new String("~/Documents/eclipse/");
ImageIcon icon = new ImageIcon(filepath+"eirika.gif");
g.drawImage(icon,xLoc,yLoc,c);
}
}
eirika.gif is just the image I'm using (I took one from one of the games I like) and the error always comes from the end and it says that drawImage needs an Image, int, int, and ImageObserver. But c (KeyImage) implements ImageObserver. I'm stuck and have no clue what's going on.