Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Java applet, draw shapes at mouse click centre

 
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.
 
Sheriff
Posts: 3036
12
Mac IntelliJ IDE Python VI Editor Java
  • 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.
 
Marshal
Posts: 25594
69
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. :)
 
I love a good mentalist. And so does this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic