• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Swing graphics problems

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

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:




NewBoxDrawer class:

 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
luke smith
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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).
 
I child proofed my house but they still get in. Distract them with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic