-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackgroundManager2.java
More file actions
68 lines (46 loc) · 1.8 KB
/
Copy pathBackgroundManager2.java
File metadata and controls
68 lines (46 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* BackgroundManager manages many backgrounds (wraparound images
used for the game's background).
Backgrounds 'further back' move slower than ones nearer the
foreground of the game, creating a parallax distance effect.
When a sprite is instructed to move left or right, the sprite
doesn't actually move, instead the backgrounds move in the
opposite direction (right or left).
*/
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class BackgroundManager2 {
private String bgImages[] = {"images2/background/1.png",
"images2/background/2.png",
"images2/background/3.png",
"images2/background/4.png",
"images2/background/5.png"};
private int moveAmount[] = {10, 4, 3, 2, 1};
// pixel amounts to move each background left or right
// a move amount of 0 makes a background stationary
private Background2[] backgrounds;
private int numBackgrounds;
private JPanel panel; // JPanel on which backgrounds are drawn
public BackgroundManager2(JPanel panel, int moveSize) {
// ignore moveSize
this.panel = panel;
numBackgrounds = bgImages.length;
backgrounds = new Background2[numBackgrounds];
for (int i = 0; i < numBackgrounds; i++) {
backgrounds[i] = new Background2(panel, bgImages[i], moveAmount[i]);
}
}
public void moveRight() {
for (int i=0; i < numBackgrounds; i++)
backgrounds[i].moveRight();
}
public void moveLeft() {
for (int i=0; i < numBackgrounds; i++)
backgrounds[i].moveLeft();
}
// The draw method draws the backgrounds on the screen. The
// backgrounds are drawn from the back to the front.
public void draw (Graphics2D g2) {
for (int i=0; i < numBackgrounds; i++)
backgrounds[i].draw(g2);
}
}