-
Notifications
You must be signed in to change notification settings - Fork 0
/
LegosP2.java
55 lines (47 loc) · 1.44 KB
/
LegosP2.java
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
package legoMania;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class LegosP2 extends JPanel {
static final long serialVersionUID = 42L;
@Override
public void paintComponent(Graphics g) {
int startX = 20;
int startY = 260;
int legoWidth = 50;
int legoHeight = 20;
int baseLength = 10;
int arcWidth = 2;
int arcHeight = 2;
Color[] colors ={Color.red, Color.BLUE, Color.YELLOW,
Color.green, Color.pink,Color.black, Color.magenta,
Color.orange, Color.cyan};
super.paintComponent(g);
//draw Rectangle
Color color = Color.red;
for (int i=baseLength;i>=1;i--) {
for (int j = 0;j<i;j++) {
g.setColor(color);
Random rand = new Random();
int x = rand.nextInt(colors.length);
color = colors[x];
int gap=(baseLength-i)*(legoWidth/2);
g.fillRoundRect(gap+(j*legoWidth)+startX, startY-(baseLength-i)*legoHeight, legoWidth, legoHeight, arcWidth, arcHeight);
}
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(550,325);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Abunaw's LEGOS Block");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LegosP2 d = new LegosP2();
JPanel panel = new JPanel();
panel.add(d);
frame.add(panel);
frame.setSize(550,325);
frame.setVisible(true);
}
}