my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes placing an image into a panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "placing an image into a panel" Watch "placing an image into a panel" New topic
Author

placing an image into a panel

Bob Chandler
Ranch Hand

Joined: Jan 13, 2004
Posts: 33
perhaps someone can help me out here...
I made an applet that displays a jpeg image as a
background. I placed another image in a specified
position using the following code:
private Image buffer;
private Graphics gBuffer;
Image background, obj;
public void init()
{
//create graphics buffer the size of the applet
buffer= createImage(size().width, size().height);
gBuffer= buffer.getGraphics();
background= getImage(getCodeBase(), "bubble.jpg");
obj= getImage(getCodeBase(), "ball.gif");
}
........//from run method
gBuffer.drawImage(background,0,0,this);
gBuffer.drawImage(obj,x,150,this);
Problem is, for my new applet, I want to place an image
into a panel....I can't use the add method....how do
you mix components with Graphics class?
any ideas? confused Bob
[ February 12, 2004: Message edited by: Bob Chandler ]
[ February 12, 2004: Message edited by: Bob Chandler ]
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

An image isn't a component, so you can't use add() to put an image on a panel. You have to override paint() and paint the image like you have above. Of course, you can make methods that will take an image and draw them on a Panel... something like this-


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Bob Chandler
Ranch Hand

Joined: Jan 13, 2004
Posts: 33
Hey Nate,
didn't get what you meant at first....
was washing the dishes and mulling it all over when
light dawned...
...create an instance of ImagePanel class in place
of the plain old Panel instance...
...when it's overridden version of paint is called,
the image is drawn in the ImagePanel!
I tried it,it worked and I thank you
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: placing an image into a panel
 
Similar Threads
Image+Button+applet
how to use drawImage in an object
Paint
Background image and scroll text???
Phasor applet-some prbs already compiled code written here