Hug*_*nor 1 java scale graphics2d
我在 Java 中使用 Graphics2D 缩放对象时遇到问题。在我的paintComponent 中,我创建了两个graphics2D 对象以便于管理。然后我尝试缩放它们。问题是输出按 x4 缩放(缩放命令相互影响)。否则,一切都会呈现应有的效果。
这个怎么解决最好。
有问题的代码:
@Override
public void paintComponent(Graphics g) {
playerGraphics = (Graphics2D) g;
backgroundGraphics = (Graphics2D) g;
playerGraphics.scale(2.0, 2.0);
backgroundGraphics.scale(2.0, 2.0);
backgroundGraphics.drawImage(background.getBackground(), 0, 0, null);
for(int i = 0; i < noPlayers; i++) {
playerGraphics.drawImage(players.get(i).getCurrentSprite(), players.get(i).getXPos(), players.get(i).getYPos(), null);
}
}
Run Code Online (Sandbox Code Playgroud)
无论是playerGraphics与backgroundGraphics变量相同的参考Graphics实例。因此,当你打电话
playerGraphics.scale(2.0, 2.0);
backgroundGraphics.scale(2.0, 2.0);
Run Code Online (Sandbox Code Playgroud)
您实际上将同一图形对象缩放了两次,因此将其缩放了四倍。尝试只在其中之一上调用 scale 方法,或者更好的是,只Graphics2D在其中调用一个方法。