File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes repainting JLabels on a JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "repainting JLabels on a JPanel" Watch "repainting JLabels on a JPanel" New topic
Author

repainting JLabels on a JPanel

D R Wilkinson
Ranch Hand

Joined: Oct 29, 2004
Posts: 51
Hello :-)



I have a map of JLabels displaying in a JPanel, within a JScrollPane.

When the cursor enters a hex, the corresponding JLabel icon repaints itself as a red hexagon. When the cursor exits, it repaints itself as a black hexagon.

I used absolute positioning on the JPanel and added the JLabels using add()

Each JLabel is 21x21 and the hexagon is 20x20. This allows the hexes to overlap by a pixel to the right and below/right. Without this, the JLabels didnt display the hexagons properly - each JLabel set to 20x20 resulted in the row end hexes losing the right and below/right edges of the hexagon.

The first hex displays correctly but the rest do not. I think this is because of the order the JLabels are added to the JPanel combined with the overlapping (by 1 pixel) of the JLabels.

Somehow, I need to change the display order of the JLabels so that the one that has detected the cursor entering it gets re-ordered to the top of the list of JLabels displayed on the JPanel. Then it can repaint itself and should show up correctly.

I experimented with JLayeredPane but the map load time was too long (there are 10,000 map hexes) whereas using JPanel, the load time is acceptable.

Any helpful ideas?

Thanks

Darren

[ November 30, 2004: Message edited by: D R Wilkinson ]

[ November 30, 2004: Message edited by: D R Wilkinson ]

[ November 30, 2004: Message edited by: D R Wilkinson ]
[ November 30, 2004: Message edited by: D R Wilkinson ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
I would try to adjust the layout so that all of each Polygon is visible. This will save a lot of event code involving z–order and label identification. Let's see how this does —
D R Wilkinson
Ranch Hand

Joined: Oct 29, 2004
Posts: 51
Thank you very much for the reply Craig.

I tried the approach you have suggested and rejected it due to the fat/thin appearance of the hex map.

Re-ordering the z-order of the map hex that needs to be brought to the top of the JPanel is the route I want to take. What I am struggling with is trying to understand what is involved in using this re-ordering approach. I appreciate your method avoids possible complexities involved in re-ordering, but I feel these complexities are:

a) going to stretch my programming ability
b) worth the development pain for the neater graphic effect

I don't expect anyone else to code it for me, I just need a little guidance as to how to approach it. Your comment regarding the complexities has hinted of the requirements of the approach. Could you (or anyone else) expand on this please?

Again, thank you for your reply and help Craig.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
I did this with a JLayeredPane and it turned out to be easier than I first thought. I understand about your not wanting to see code so I'll try to not spoil your fun. I added the JLabel components to the JLayeredPane DEFAULT_LAYER. In the mouse code I changed the component layer to a higher layer in mouseEntered and back again to the default layer at exit. The top part of the JLayeredPane api has a discussion about the layers, adding components to them and changing layers for a component.
D R Wilkinson
Ranch Hand

Joined: Oct 29, 2004
Posts: 51
Hello :-)

I tried using a JlayeredPane but the performance suffered dramatically - there are 10,000 map hexes in a map - no problem on a JPanel but something a JLayeredPane doesn't seem to like... unless I am doing something wrong.

I add the MapHex objects with this line...


and the layer switching is done with...


Looking at it now, the problem may be that all, or all but one MapHex's always have isMouseInside == false, so maybe there are 9,999 MapHex's permenantly trying to move to the back of the JLayeredPane.

I'll see if this is the problem.
D R Wilkinson
Ranch Hand

Joined: Oct 29, 2004
Posts: 51
I commented out the line...


and the performance of the red/black highlight effect was fine, however the rest of the map displays incorrectly (see below)...



There is no discernable pattern to the errors, although they centre around the hex the cursor has entered/exited.

Also the initial load time of the map itself is around 50 seconds. Using a JPanel, the load time is maybe 2 seconds.
[ December 03, 2004: Message edited by: D R Wilkinson ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Is there any reason why you don't just paint the whole panel, rather then using 10,000 JLabels and layout? The performance would be much better all around, and the code would be simpler, too.

Note that (for example) JTable is so fast because, although it appears to be using a separate JLabel for each cell, it actually uses just one (by default, anyway) and repositions and "stamps" it as necessary.


[Jess in Action][AskingGoodQuestions]
D R Wilkinson
Ranch Hand

Joined: Oct 29, 2004
Posts: 51
Thanks for that.

I was just starting to think along the same lines re: painting the map.

The only reason I wanted to use JLabels was that I wanted to confine the drawing of each map hex to a map hex object that then 'stamped' its image onto the JPanel when it was added - the JLabel seems to do this automatically.

rethinking the map hex object, i need an object that knows its xy position on the map, contains a drawing area that is 21x21 and has mouse event listeners for the mouse enter, exit, click, etc. If this object has a reference to the map object, i could use the size of the map hex (offset by its xy position on the map) to copy the region of the existing map image below it (a sort of clipping region) then it could use this as a background image (capturing the parts of the neighbouring map hex's images that need to stay unaltered), then the map hex can paint itself onto this background image (so graphically representing its new state) and this image could be painted onto the map itself.

OK... not entirely sure how to make that happen but... will give it a go.

Thanks for the help everyone :-)
[ December 03, 2004: Message edited by: D R Wilkinson ]
D R Wilkinson
Ranch Hand

Joined: Oct 29, 2004
Posts: 51
OK - I am getting close but I just can't seem to get it right.

Here's the code - it sort of does what I need but in a very wrong way.

Any help ???

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: repainting JLabels on a JPanel