-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStars & Stripes Forever. Assignment 3
157 lines (128 loc) · 4.55 KB
/
Stars & Stripes Forever. Assignment 3
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
// Please put your name here!
public class StarsAndStripes {
public static void drawFlag(int stars, int stripes, java.awt.Graphics g, int x, int y, int width, int height) {
// Background
g.setColor(Color.white);
g.fillRect(x, y, width, height);
// Stripes
int numberOfStripes = stripes;
g.setColor(Color.red);
int stripesHeight = (height / stripes);
for (int count = 0; count < numberOfStripes; count++) {
if (count % 2 == 0) {
g.setColor(Color.red);
} else {
continue;
}
g.fillRect(x, y + (count * stripesHeight), width, stripesHeight);
}
// Blue Rectangle
int redStripes = stripes / 2;
if (stripes % 2 == 1) {
redStripes++;
}
int starfieldHeight = stripesHeight * redStripes;
int starfieldWidth = ((starfieldHeight * width) / height);
g.setColor(Color.blue);
g.fillRect(x, y, starfieldWidth, starfieldHeight);
for (int columns = 1; columns < stars; columns++) {
int rows = (stars / columns);
if ((columns > rows) && (columns < 2 * rows)) {
if (stars % columns == 0) {
int starsWidth = starfieldHeight / rows;
for (int y1 = 0; y1 < rows; y1 = y1 + 1) {
for (int x1 = 0; x1 < columns; x1 = x1 + 1) {
if ((columns > rows) && (columns < 2 * rows)) {
g.setColor(Color.white);
drawStar(g, (x + (x1 * starsWidth)), (y + (y1 * starsWidth)), starsWidth);
}
}
}
}
}
}
}
// stars
public static void drawStar(java.awt.Graphics g, int x, int y, int size) {
// Fill this in according to the assignment!
g.setColor(Color.white);
int xPointA = (x + (size / 5));
int yPointA = (y + size);
int xPointB = (x + (4 * (size / 5)));
int yPointB = (y + size);
int xPointC = (x + size);
int yPointC = (y + (2 * (size / 5)));
int xPointD = (x + (size / 2));
int yPointD = (y);
int xPointE = (x);
int yPointE = (y + (2 * (size / 5)));
g.drawLine(xPointA, yPointA, xPointC, yPointC);
g.drawLine(xPointC, yPointC, xPointE, yPointE);
g.drawLine(xPointE, yPointE, xPointB, yPointB);
g.drawLine(xPointB, yPointB, xPointD, yPointD);
g.drawLine(xPointD, yPointD, xPointA, yPointA);
}
// Only alter the "drawFlag" part of the paintComponent
// code to call it in different ways. You can also test
// drawing multiple flags at once!
public static void main(String[] args) {
JFrame window = new JFrame("Graphics window");
window.setLocationByPlatform(true);
final JLabel coords = new JLabel(" ");
@SuppressWarnings("serial")
final JPanel panel = new JPanel() {
protected void paintComponent(Graphics gx) {
coords.setText(" ");
Graphics2D g = (Graphics2D) gx;
int width = getWidth();
int height = getHeight();
g.setBackground(Color.GREEN); // To make sure you cover the base rectangle!
g.clearRect(0, 0, width, height);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.BLACK);
// You could alter this code to try different flags!
drawFlag(15, 13, g, 0, 0, width / 2, height / 2); // upper left
drawFlag(20, 14, g, width / 2, 0, width / 2, height / 2); // upper right
drawFlag(24, 15, g, 0, height / 2, width / 2, height / 2); // lower left
drawFlag(48, 16, g, width / 2, height / 2, width / 2, height / 2); // lower right
}
};
panel.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
coords.setText(e.getX() + ", " + e.getY());
}
});
window.setLayout(new BorderLayout());
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
window.setSize(d.width / 2, d.height / 2);
JPanel coordPanel = new JPanel();
coordPanel.setLayout(new BorderLayout());
coordPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
window.add(coordPanel, BorderLayout.SOUTH);
coordPanel.add(coords, BorderLayout.WEST);
window.setBackground(Color.WHITE); // To make sure you cover the base rectangle!
panel.setBackground(Color.BLACK);
window.add(panel, BorderLayout.CENTER);
// window.setContentPane(panel);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}