Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Select java graphic and drag them  RSS feed

 
Ariana Hobson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a House class where I constructed a house by drawing java graphics (rectangle and lines). I have a SelectAndDragTest class which is the main class. When I run this class, it will generate a house on the JPanel. It is draggable. But when I click the button to add another house, I am able to drag the newly added house but not the previous houses. How do I make that other house is selectable upon mousepressed and draggable? I'd appreciate if anyone could give any suggestion. Below are my codes:

SelectAndDragTest.java


House.java
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A painting method is for painting only. It should NOT create new objects.

Instead you need to modify your House class to have a "contains(Point mousePoint)" method.

The code might be something like:



In your mouseDragged() code you now need to iterate through all the Houses in the ArrayList and invoke the contains(...) method on each house. When you get a value of true you then need to adjust the x/y value of the house so it can be painted in its new location.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!