• Post Reply Bookmark Topic Watch Topic
  • New Topic

Painting components over images  RSS feed

 
D N Taylor
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've exteneded a JFrame and overridden it's paint() method because I want to draw an image in the background and then display a component on top of it (label, button, etc). But my component is not displaying in the right place. It only displays in the top left corner, even though I've set the location to something else. I don't think I'm painting it right somehow.

Here's a snippet:


JLabel label1 = new JLabel("hello");
label1.setSize(150, 50);
label1.setLocation(200, 200);
...

public void paint(Graphics g)
{
Graphics offScreenGraphics = offScreenImage.getGraphics();

offScreenGraphics.drawImage(myImage, 0, 0, 800, 600, 0, 0, 800, 600, this);

paint(offScreenGraphics);

label1.paint(offScreenGraphics);

g.drawImage(offScreenImage, 0, 0, this);

offScreenGraphics.dispose();
}


Any help or suggestions much appreciated!!

Thanks,

D Taylor
 
jefff willis
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just a thought, but it may be related to the layout manager that you are using to display these things.

Also, your paint() method is calling two other paint() methods. I'm a little confused there. Make sure that part of you code is correct.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swing will paint all of a containers components for you. All you have to do is render your background image to a component and then add the child components as usual. Here's the recommended way:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!