*
The moose likes Java Micro Edition and the fly likes problem with drawImage() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "problem with drawImage()" Watch "problem with drawImage()" New topic
Author

problem with drawImage()

pingkan paula
Ranch Hand

Joined: Sep 03, 2004
Posts: 117
Hi everyone

I want to create an image when a button "1" pressed
The code looks like this

public class Test extends Canvas{
public static Graphics g_fx;
private image img;
public Test(){
try{
img=Image.createImage("face.png");
}
}

protected void paint(Graphics g){
g_fy=g;
}
protected void keyPressed(int key){
if(key==49)
g_fy.drawImage(img,50,50,Graphics.LEFT+Graphics.TOP);
}
}

When i run it, it doesn't display anything, although I have test to print something inside keyPressed() method it did displayed in the console, is method drawImage() can't be applied outside the paint() method?
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
"repaint" ?

/Svend Rost
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
Yes, generally, in Java, the reference to the screen's Graphics object (called "g" in this case) is not reliable outside of the paint() method.

Standard procedure is to do all of your drawing in the paint() method, or in methods that are called only by paint(). That drawing code should look at the state of your application to know what to draw.

For example, you could have your key press set a boolean variable. Then, when paint() did its thing, it would look at the variable to know whether or not to draw the image.

And you don't normally call paint() directly. As Svend said, you would usually call repaint().

- Jeff
Arun Soman
Greenhorn

Joined: May 05, 2005
Posts: 5
Check the image size and its dimesions also.
 
jQuery in Action, 2nd edition
 
subject: problem with drawImage()
 
Similar Threads
Image in a Panel.. need help urgently..!!
Draw bitmap in Canvas
graphics object
Type Mismatch- Cannot convert from DrawImage to Image
Label with Image