-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyAmericanFlag.java
More file actions
133 lines (111 loc) · 3.7 KB
/
Copy pathMyAmericanFlag.java
File metadata and controls
133 lines (111 loc) · 3.7 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class MyAmericanFlag extends JPanel {
private final int blockSize = 20;
private final int rows = 13;
private final int cols = 30;
private final int starRows = 7;
private final int starCols = 12;
private List<Star> stars = new ArrayList<>();
private List<RedStripe> redStripes = new ArrayList<>();
private int starStep = 0;
public MyAmericanFlag() {
setPreferredSize(new Dimension(cols * blockSize, rows * blockSize));
// Stars initialization
for (int r = 0; r < starRows; r++) {
for (int c = 0; c < starCols; c++) {
if ((r + c) % 2 == 0) {
stars.add(new Star(c, r));
}
}
}
// Red stripes initialization
for (int r = 0; r < rows; r += 2) { // even rows are red
redStripes.add(new RedStripe(r));
}
Timer timer = new Timer(600, e -> { // faster for smoother animation
starStep = (starStep + 1) % 2;
updateStars();
updateRedStripes();
repaint();
});
timer.start();
}
private void updateStars() {
for (Star s : stars) {
if (s.baseY == 0 || s.baseY == starRows - 1)
s.offsetY = (starStep == 1) ? -1 : 0;
else
s.offsetY = 0;
if (s.baseY != 0 && s.baseY != starRows - 1) {
if ((s.baseX + s.baseY) % 2 == 0)
s.offsetX = (starStep == 1) ? -1 : 0;
else
s.offsetX = (starStep == 1) ? 1 : 0;
} else s.offsetX = 0;
}
}
private void updateRedStripes() {
for (RedStripe stripe : redStripes) {
stripe.update();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw white background first
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
// Draw red stripes with offsets
g.setColor(Color.RED);
for (RedStripe stripe : redStripes) {
for (int c = 0; c < cols; c++) {
int y = stripe.baseRow * blockSize + stripe.offsets[c] * blockSize;
g.fillRect(c * blockSize, y, blockSize, blockSize);
}
}
// Blue rectangle
g.setColor(Color.BLUE);
g.fillRect(0, 0, starCols * blockSize, starRows * blockSize);
// Stars
g.setColor(Color.WHITE);
for (Star s : stars) {
g.fillRect((s.baseX + s.offsetX) * blockSize + 3,
(s.baseY + s.offsetY) * blockSize + 3,
blockSize - 6, blockSize - 6);
}
}
class Star {
int baseX, baseY;
int offsetX = 0, offsetY = 0;
public Star(int x, int y) {
baseX = x;
baseY = y;
}
}
class RedStripe {
int baseRow;
int[] offsets;
public RedStripe(int row) {
baseRow = row;
offsets = new int[cols];
}
public void update() {
// Keep the original flip-flop per-column movement
for (int c = 0; c < offsets.length; c++) {
offsets[c] = (offsets[c] == 0) ? -1 : 0;
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("My American Flag");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MyAmericanFlag());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
// Thank you to all you ugly americans.