• Post Reply Bookmark Topic Watch Topic
  • New Topic

ImageObserver interface  RSS feed

 
Jonathan Sternberg
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Indeed, it's an ImageObserver, but an ImageIcon is not an Image. You can call getImage() on the ImageIcon to get one the Image object.

Note, though, that this "call drawImage() on a static Graphics object" idea isn't going to work. "g" is likely to be null anyway since getGraphics() can return null before the component appears on the screen.

The right way to paint is to override the paintComponent(Graphics) method, call super.paintComponent(), then do your drawImage (or whatever) in there. Never try to paint outside of your paintComponent() method (well, not absolutely never, but you need to know what you're doing.)
 
Jonathan Sternberg
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ImageIcon was the problem??? 0_o

Well the g = getGraphics() works after you call setVisible(true) (I tested this, it doesn't work otherwise) but I'm just beginning graphics. I'll check out what this paintComponent thing is on java sun.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!