Rotating a picture causes rotation of other pictures in a loop g2d.rotate.

huangapple go评论79阅读模式
英文:

Rotating a picture causes rotation of other pictures in a loop g2d.rotate

问题

这是您的代码部分的翻译:

// 绘制坦克
ArrayList<Tank> tanks = new ArrayList<>();
tanks.addAll(TankTroubleMap.getAITanks());
tanks.addAll(TankTroubleMap.getUserTanks());
for (Tank tankToDraw : tanks) {
    g2d.rotate(-Math.toRadians(tankToDraw.getAngle())
            , tankToDraw.getCenterPointOfTank().getXCoordinate()
            , tankToDraw.getCenterPointOfTank().getYCoordinate());
    g2d.drawImage(tankToDraw.getTankImage()
            , (int) tankToDraw.getCenterPointOfTank().getXCoordinate() - Constants.TANK_SIZE / 2
            , (int) tankToDraw.getCenterPointOfTank().getYCoordinate() - Constants.TANK_SIZE / 2
            , Constants.TANK_SIZE, Constants.TANK_SIZE, null);
}

希望这对您有所帮助。如果您有任何其他问题,请随时提出。

英文:

I'm trying to make a game like Tank Trouble.

The problem is that when I'm trying to rotate and draw a tank picture, all other tanks pictures will be rotated.

Here is my code:

// draw tanks
ArrayList&lt;Tank&gt; tanks = new ArrayList&lt;&gt;();
tanks.addAll(TankTroubleMap.getAITanks());
tanks.addAll(TankTroubleMap.getUserTanks());
for (Tank tankToDraw : tanks) {
    g2d.rotate(-Math.toRadians(tankToDraw.getAngle())
            , tankToDraw.getCenterPointOfTank().getXCoordinate()
            , tankToDraw.getCenterPointOfTank().getYCoordinate());
    g2d.drawImage(tankToDraw.getTankImage()
            , (int) tankToDraw.getCenterPointOfTank().getXCoordinate() - Constants.TANK_SIZE / 2
            , (int) tankToDraw.getCenterPointOfTank().getYCoordinate() - Constants.TANK_SIZE / 2
            , Constants.TANK_SIZE, Constants.TANK_SIZE, null);
}

答案1

得分: 0

只需在绘制所需旋转的图像后,将图形进行逆旋转,否则同一旋转将应用于应用旋转的点之后绘制的所有内容。

所以在你的情况下:

    for (Tank tankToDraw : tanks) {
        g2d.rotate(-Math.toRadians(tankToDraw.getAngle())
                , tankToDraw.getCenterPointOfTank().getXCoordinate()
                , tankToDraw.getCenterPointOfTank().getYCoordinate());
        g2d.drawImage(tankToDraw.getTankImage()
                , (int) tankToDraw.getCenterPointOfTank().getXCoordinate() - Constants.TANK_SIZE / 2
                , (int) tankToDraw.getCenterPointOfTank().getYCoordinate() - Constants.TANK_SIZE / 2
                , Constants.TANK_SIZE, Constants.TANK_SIZE, null);
        g2d.rotate(Math.toRadians(tankToDraw.getAngle())
                , tankToDraw.getCenterPointOfTank().getXCoordinate()
                , tankToDraw.getCenterPointOfTank().getYCoordinate());
    }

你应用于图形的任何变换(在这种情况下是旋转)都会保留给该图形实例上执行的所有未来操作,包括进一步的变换。在处理图形时,你必须记住这一点。

英文:

You simply need to counter-rotate graphics back once you've drawn the image you wanted to rotate. Otherwise the same rotation will be applied to everything painted past the point you applied the rotation at.

So in your case:

    for (Tank tankToDraw : tanks) {
        g2d.rotate(-Math.toRadians(tankToDraw.getAngle())
                , tankToDraw.getCenterPointOfTank().getXCoordinate()
                , tankToDraw.getCenterPointOfTank().getYCoordinate());
        g2d.drawImage(tankToDraw.getTankImage()
                , (int) tankToDraw.getCenterPointOfTank().getXCoordinate() - Constants.TANK_SIZE / 2
                , (int) tankToDraw.getCenterPointOfTank().getYCoordinate() - Constants.TANK_SIZE / 2
                , Constants.TANK_SIZE, Constants.TANK_SIZE, null);
        g2d.rotate(Math.toRadians(tankToDraw.getAngle())
                , tankToDraw.getCenterPointOfTank().getXCoordinate()
                , tankToDraw.getCenterPointOfTank().getYCoordinate());
    }

Any transformations (in this case - rotation) you apply to graphics are preserved for all future operations you perform on that graphics instance, including further transformations. You have to remember that when working with graphics.

huangapple
  • 本文由 发表于 2020年7月28日 15:07:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63128745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定