Pergunta

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);
    }
Foi útil?

Solução

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));
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top