aspose file tools
The moose likes Java in General and the fly likes ImageObserver interface Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "ImageObserver interface" Watch "ImageObserver interface" New topic
Author

ImageObserver interface

Jonathan Sternberg
Greenhorn

Joined: Jun 03, 2005
Posts: 5
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
Marshal

Joined: Jul 08, 2003
Posts: 24054
    
  13

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.)


[Jess in Action][AskingGoodQuestions]
Jonathan Sternberg
Greenhorn

Joined: Jun 03, 2005
Posts: 5
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: ImageObserver interface
 
Similar Threads
Graphics
Not getting key pressed event.
Moving object on keyEvents FAILING !
best way to update a JList
Beginning Java Games