Skip to content

Instantly share code, notes, and snippets.

@tiltedlistener
Created April 2, 2015 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiltedlistener/8dc4258a1360d3002b12 to your computer and use it in GitHub Desktop.
Save tiltedlistener/8dc4258a1360d3002b12 to your computer and use it in GitHub Desktop.
Scrolling Background in Java
public void drawBackground() {
// CIRCLE is a static var defined as MATH.PI * 2
// graphics() returns a Graphics2D context in this case from a BufferedImage createGraphics() method
rotationOfBackground += 0.05;
if(rotationOfBackground >= CIRCLE) {
rotationOfBackground -= CIRCLE;
}
// In this case we're assuming that 300 is the height that we need
int width = (int)(background.getWidth(this));
int left =(int)((rotationOfBackground / CIRCLE) * -width);
graphics().drawImage(background, left, 0, width, 300, this);
if (left < width - this.screenWidth) {
graphics().drawImage(background, left + width, 0, width, 300, this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment