• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

How to draw over an image?

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have made a program to display an image.  The image displays fine.  I want to be able to draw over this image with graphics paintComponent.  This is the code I used but it has not worked as I intended:



Can you please advise me what I should do differently?
 
Saloon Keeper
Posts: 5233
211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make the JLabel a field of your class, and add it to your panel in the panels constructor. Then override the panels paint-method, like:
 
Saloon Keeper
Posts: 14674
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Connor wrote:This is the code I used but it has not worked as I intended


The help we can offer is very limited if you don't tell us what the problem is. We don't know what you intended, and how the program output differed from what you intended.

Having said that, I can already identify some issues with your code. The paintComponent() method must only be used for interacting with the Graphics object. Do NOT modify your components properties (such as the background color) in that method, and do NOT add new components in that method.

Please also consider the advice I already gave you in your other topic. Do not make classes public unless you really have to. Make classes final unless you have a really good reason not to.
 
Marshal
Posts: 77157
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens? Where do you have the image? Go through the Java™ Tutorials about layouts; I think but I am not certain that if you add two different Components to the same location, the latter sits in front of the former and hides it. Might be mistaken there. Try setting the transparency of the Component in front and see what happens. Or the α component for every colour (=Color object) on the Component in front.
 
Rancher
Posts: 3288
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I want to be able to draw over this image with graphics paintComponent.



1) Never do I/O in a painting method. A painting method should be efficient and only do painting.

2) Never create a component add attempt to add it to the panel in a painting method. Every time the painting method is called you end up creating a new component and adding it to the panel.

If you want to paint on top of an image, then you:

1. get rid of the JLabel.
2. read the image in the constructor of your class
3. use the drawImage(....) method to paint the image in the paintComponent(...) method
4. then do your drawOval(...). Since the drawOval is done after the drawImage, the oval will appear on top of the image.

You also need to override the getPreferredSize() method of the PaintingCanvas class so layout managers know what size your canvas should be.

 
Saloon Keeper
Posts: 26534
187
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:
1) Never do I/O in a painting method. A painting method should be efficient and only do painting.



Painting is done in the GUI rendering thread. You don't own that thread. Nothing but rendering should be done in that thread. That thread may be invoked at unpredictable times and may be invoked multiple times in short succession.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic