This week's book giveaway is in the Kotlin forum.We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# How to bring a shape to front/back?

Veronique Kus
Ranch Hand
Posts: 41
I am looking for a way of determining the order of shapes to be able to move them to in front of/behind other shapes. I did a search and I couldn't find anything useful. I remember that some time ago I saw something about z-value (?) that related to the layering order...

pete stein
Bartender
Posts: 1561
You might want to provide some context here. Please have a look in the FAQ, in particular, TellTheDetails.

Much luck!

Veronique Kus
Ranch Hand
Posts: 41
I have an application which allows the user to draw shapes on a canvas. Shapes are subclasses of Rectangle2D.Double class. As for now, the first shape you draw will always be at the bottom, so if you draw another shape and you move it over the first shape, the first shape will be covered by the second shape. I want to find a way of changing this order - bringing the first shape to the front so that it is not covered by the second shape. Hope this helps.

pete stein
Bartender
Posts: 1561
How are the Shapes drawn? Are they held in an ArrayList and is there a for loop in your paint/paintComponent override that draws the Shapes? If so, what happens if you change the order of the Shapes in the ArrayList?

Rob Camick
Ranch Hand
Posts: 2800
15
As for now, the first shape you draw will always be at the bottom, so if you draw another shape and you move it over the first shape, the first shape will be covered by the second shape

Then change the order in which you draw the shapes. I gave you an example of drawing shapes from an ArrayList before. So you would need to reorder the items in the ArrayList if you want them to paint in a different order.

I remember that some time ago I saw something about z-value (?) that related to the layering order...

That is used when you add components (like a JLabel) to a JPanel.

Veronique Kus
Ranch Hand
Posts: 41
Yes I am using an ArrayList of shapes. I saw that z-value thing some time ago and I just thought I could use it to save time which will be consumed by reordering the ArrayList. Now that I know that I can't, I can start working on the reordering :-) Thank you to both of you.

pete stein
Bartender
Posts: 1561
One caveat: if you are using a MouseListener to allow the user to drag the Shapes, you'll want to use the listener to iterate through the reverse order that you draw the shapes when deciding if a Shape has been clicked or not. Otherwise if you click on a bunch of Shapes, you may select one that is in under other shapes. Thus the MouseListener should use the same array list as the paintComponent method if possible, but just iterate through in the other direction.

Veronique Kus
Ranch Hand
Posts: 41
Useful tip, thank you!