I'm new to Java programming (and GUIs) and I can't figure out why my program is doing this. This is a very simple program that Starts out with a square rectangle at the top left of the screen, and when the user clicks on the screen it should redraw a new rectangle at the point where the screen is clicked. My program doesn't redraw, though. I inserted "System.out.println(x)" in the mouseClicked interface, which works correctly, so I know that the program is getting my mouseclicks, but it is not repainting correctly? Help please? Here's the code:
basically, your NewBoxDrawer class never changes the x,y positions so it simply repaints the same thing over and over.
your code modified - few comments added
Joined: Nov 15, 2008
Thanks Michael. So, if I'm understanding correctly, when the mouseclicker gets the new x and y coordinates, they belong to the newbox class and not the newboxdrawer (source). So I need to explicitly pass them to the source class by calling setX and setY. Why wouldn't it be sufficient to write:
rather than creating a function to pass the variables and then setting them in the other class? If the variables are stored in the "source" class, can they only be modified within that class? It seems like writing new methods for each variable to be changed might get tedious in a program more complex than this simple example.
Thanks again, Luke
Joined: Jun 09, 2003
> Why wouldn't it be sufficient to write: > source.x = evt.getX(); > source.y = evt.getY();
the very best answer I can give you now is to try it, and see what happens. you'll get more out of seeing the message generated (based on your original code).