• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Swing graphics problems

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic