This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How to move a second object?  RSS feed

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JFrame with a JPanel.

I have a red square which can be moved around. I now want to add a second square in green which can also be moved around. I have gotten the green square to appear in the position I have set x and y on the panel but it will not allow me to move it.

Any ideas where i'm going wrong would be a big help as i want to add other movable squares after this one.

My code is below:

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Steven:

First I would like to note that you have a couple of design problems (too much repeated code, and not following OO principles).

Here is your code with some design corrections (to avoid you MAYOR headaches in the future maintenance), and some notes on what you could do to get the squares moving...




Notes about the Square classes... you had duplicated code for every method of each class (RedSquare and GreenSquare) that is calling for maintenance nightmare in the short run (specially if you'r going to put more squares or other shapes in the app, so I rewrote the code to give you an idea of how you can organize your objects... Notice that i have put every getter/setter (that are common to all Squares) in the Square class, you can improve it further adding a property to the Square class named 'color' and asign it a value in the constructor of the object type you are creating... There are probably hundreds of different ways to do it, so here's mine.

Answering your question, check the comments in the 'mouseEvent' pieces of code.

Hope this helps...
Diego
[ November 11, 2007: Message edited by: Diego Pedrosa ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!