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
|
package se.liu.gusso230.shapes;
import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.Random;
public class DiagramViewer {
private final static List<Color> COLORS =
List.of(Color.BLACK, Color.RED, Color.GREEN, Color.BLUE, Color.CYAN, Color.YELLOW, Color.MAGENTA);
private final static Random rnd = new Random(0);
private static Color getRandomColor() {
return COLORS.get(rnd.nextInt(COLORS.size()));
}
private static Circle getRandomCircle() {
return new Circle(rnd.nextInt(400), rnd.nextInt(400), rnd.nextInt(200), getRandomColor());
}
private static Rectangle getRandomRectangle() {
return new Rectangle(rnd.nextInt(400), rnd.nextInt(400), rnd.nextInt(200), rnd.nextInt(200), getRandomColor());
}
private static Text getRandomText() {
return new Text(rnd.nextInt(400), rnd.nextInt(400), 16, getRandomColor(), "hello");
}
public static void main(String[] args) {
DiagramComponent comp = new DiagramComponent();
final Random rnd = new Random(0);
for (int i = 0; i < 10; i++) {
switch (rnd.nextInt(3)) {
case 0:
comp.addShape(getRandomCircle());
break;
case 1:
comp.addShape(getRandomRectangle());
break;
case 2:
comp.addShape(getRandomText());
break;
}
}
JFrame frame = new JFrame("My window");
frame.setLayout(new BorderLayout());
frame.add(comp, BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setVisible(true);
}
}
|