Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Problem in code need help

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am new to java and for my first assignment in my java class we were to write a program which generated 4 semi randomly placed rectangles with semi random colors and sizes. Then when two rectangles are clicked on they are to switch places. I am using a constructor to construct all four rectangles and this portion of the code is working just as it is supposed to. However, the portion to switch the rectangles is not working and after looking at it for literally hours I can not figure out why. I am not completely new to programming as I have been in other courses as well but for some reason I am stumped. If you could please at least point me in the direction of my problem that would be great. I don't expect someone to tell me explicitly the problem but any help would be greatly appreciated as my assignment is due tommorrow. Thanks ahead of time.

The following is my code the second section being the constructor and the first being the class to switch the rectangles and such:


[edit]Add newlines to avoid very long lines[/edit]
[ September 15, 2008: Message edited by: Campbell Ritchie ]
Posts: 69790
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Please check the settings on your text editor; have you got tab = 8 spaces? If so change it to "replace tab with spaces" and "tab = 4 spaces." Also make sure to put a space after every comma. Both features which will gain marks. Also don't write lines so long they fall off the edge of the screen. Nesting assignment statements inside constructor calls will lose you marks, I am afraid.

What you need to do when clicking on a Rectangle is to store its fields (x, y, height, width, colour) somewhere, then when you click on the other rectangle, apply its x y height width colour to the first rectangle, then apply the stored variables to the 2nd rectangle. You may need a "mouseClicked" method which means that one of your Components has to implement the MouseListener interface. Remember that interface has 5 methods in, so you might need to implement 4 as "empty", or use the MouseAdapter. You can use an if-else on the firstClick field to choose which rectangle is frstToSwap and which is secondToSwap.

[edit]Minor spelling corrections.[/edit]
[ September 15, 2008: Message edited by: Campbell Ritchie ]
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic