• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

repainting JLabels on a JPanel

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
D R Wilkinson
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ???

 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic