Created
April 2, 2015 05:43
-
-
Save tiltedlistener/8dc4258a1360d3002b12 to your computer and use it in GitHub Desktop.
Scrolling Background in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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