This is the third in a series of posts on how to put a scrolling background. We started with ActionScript and went to Python. Now it’s time for Java.
Quick Demo
Here’s the same demo as the previous two in the series. What we’re going to be creating is two copies of a background image that scroll across and then leap frog back to the right when they’re off stage to the left.
Click to start and click to stop.
Sorry, either Adobe flash is not installed or you do not have it enabledThe Code
In the previous posts I’ve showed the code and then went back and explained. This time, since Java involves quite a bit more code, I put comments into the code instead.
There are 3 files as part of this demo; one to hold an individual background, one is the canvas that holds both background instances, and a runner.
Background.java
This is an individual copy of the background. It takes care of the motion, drawing to a Graphics reference, and moving back when needed.
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class Background {
private BufferedImage image;
private int x;
private int y;
public Background() {
this(0,0);
}
public Background(int x, int y) {
this.x = x;
this.y = y;
// Try to open the image file background.png
try {
image = ImageIO.read(new File("background.png"));
}
catch (Exception e) { System.out.println(e); }
}
/**
* Method that draws the image onto the Graphics object passed
* @param window
*/
public void draw(Graphics window) {
// Draw the image onto the Graphics reference
window.drawImage(image, getX(), getY(), image.getWidth(), image.getHeight(), null);
// Move the x position left for next time
this.x -= 5;
// Check to see if the image has gone off stage left
if (this.x <= -1 * image.getWidth()) {
// If it has, line it back up so that its left edge is
// lined up to the right side of the other background image
this.x = this.x + image.getWidth() * 2;
}
}
public void setX(int x) {
this.x = x;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public int getImageWidth() {
return image.getWidth();
}
public String toString() {
return "Background: x=" + getX() + ", y=" + getY() + ", height=" + image.getHeight() + ", width=" + image.getWidth();
}
}
ScrollingBackground.java
ScrollingBackground holds two instances of the Background object and takes care of drawing them on the stage.
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
public class ScrollingBackground extends Canvas implements Runnable {
// Two copies of the background image to scroll
private Background backOne;
private Background backTwo;
private BufferedImage back;
public ScrollingBackground() {
backOne = new Background();
backTwo = new Background(backOne.getImageWidth(), 0);
new Thread(this).start();
setVisible(true);
}
@Override
public void run() {
try {
while (true) {
Thread.currentThread().sleep(5);
repaint();
}
}
catch (Exception e) {}
}
@Override
public void update(Graphics window) {
paint(window);
}
public void paint(Graphics window) {
Graphics2D twoD = (Graphics2D)window;
if (back == null)
back = (BufferedImage)(createImage(getWidth(), getHeight()));
// Create a buffer to draw to
Graphics buffer = back.createGraphics();
// Put the two copies of the background image onto the buffer
backOne.draw(buffer);
backTwo.draw(buffer);
// Draw the image onto the window
twoD.drawImage(back, null, 0, 0);
}
}
Runner.java
Runner puts everything together.
import java.awt.Component;
import javax.swing.JFrame;
public class Runner extends JFrame {
public Runner() {
super("Scrolling Background Demo");
setSize(550, 250);
ScrollingBackground back = new ScrollingBackground();
((Component)back).setFocusable(true);
getContentPane().add(back);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Runner();
}
}
In This Series
This post is part of a series of posts on how to create scrolling backgrounds.
- Scrolling Background in Flash with ActionScript
- Scrolling Background in Python
- Scrolling Background in Java