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);
    }
有帮助吗?

解决方案

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));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top