From 5dfbf0b8ae67429af94a24c0430bc049d1f88621 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Sun, 22 May 2022 23:45:54 +0800 Subject: [PATCH] a6 --- assignment6/Question1/IShape.java | 17 +++++ assignment6/Question1/Shape.java | 51 +++++++++++++++ assignment6/Question1/Start.java | 5 ++ assignment6/Question2/Bubble.java | 89 +++++++++++++++++++++++++ assignment6/Question2/IShape.java | 17 +++++ assignment6/Question2/Shape.java | 42 ++++++++++++ assignment6/Question2/Start.java | 6 ++ assignment6/Question3/Bubble.java | 65 ++++++++++++++++++ assignment6/Question3/IShape.java | 17 +++++ assignment6/Question3/Model.java | 105 ++++++++++++++++++++++++++++++ assignment6/Question3/Shape.java | 41 ++++++++++++ assignment6/Question3/Start.java | 7 ++ 12 files changed, 462 insertions(+) create mode 100644 assignment6/Question1/IShape.java create mode 100644 assignment6/Question1/Shape.java create mode 100644 assignment6/Question1/Start.java create mode 100644 assignment6/Question2/Bubble.java create mode 100644 assignment6/Question2/IShape.java create mode 100644 assignment6/Question2/Shape.java create mode 100644 assignment6/Question2/Start.java create mode 100644 assignment6/Question3/Bubble.java create mode 100644 assignment6/Question3/IShape.java create mode 100644 assignment6/Question3/Model.java create mode 100644 assignment6/Question3/Shape.java create mode 100644 assignment6/Question3/Start.java diff --git a/assignment6/Question1/IShape.java b/assignment6/Question1/IShape.java new file mode 100644 index 0000000..e9a3cd3 --- /dev/null +++ b/assignment6/Question1/IShape.java @@ -0,0 +1,17 @@ +import java.awt.Graphics; + +public interface IShape { + public int getX(); + + public int getY(); + + public void setX(int x); + + public void setY(int y); + + public boolean isVisible(int w, int h); + + public boolean isIn(int x, int y); + + public void draw(Graphics g); +} diff --git a/assignment6/Question1/Shape.java b/assignment6/Question1/Shape.java new file mode 100644 index 0000000..1a2a05a --- /dev/null +++ b/assignment6/Question1/Shape.java @@ -0,0 +1,51 @@ +import java.awt.Color; +import java.awt.Graphics; + +public abstract class Shape { + private int x; + private int y; + private Color color; + + /** + * Constructor for Shape + * + * @param x + * the x coordinate of the shape + * @param y + * the y coordinate of the shape + */ + public Shape(int x, int y) { + this.x = x; + this.y = y; + color = new Color((float) Math.random(), (float) Math.random(), (float) Math.random()); + } + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } + + public abstract boolean isVisible(int w, int h); + + public abstract boolean isIn(int x, int y); + + public void draw(Graphics g) { + g.setColor(color); + } + + public static void testShape() { + // TODO: implement this method + // Because this is the abstract so we don't test anything here. + } +} diff --git a/assignment6/Question1/Start.java b/assignment6/Question1/Start.java new file mode 100644 index 0000000..7afbdb6 --- /dev/null +++ b/assignment6/Question1/Start.java @@ -0,0 +1,5 @@ +public class Start { + public static void main(String[] args) { + Shape.testShape(); + } +} diff --git a/assignment6/Question2/Bubble.java b/assignment6/Question2/Bubble.java new file mode 100644 index 0000000..d224263 --- /dev/null +++ b/assignment6/Question2/Bubble.java @@ -0,0 +1,89 @@ +import java.awt.Graphics; + +public class Bubble extends Shape { + private double radius; + + /** + * Constructor for the Bubble class. + * + * @param x the x coordinate of the center of the bubble + * @param y the y coordinate of the center of the bubble + */ + public Bubble(int x, int y) { + super(x, y); + this.radius = 10; + } + + // Find the point (wx, wy) inside the window which is closest to the + // center (x, y) of the circle. In other words, find the wx in the + // interval [0, w - 1] which is closest to x, and find the wy in the + // interval [0, h - 1] which is closest to y. + // If the distance between (wx, wy) and (x, y) is less than the radius + // of the circle (using Pythagoras's theorem) then at least part of + // the circle is visible in the window. + // Note: if the center of the circle is inside the window, then (wx, wy) + // is the same as (x, y), and the distance is 0. + @Override + public boolean isVisible(int w, int h) { + double x = getX(); + double y = getY(); + double wx = (x < 0 ? 0 : (x > w - 1 ? w - 1 : x)); + double wy = (y < 0 ? 0 : (y > h - 1 ? h - 1 : y)); + double dx = wx - x; + double dy = wy - y; + return dx * dx + dy * dy <= radius * radius; + } + + /** + * Whether the point at xy is currently inside the bubble + * + * @param x the x coordinate of the point + * @param y the y coordinate of the point + * @return true if the point is inside the bubble, false otherwise + */ + @Override + public boolean isIn(int x, int y) { + double dx = x - getX(); + double dy = y - getY(); + return dx * dx + dy * dy < radius * radius; + } + + /** + * Draw the bubble. + * + * @param g the graphics context + */ + @Override + public void draw(Graphics g) { + super.draw(g); + g.drawOval( + getX() - (int) radius, + getY() - (int) radius, + (int) radius * 2, + (int) radius * 2); + } + + public static void testBubble() { + Bubble b = new Bubble(20, 30); + + System.out.println(b.getX() == 20); + System.out.println(b.getY() == 30); + + b.setX(40); + System.out.println(b.getX() == 40); + System.out.println(b.getY() == 30); + + b.setY(60); + System.out.println(b.getX() == 40); + System.out.println(b.getY() == 60); + System.out.println(b.isVisible(100, 100) == true); + + b.setX(50); + b.setY(0); + System.out.println(b.isVisible(100, 100) == true); + + b.setX(99); + b.setY(50); + System.out.println(b.isVisible(100, 100) == true); + } +} diff --git a/assignment6/Question2/IShape.java b/assignment6/Question2/IShape.java new file mode 100644 index 0000000..e9a3cd3 --- /dev/null +++ b/assignment6/Question2/IShape.java @@ -0,0 +1,17 @@ +import java.awt.Graphics; + +public interface IShape { + public int getX(); + + public int getY(); + + public void setX(int x); + + public void setY(int y); + + public boolean isVisible(int w, int h); + + public boolean isIn(int x, int y); + + public void draw(Graphics g); +} diff --git a/assignment6/Question2/Shape.java b/assignment6/Question2/Shape.java new file mode 100644 index 0000000..8f9d373 --- /dev/null +++ b/assignment6/Question2/Shape.java @@ -0,0 +1,42 @@ +import java.awt.Color; +import java.awt.Graphics; + +public abstract class Shape { + private int x; + private int y; + private Color color; + + public Shape(int x, int y) { + this.x = x; + this.y = y; + color = new Color((float) Math.random(), (float) Math.random(), (float) Math.random()); + } + + // getters and setters + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } + + public abstract boolean isVisible(int w, int h); + + public abstract boolean isIn(int x, int y); + + public void draw(Graphics g) { + g.setColor(color); + } + + public static void testShape() { + } +} diff --git a/assignment6/Question2/Start.java b/assignment6/Question2/Start.java new file mode 100644 index 0000000..f7f86b8 --- /dev/null +++ b/assignment6/Question2/Start.java @@ -0,0 +1,6 @@ +public class Start { + public static void main(String[] args) { + Shape.testShape(); + Bubble.testBubble(); + } +} diff --git a/assignment6/Question3/Bubble.java b/assignment6/Question3/Bubble.java new file mode 100644 index 0000000..03a9122 --- /dev/null +++ b/assignment6/Question3/Bubble.java @@ -0,0 +1,65 @@ +import java.awt.Graphics; + +public class Bubble extends Shape implements IShape { + // instance variables + private double radius; + + // constructor + public Bubble(int x, int y) { + super(x, y); + this.radius = 10; + } + + // Find the point (wx, wy) inside the window which is closest to the + // center (x, y) of the circle. In other words, find the wx in the + // interval [0, w - 1] which is closest to x, and find the wy in the + // interval [0, h - 1] which is closest to y. + // If the distance between (wx, wy) and (x, y) is less than the radius + // of the circle (using Pythagoras's theorem) then at least part of + // the circle is visible in the window. + // Note: if the center of the circle is inside the window, then (wx, wy) + // is the same as (x, y), and the distance is 0. + @Override + public boolean isVisible(int w, int h) { + double x = getX(); + double y = getY(); + double wx = (x < 0 ? 0 : (x > w - 1 ? w - 1 : x)); + double wy = (y < 0 ? 0 : (y > h - 1 ? h - 1 : y)); + double dx = wx - x; + double dy = wy - y; + return dx * dx + dy * dy <= radius * radius; + } + + @Override + public boolean isIn(int x, int y) { + double dx = x - getX(); + double dy = y - getY(); + return dx * dx + dy * dy < radius * radius; + } + + @Override + public void draw(Graphics g) { + super.draw(g); + g.drawOval( + getX() - (int) radius, + getY() - (int) radius, + (int) radius * 2, + (int) radius * 2); + } + + public static void testBubble() { + Bubble b = new Bubble(20, 30); + System.out.println(b.getX() == 20); + System.out.println(b.getY() == 30); + b.setX(40); + System.out.println(b.getX() == 40); + System.out.println(b.getY() == 30); + b.setY(60); + System.out.println(b.getX() == 40); + System.out.println(b.getY() == 60); + System.out.println(b.isVisible(100, 100) == true); + b.setX(50); + b.setY(0); + System.out.println(b.isVisible(100, 100) == true); + } +} diff --git a/assignment6/Question3/IShape.java b/assignment6/Question3/IShape.java new file mode 100644 index 0000000..e9a3cd3 --- /dev/null +++ b/assignment6/Question3/IShape.java @@ -0,0 +1,17 @@ +import java.awt.Graphics; + +public interface IShape { + public int getX(); + + public int getY(); + + public void setX(int x); + + public void setY(int y); + + public boolean isVisible(int w, int h); + + public boolean isIn(int x, int y); + + public void draw(Graphics g); +} diff --git a/assignment6/Question3/Model.java b/assignment6/Question3/Model.java new file mode 100644 index 0000000..bb0eb78 --- /dev/null +++ b/assignment6/Question3/Model.java @@ -0,0 +1,105 @@ +import java.awt.Graphics; +import java.util.ArrayList; + +public class Model { + // instance variables + private int score; + private ArrayList bubbles; + + // constructor + public Model() { + score = 0; + bubbles = new ArrayList(); + } + + // getters and setters + public int getScore() { + return score; + } + + /** + * @param w + * @param h + */ + public void addBubble(int w, int h) { + bubbles.add(new Bubble((int) (w * Math.random()), (int) (h * Math.random()))); + } + + /** + * @param dx + * @param dy + */ + public void moveAll(int dx, int dy) { + for (int i = 0; i < bubbles.size(); i++) { + bubbles.get(i).setX(bubbles.get(i).getX() + dx); + bubbles.get(i).setY(bubbles.get(i).getY() + dy); + } + } + + /** + * clear all bubbles + * + * @param w + * @param h + */ + public void clearInvisibles(int w, int h) { + for (int i = bubbles.size() - 1; i >= 0; i--) { + if (!bubbles.get(i).isVisible(w, h)) { + bubbles.remove(i); + score--; + } + } + } + + /** + * delete bubbles that are hit by the mouse + * + * @param x + * @param y + */ + public void deleteBubblesAtPoint(int x, int y) { + for (int i = bubbles.size() - 1; i >= 0; i--) { + if (bubbles.get(i).isIn(x, y)) { + bubbles.remove(i); + score++; + } + } + } + + /** + * draw all bubbles + * + * @param g + */ + public void drawAll(Graphics g) { + for (IShape bubble : bubbles) { + bubble.draw(g); + } + } + + public static void testModel() { + Model m = new Model(); + System.out.println(m.getScore() == 0); + System.out.println(m.bubbles.size() == 0); + m.addBubble(100, 100); + m.addBubble(100, 100); + System.out.println(m.getScore() == 0); + System.out.println(m.bubbles.size() == 2); + m.clearInvisibles(200, 200); + System.out.println(m.getScore() == 0); + System.out.println(m.bubbles.size() == 2); + m.clearInvisibles(0, 0); + System.out.println(m.getScore() == -2); + System.out.println(m.bubbles.size() == 0); + m.addBubble(100, 100); + m.addBubble(100, 100); + System.out.println(m.getScore() == -2); + System.out.println(m.bubbles.size() == 2); + m.moveAll(200, 200); + System.out.println(m.getScore() == -2); + System.out.println(m.bubbles.size() == 2); + m.clearInvisibles(200, 200); + System.out.println(m.getScore() == -4); + System.out.println(m.bubbles.size() == 0); + } +} diff --git a/assignment6/Question3/Shape.java b/assignment6/Question3/Shape.java new file mode 100644 index 0000000..e6fdfbb --- /dev/null +++ b/assignment6/Question3/Shape.java @@ -0,0 +1,41 @@ +import java.awt.Color; +import java.awt.Graphics; + +public abstract class Shape { + private int x; + private int y; + private Color color; + + public Shape(int x, int y) { + this.x = x; + this.y = y; + color = new Color((float) Math.random(), (float) Math.random(), (float) Math.random()); + } + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } + + public abstract boolean isVisible(int w, int h); + + public abstract boolean isIn(int x, int y); + + public void draw(Graphics g) { + g.setColor(color); + } + + public static void testShape() { + } +} diff --git a/assignment6/Question3/Start.java b/assignment6/Question3/Start.java new file mode 100644 index 0000000..fd30a87 --- /dev/null +++ b/assignment6/Question3/Start.java @@ -0,0 +1,7 @@ +public class Start { + public static void main(String[] args) { + Shape.testShape(); + Bubble.testBubble(); + Model.testModel(); + } +}