Jump to content

  • Log In with Google      Sign In   
  • Create Account

#Actualde_mattT

Posted 09 December 2012 - 02:52 PM

Inded the g2d object represents the entire contents of the JPanel - it's the canvas upon which everything is drawn.

Rather than everything having its own g2d object, everything should have its own AffineTransform object to define where it should be drawn on the canvas.

It's a good idea to reset the transformation after you've drawn your image (I think this will fix your problem) as follows:

public void draw(Graphics2D g2d) {
	  rotate.translate(location.x, location.y);
	  rotate.rotate(rotation);
	  rotate.translate(-location.x,-location.y);
	
	  // take a copy of the transformation that g2d is using
	  AffineTransform prevTransform = g2d.getTransform();

	  // apply the transformation for this object and draw
	  g2d.setTransform(rotate);
	  g2d.drawImage(currentAnimation.getCurrentFrame(), (int) location.x, (int) location.y, null);

	  // revert g2d back to the old transformation
	  g2d.setTransform(prevTransform);
}

Matt

#1de_mattT

Posted 09 December 2012 - 02:20 PM

Inded the g2d object represents the entire contents of the JPanel - it's the canvas upon which everything is drawn.

Rather than everything having its own g2d object, everything should have its own AffineTransform object to define where it should be drawn on the canvas.

Alternatively, a quick fix for you might be to reset the transformation after you've drawn your image, as follows:

public void draw(Graphics2D g2d) {
	  rotate.translate(location.x, location.y);
	  rotate.rotate(rotation);
	  rotate.translate(-location.x,-location.y);
	
	  // take a copy of the transformation that g2d is using
	  AffineTransform prevTransform = g2d.getTransform();

	  // apply the transformation for this object and draw
	  g2d.setTransform(rotate);
	  g2d.drawImage(currentAnimation.getCurrentFrame(), (int) location.x, (int) location.y, null);

	  // revert g2d back to the old transformation
	  g2d.setTransform(prevTransform);
}

Matt

PARTNERS