Search code examples
javascalegraphics2d

Java Graphics2D scale doubling


I'm having trouble scaling objects with Graphics2D in Java. In my paintComponent I create two graphics2D objects for easier management. I then try to scale them. The problem is that the output is scaled x4 (the scale commands affect each other). Otherwise everything renders as it should.

How is this best solved.

The code in question:

@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);  
    }
}

Solution

  • Both the playerGraphics and the backgroundGraphics variables are a reference to the same Graphics instance. Thus, when you call

    playerGraphics.scale(2.0, 2.0);
    backgroundGraphics.scale(2.0, 2.0);
    

    You are actually scaling the same graphics object twice, thus you are scaling it up by four times. Try just calling the scale method on one of them, or better yet, have only one Graphics2D on which you call the methods.