This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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

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
Problem is, for my new applet, I want to place an image
into a panel....I can't use the add 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

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-

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
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: placing an image into a panel
It's not a secret anymore!