Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java applet, draw shapes at mouse click centre

 
Hayles Berry
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2993
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. :)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic