File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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(){

protected void paint(Graphics g){
protected void keyPressed(int key){

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

Joined: May 05, 2005
Posts: 5
Check the image size and its dimesions also.
I agree. Here's the link:
subject: problem with drawImage()
It's not a secret anymore!