Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to bring a shape to front/back?  RSS feed

 
Veronique Kus
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Useful tip, thank you!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!