This commit is contained in:
2022-05-22 23:45:54 +08:00
parent a9f7967976
commit 5dfbf0b8ae
12 changed files with 462 additions and 0 deletions

View File

@@ -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);
}

View File

@@ -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.
}
}

View File

@@ -0,0 +1,5 @@
public class Start {
public static void main(String[] args) {
Shape.testShape();
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -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() {
}
}

View File

@@ -0,0 +1,6 @@
public class Start {
public static void main(String[] args) {
Shape.testShape();
Bubble.testBubble();
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -0,0 +1,105 @@
import java.awt.Graphics;
import java.util.ArrayList;
public class Model {
// instance variables
private int score;
private ArrayList<IShape> bubbles;
// constructor
public Model() {
score = 0;
bubbles = new ArrayList<IShape>();
}
// 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);
}
}

View File

@@ -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() {
}
}

View File

@@ -0,0 +1,7 @@
public class Start {
public static void main(String[] args) {
Shape.testShape();
Bubble.testBubble();
Model.testModel();
}
}