-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackground2.java
More file actions
87 lines (58 loc) · 1.82 KB
/
Copy pathBackground2.java
File metadata and controls
87 lines (58 loc) · 1.82 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.awt.Graphics2D;
import java.awt.Dimension;
import java.awt.Image;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
public class Background2 {
private Image bgImage;
private int bgImageWidth; // width of the background (>= panel Width)
private Dimension dimension;
private int bgX;
private int backgroundX;
private int backgroundX2;
private int bgDX; // size of the background move (in pixels)
public Background2(JPanel panel, String imageFile, int bgDX) {
this.bgImage = loadImage(imageFile);
bgImageWidth = bgImage.getWidth(null); // get width of the background
System.out.println ("bgImageWidth = " + bgImageWidth);
dimension = panel.getSize();
if (bgImageWidth < dimension.width)
System.out.println("Background width < panel width");
this.bgDX = bgDX;
}
public void moveRight() {
if (bgX == 0) {
backgroundX = 0;
backgroundX2 = bgImageWidth;
}
bgX = bgX - bgDX;
backgroundX = backgroundX - bgDX;
backgroundX2 = backgroundX2 - bgDX;
if ((bgX + bgImageWidth) % bgImageWidth == 0) {
System.out.println ("Background change: bgX = " + bgX);
backgroundX = 0;
backgroundX2 = bgImageWidth;
}
}
public void moveLeft() {
if (bgX == 0) {
backgroundX = bgImageWidth * -1;
backgroundX2 = 0;
}
bgX = bgX + bgDX;
backgroundX = backgroundX + bgDX;
backgroundX2 = backgroundX2 + bgDX;
if ((bgX + bgImageWidth) % bgImageWidth == 0) {
//System.out.println ("Background change: bgX = " + bgX);
backgroundX = bgImageWidth * -1;
backgroundX2 = 0;
}
}
public void draw (Graphics2D g2) {
g2.drawImage(bgImage, backgroundX, 0, null);
g2.drawImage(bgImage, backgroundX2, 0, null);
}
public Image loadImage (String fileName) {
return new ImageIcon(fileName).getImage();
}
}