• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Painting components over images

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
reply
    Bookmark Topic Watch Topic
  • New Topic