Вопрос

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