This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Applets and the fly likes Java applet, draw shapes at mouse click centre Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Java applet, draw shapes at mouse click centre" Watch "Java applet, draw shapes at mouse click centre" New topic
Author

Java applet, draw shapes at mouse click centre

Hayles Berry
Ranch Hand

Joined: Dec 16, 2012
Posts: 32
Currently in the below program, the the mouse coordinates represent the top left hand corner of the rectangle
or oval. I need to alter the program, so that oval or rectangle are drawn with the mouse coordinates in the centre of the shape.



Any help/guidance will be much appreciated.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2840
    
  11

Hi Hayles,

Please be careful with your subjects. "Please read", all in caps, and with an exclamation point (though mercifully just one), could easily be misinterpreted as you trying to say your question is more important than other people's. Ironically, that's going to make us less likely to answer you, or even read your question at all.

In any case, the Graphics class doesn't have exactly the method you want, so you have to do the work yourself to translate the mouse click coordinates to the top-left corner of the rectangle you want your shape drawn into. I'm sure you can figure that out if you think about it, plus a little trial and error. Hint: it's going to involve dividing the size by 2.

Hayles Berry
Ranch Hand

Joined: Dec 16, 2012
Posts: 32
Greg Charles wrote:In any case, the Graphics class doesn't have exactly the method you want, so you have to do the work yourself to translate the mouse click coordinates to the top-left corner of the rectangle you want your shape drawn into. I'm sure you can figure that out if you think about it, plus a little trial and error. Hint: it's going to involve dividing the size by 2.


I have the formula for getting the centre coordinates (the ones I want to translate the mouse click coordinates to) already, but when I try using that, it just returns the same coordinates wherever I click.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Then that would mean that your formula isn't doing the right thing, no? So would you like to show it to us, in order that somebody might notice the problem?
Hayles Berry
Ranch Hand

Joined: Dec 16, 2012
Posts: 32
Paul Clapham wrote:Then that would mean that your formula isn't doing the right thing, no? So would you like to show it to us, in order that somebody might notice the problem?


Thanks, but I've figured it out now. :)
 
Don't get me started about those stupid light bulbs.
 
subject: Java applet, draw shapes at mouse click centre
 
Similar Threads
HELP
HELP 5
HELP2
Why is this Image being painted?
Help 3