File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

placing an image into a panel

 
Bob Chandler
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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-
 
Bob Chandler
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic