Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Displaying and Image

807598Aug 13 2006 — edited Aug 14 2006
I'm having trouble displaying an image with this code. What's sad is that I figured it out at 3 am yesterday but then overwrote the file to expand the example. I don't remember what I did to make it workk. I only remember adding a line of code in the constructor over 'this.setVisible (true)"...but I can't remember what I did...argh!

In any case, the .gif is in the same location as the java file...The image just does not display and I think it's something simple that I'm missing.
Any help is GREATLY appreciated.

[code ]class NewFrame extends Frame
{
private Image image;

public NewFrame (int width, int height)
{
this.setTitle ("Display image example");
this.setResizable (true);
this.setBackground (Color.cyan);
this.setSize (width, height);

this.setVisible (true);
}

public void paint (Graphics g)
{
Toolkit tk = Toolkit.getDefaultToolkit ();
image = tk.getImage ("Bike.gif");
g.drawImage (image, 0, 0, this);
}
}
public class Test {

public static void main (String[] args)
{
new NewFrame (300, 200);
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 11 2006
Added on Aug 13 2006
12 comments
171 views