summaryrefslogtreecommitdiffstats
path: root/src/se/liu/gusso230/shapes/DiagramViewer.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/se/liu/gusso230/shapes/DiagramViewer.java')
-rw-r--r--src/se/liu/gusso230/shapes/DiagramViewer.java64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/se/liu/gusso230/shapes/DiagramViewer.java b/src/se/liu/gusso230/shapes/DiagramViewer.java
new file mode 100644
index 0000000..56a63d0
--- /dev/null
+++ b/src/se/liu/gusso230/shapes/DiagramViewer.java
@@ -0,0 +1,64 @@
+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);
+ }
+}