Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

ImageObserver interface

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic