java.awt形状不能正常旋转

时间:2017-10-10 20:27:45

标签: java

我在Java中创建一个Pool游戏,根据鼠标在屏幕上的位置旋转提示,但旋转总是+ - 20度关闭

由于提示只是视觉形式,我提供渲染代码:

@Override
protected void paintComponent(Graphics graphics) {
    Graphics2D g = (Graphics2D) graphics;

    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    super.paintComponent(g);
    //draw here
    g.drawImage(Resources.getPoolTableImage(), 0, 0, null);

    double dx = EventHandler.getMouseX() - table.getWhiteBall().getPosition().x;
    double dy = EventHandler.getMouseY() - table.getWhiteBall().getPosition().y;
    double dir = Math.atan2(dy, dx);

    System.out.println(Math.toDegrees(dir));

    g.fill(Resources.rotate(new Rectangle((int) table.getWhiteBall().getPosition().x + 3,
            (int) table.getWhiteBall().getPosition().y + 10, 20, 100), dir));
    for (Ball ball : table.getBalls()) {
        ball.render(g);
    }

    this.repaint();
}

这是负责旋转形状的方法

public static Shape rotate(Rectangle rect, double angle) {
    Rectangle2D myRect = new Rectangle2D.Double(rect.x, rect.y, rect.width, rect.height);
    AffineTransform at = AffineTransform.getRotateInstance(angle - 90, rect.x + rect.width / 2, rect.y);
    Shape rotatedShape = at.createTransformedShape(myRect);
    return rotatedShape;
}

正在使用-90度创建旋转实例以尝试修复形状的旋转,但它仍处于关闭状态。

0 个答案:

没有答案