• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Select java graphic and drag them

 
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
 
Rancher
Posts: 3052
22
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!