tags:

views:

373

answers:

3

My Project is in Java Swing.

I have a JPanel on which I am adding some images with .png extension (which are on JLabels) at center.

Now I want to add a line which will be partially on the JPanel & partially on that image. Currently when I am adding a line, JPanel shows the line but when I resize the image & drag it to the image, the image hides the line.

What can be done so that the image doesn't hide my line & shows it on image?

+2  A: 

You're probably better off drawing the image yourself and drawing the line over the top in the same control. Create a class that extends Canvas and in the paint method write your own code to paint the image and then draw the line.

Tom
A: 

You could try using JXLayer and defining a custom LayerUI for it that would draw the lines. These would then appear above the components you need to draw over. This is a little more advanced and involves using a 3rd party (open source) custom component but will allow you to change you mind about what Swing component you use to render your images later. I think this article best describes how to achieve what you want.

I've solved many similar issues to this in the past in a variety of ways and none have had the flexibility and maintainability of JXLayer.

Tom Martin
+1  A: 

Another option is to use JLayeredPane instead of JPanel as your main container and place a non-opaque (setOpaque(false)) JPanel on a higher layer Use JLayeredPane.setLayer(yourPanel, highNumber) and fill your JLayeredPane using something like GridBagLayout or a simple custom LayoutManager.

You can then implement the custom painting on that panel.

Tom Martin