summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-02-01 11:01:27 +0100
committerGustav Sörnäs <gustav@sornas.net>2021-02-01 11:01:27 +0100
commit403f99cf6dbbf2448764742ebaae61999c5bd958 (patch)
tree53264b072917c3d9494563370a9a5bf69dc1529d /src
parent967eb05b130017bc1ea197c5b23ec1c953c3c5ca (diff)
downloadtdde30-403f99cf6dbbf2448764742ebaae61999c5bd958.tar.gz
lab 3
Diffstat (limited to 'src')
-rw-r--r--src/se/liu/gusso230/lab3/ListManipulator.java40
-rw-r--r--src/se/liu/gusso230/lab3/ListTest.java33
-rw-r--r--src/se/liu/gusso230/lab3/Queue.java14
-rw-r--r--src/se/liu/gusso230/lab3/Stack.java13
-rw-r--r--src/se/liu/gusso230/shapes/AbstractShape.java43
-rw-r--r--src/se/liu/gusso230/shapes/Circle.java29
-rw-r--r--src/se/liu/gusso230/shapes/CircleTest.java19
-rw-r--r--src/se/liu/gusso230/shapes/DiagramComponent.java25
-rw-r--r--src/se/liu/gusso230/shapes/DiagramViewer.java64
-rw-r--r--src/se/liu/gusso230/shapes/Rectangle.java23
-rw-r--r--src/se/liu/gusso230/shapes/Shape.java13
-rw-r--r--src/se/liu/gusso230/shapes/ShapeTest.java21
-rw-r--r--src/se/liu/gusso230/shapes/Text.java24
-rw-r--r--src/se/liu/gusso230/tetris/Poly.java2
14 files changed, 362 insertions, 1 deletions
diff --git a/src/se/liu/gusso230/lab3/ListManipulator.java b/src/se/liu/gusso230/lab3/ListManipulator.java
new file mode 100644
index 0000000..9a75049
--- /dev/null
+++ b/src/se/liu/gusso230/lab3/ListManipulator.java
@@ -0,0 +1,40 @@
+package se.liu.gusso230.lab3;
+
+import se.liu.gusso230.lab1.Person;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+public class ListManipulator
+{
+ protected List<Person> elements = new ArrayList<>();
+
+ public int size() {
+ return elements.size();
+ }
+
+ public boolean isEmpty() {
+ return elements.isEmpty();
+ }
+
+ public boolean contains(final Object o) {
+ return elements.contains(o);
+ }
+
+ public Iterator<Person> iterator() {
+ return elements.iterator();
+ }
+
+ public boolean add(final Person person) {
+ return elements.add(person);
+ }
+
+ public boolean remove(final Object o) {
+ return elements.remove(o);
+ }
+
+ public void clear() {
+ elements.clear();
+ }
+}
diff --git a/src/se/liu/gusso230/lab3/ListTest.java b/src/se/liu/gusso230/lab3/ListTest.java
new file mode 100644
index 0000000..565791f
--- /dev/null
+++ b/src/se/liu/gusso230/lab3/ListTest.java
@@ -0,0 +1,33 @@
+package se.liu.gusso230.lab3;
+
+import se.liu.gusso230.lab1.Person;
+
+import java.time.LocalDate;
+
+public class ListTest {
+ public static void main(String[] args) {
+ Queue q = new Queue();
+ q.enqueue(new Person("1", LocalDate.of(2020, 1, 1)));
+ q.enqueue(new Person("2", LocalDate.of(2019, 1, 1)));
+ q.enqueue(new Person("3", LocalDate.of(2018, 1, 1)));
+ q.enqueue(new Person("4", LocalDate.of(2017, 1, 1)));
+ q.enqueue(new Person("5", LocalDate.of(2016, 1, 1)));
+
+ while (!q.isEmpty()) {
+ System.out.println(q.dequeue());
+ }
+
+ System.out.println();
+
+ Stack s = new Stack();
+ s.push(new Person("1", LocalDate.of(2020, 1, 1)));
+ s.push(new Person("2", LocalDate.of(2019, 1, 1)));
+ s.push(new Person("3", LocalDate.of(2018, 1, 1)));
+ s.push(new Person("4", LocalDate.of(2017, 1, 1)));
+ s.push(new Person("5", LocalDate.of(2016, 1, 1)));
+
+ while (!s.isEmpty()) {
+ System.out.println(s.pop());
+ }
+ }
+}
diff --git a/src/se/liu/gusso230/lab3/Queue.java b/src/se/liu/gusso230/lab3/Queue.java
new file mode 100644
index 0000000..32ee688
--- /dev/null
+++ b/src/se/liu/gusso230/lab3/Queue.java
@@ -0,0 +1,14 @@
+package se.liu.gusso230.lab3;
+
+import se.liu.gusso230.lab1.Person;
+
+public class Queue extends ListManipulator {
+ public void enqueue(Person p) {
+ elements.add(p);
+ }
+
+ public Person dequeue() {
+ return elements.remove(0);
+ }
+
+}
diff --git a/src/se/liu/gusso230/lab3/Stack.java b/src/se/liu/gusso230/lab3/Stack.java
new file mode 100644
index 0000000..a346420
--- /dev/null
+++ b/src/se/liu/gusso230/lab3/Stack.java
@@ -0,0 +1,13 @@
+package se.liu.gusso230.lab3;
+
+import se.liu.gusso230.lab1.Person;
+
+public class Stack extends ListManipulator {
+ public void push(Person p) {
+ elements.add(0, p);
+ }
+
+ public Person pop() {
+ return elements.remove(0);
+ }
+}
diff --git a/src/se/liu/gusso230/shapes/AbstractShape.java b/src/se/liu/gusso230/shapes/AbstractShape.java
new file mode 100644
index 0000000..41a7294
--- /dev/null
+++ b/src/se/liu/gusso230/shapes/AbstractShape.java
@@ -0,0 +1,43 @@
+package se.liu.gusso230.shapes;
+
+import java.awt.*;
+import java.util.Objects;
+
+public abstract class AbstractShape implements Shape {
+ protected int x;
+ protected int y;
+ protected Color color;
+
+ @Override public boolean equals(final Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ final AbstractShape that = (AbstractShape) o;
+ return x == that.x && y == that.y && color.equals(that.color);
+ }
+
+ @Override public int hashCode() {
+ return Objects.hash(x, y, color);
+ }
+
+ public AbstractShape(int x, int y, Color color) {
+ this.x = x;
+ this.y = y;
+ this.color = color;
+ }
+
+ @Override public int getX() {
+ return x;
+ }
+
+ @Override public int getY() {
+ return y;
+ }
+
+ @Override public Color getColor() {
+ return color;
+ }
+}
diff --git a/src/se/liu/gusso230/shapes/Circle.java b/src/se/liu/gusso230/shapes/Circle.java
new file mode 100644
index 0000000..1d70499
--- /dev/null
+++ b/src/se/liu/gusso230/shapes/Circle.java
@@ -0,0 +1,29 @@
+package se.liu.gusso230.shapes;
+
+import java.awt.*;
+
+public class Circle extends AbstractShape {
+ private int radius;
+
+ public Circle(int x, int y, int radius, Color color) {
+ super(x, y, color);
+ if (radius < 0) {
+ throw new IllegalArgumentException("Negative radius");
+ }
+
+ this.radius = radius;
+ }
+
+ @Override public void draw(final Graphics g) {
+ g.setColor(color);
+ g.drawOval(x, y, radius * 2, radius * 2);
+ }
+
+ public int getRadius() {
+ return radius;
+ }
+
+ @Override public String toString() {
+ return "Circle{" + "x=" + x + ", y=" + y + ", radius=" + radius + ", color=" + color + '}';
+ }
+}
diff --git a/src/se/liu/gusso230/shapes/CircleTest.java b/src/se/liu/gusso230/shapes/CircleTest.java
new file mode 100644
index 0000000..84d912b
--- /dev/null
+++ b/src/se/liu/gusso230/shapes/CircleTest.java
@@ -0,0 +1,19 @@
+package se.liu.gusso230.shapes;
+
+import java.awt.*;
+import java.util.ArrayList;
+import java.util.List;
+
+public class CircleTest {
+ public static void main(String[] args) {
+ final List<Circle> circles = new ArrayList<>();
+
+ circles.add(new Circle(0, 0, 1, Color.BLACK));
+ circles.add(new Circle(1, 1, 1, Color.RED));
+ circles.add(new Circle(5, 0, 3, Color.BLUE));
+
+ for (Circle circle : circles) {
+ System.out.println(circle.getX() + " " + circle.getY());
+ }
+ }
+}
diff --git a/src/se/liu/gusso230/shapes/DiagramComponent.java b/src/se/liu/gusso230/shapes/DiagramComponent.java
new file mode 100644
index 0000000..5730e0d
--- /dev/null
+++ b/src/se/liu/gusso230/shapes/DiagramComponent.java
@@ -0,0 +1,25 @@
+package se.liu.gusso230.shapes;
+
+import javax.swing.*;
+import java.awt.*;
+import java.util.ArrayList;
+import java.util.List;
+
+public class DiagramComponent extends JComponent {
+ private List<Shape> shapes;
+
+ @Override protected void paintComponent(final Graphics g) {
+ super.paintComponent(g);
+ for (Shape shape: shapes) {
+ shape.draw(g);
+ }
+ }
+
+ public DiagramComponent() {
+ shapes = new ArrayList<>();
+ }
+
+ public void addShape(Shape s) {
+ shapes.add(s);
+ }
+}
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);
+ }
+}
diff --git a/src/se/liu/gusso230/shapes/Rectangle.java b/src/se/liu/gusso230/shapes/Rectangle.java
new file mode 100644
index 0000000..0d16655
--- /dev/null
+++ b/src/se/liu/gusso230/shapes/Rectangle.java
@@ -0,0 +1,23 @@
+package se.liu.gusso230.shapes;
+
+import java.awt.*;
+
+public class Rectangle extends AbstractShape {
+ private int width;
+ private int height;
+
+ public Rectangle(final int x, final int y, final int width, final int height, final Color color) {
+ super(x, y, color);
+ this.width = width;
+ this.height = height;
+ }
+
+ @Override public void draw(final Graphics g) {
+ g.setColor(color);
+ g.drawRect(x, y, width, height);
+ }
+
+ @Override public String toString() {
+ return "Rectangle{" + "x=" + x + ", y=" + y + ", width=" + width + ", height=" + height + ", color=" + color + '}';
+ }
+}
diff --git a/src/se/liu/gusso230/shapes/Shape.java b/src/se/liu/gusso230/shapes/Shape.java
new file mode 100644
index 0000000..7685e40
--- /dev/null
+++ b/src/se/liu/gusso230/shapes/Shape.java
@@ -0,0 +1,13 @@
+package se.liu.gusso230.shapes;
+
+import java.awt.*;
+
+public interface Shape {
+ void draw(final Graphics g);
+
+ int getX();
+
+ int getY();
+
+ Color getColor();
+}
diff --git a/src/se/liu/gusso230/shapes/ShapeTest.java b/src/se/liu/gusso230/shapes/ShapeTest.java
new file mode 100644
index 0000000..e0e8601
--- /dev/null
+++ b/src/se/liu/gusso230/shapes/ShapeTest.java
@@ -0,0 +1,21 @@
+package se.liu.gusso230.shapes;
+
+import java.awt.*;
+import java.util.ArrayList;
+import java.util.List;
+
+public class ShapeTest {
+ public static void main(String[] args) {
+ final List<Shape> shapes = new ArrayList<>();
+
+ shapes.add(new Circle(0, 0, 1, Color.BLACK));
+ shapes.add(new Circle(1, 1, 1, Color.RED));
+ shapes.add(new Circle(5, 0, 3, Color.BLUE));
+ shapes.add(new Rectangle(5, 2, 1, 2, Color.BLUE));
+ shapes.add(new Text(5, 2, 12, Color.BLUE, "Hello World!"));
+
+ for (Shape shape : shapes) {
+ //shape.draw();
+ }
+ }
+}
diff --git a/src/se/liu/gusso230/shapes/Text.java b/src/se/liu/gusso230/shapes/Text.java
new file mode 100644
index 0000000..9abd63b
--- /dev/null
+++ b/src/se/liu/gusso230/shapes/Text.java
@@ -0,0 +1,24 @@
+package se.liu.gusso230.shapes;
+
+import java.awt.*;
+
+public class Text extends AbstractShape {
+ private int size;
+ private String text;
+
+ public Text(final int x, final int y, final int size, final Color color, final String text) {
+ super(x, y, color);
+ this.size = size;
+ this.text = text;
+ }
+
+ @Override public void draw(final Graphics g) {
+ g.setColor(color);
+ g.setFont(new Font("serif", Font.PLAIN, size));
+ g.drawString(text, x, y);
+ }
+
+ @Override public String toString() {
+ return "Text{" + "x=" + x + ", y=" + y + ", size=" + size + ", color=" + color + ", text='" + text + '\'' + '}';
+ }
+}
diff --git a/src/se/liu/gusso230/tetris/Poly.java b/src/se/liu/gusso230/tetris/Poly.java
index d410abe..bedcd77 100644
--- a/src/se/liu/gusso230/tetris/Poly.java
+++ b/src/se/liu/gusso230/tetris/Poly.java
@@ -42,7 +42,7 @@ public class Poly {
*/
public void rotateClockwise() {
for (Point point : points) {
- point.rotateClockw:while ()ise(boundingBoxSize);
+ point.rotateClockwise(boundingBoxSize);
}
}