-2

I am making a program that draws ellipses when the user clicks the screen. Currently when the ellipse is drawn the origin is (0,0) so it is being drawn from the top right. I want it to be drawn around the mouse click so then center is exactly where the user clicks but I'm not sure how to do it. If someone could steer me in the right direction that would be great!

    public void DrawSprite( Graphics2D g2 )
    {
        AffineTransform tOldTransform = g2.getTransform();
        g2.setColor(SetSpriteColor());
        g2.translate(mX, mY);
        g2.rotate(mRotation*(Math.PI/180));
        g2.draw(new Ellipse2D.Double(0, 0, mWidth, mHeight));
        g2.setTransform(tOldTransform);
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2150807
  • 411
  • 2
  • 7
  • 14
  • I've tried that which I figured would work but when I click the screen the ellipse is drawn really far away from where I click – user2150807 May 14 '13 at 20:49
  • Please see the Java naming conventions in the [`java` tag wiki](http://stackoverflow.com/tags/java/info). – wchargin May 14 '13 at 20:52
  • I'm betting that your mX and mY are incorrect, that they are either not being set, or they are absolute values when they really should be relative to the pane you are drawing in – greedybuddha May 14 '13 at 20:52
  • I just checked and they are certainly being set and the values are correct (i used system.out.println(mX) within the draw method) – user2150807 May 14 '13 at 21:05

1 Answers1

2

g2.translate(mX, mY);

I'm guessing mX and mY is the Point where the mouse was clicked. So you translation can't be that exact Point. Maybe something like:

g2.translate(mX - (mWidth / 2), mY - (mHeight / 2));
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Hi. Teacher for that assignment here. Maybe you were playing LoL in class while I did it on the board, but in any case you could have just asked me. – CBGraham Jul 09 '14 at 04:09