Restore
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Question3</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,159 @@
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
|
||||
public class Bubble extends Shape {
|
||||
//instance variable
|
||||
private double radius;
|
||||
|
||||
//constructor
|
||||
public Bubble(int x, int y) {
|
||||
super(x, y);
|
||||
this.radius = 10.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;
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
super.draw(g); // Call the superclass's draw method
|
||||
int x = (int)Math.round(getX() - radius);
|
||||
int y = (int)Math.round(getY() - radius);
|
||||
int diameter = (int)Math.round(radius * 2);
|
||||
g.drawOval(x, y, diameter, diameter);
|
||||
}
|
||||
|
||||
//test
|
||||
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);
|
||||
// circle fully inside window
|
||||
System.out.println(b.isVisible(100, 100) == true);
|
||||
// circle center on window top edge
|
||||
b.setX(50);
|
||||
b.setY(0);
|
||||
System.out.println(b.isVisible(100, 100) == true);
|
||||
// circle center on window right edge
|
||||
b.setX(99);
|
||||
b.setY(50);
|
||||
System.out.println(b.isVisible(100, 100) == true);
|
||||
// circle center on window bottom edge
|
||||
b.setX(50);
|
||||
b.setY(99);
|
||||
System.out.println(b.isVisible(100, 100) == true);
|
||||
// circle center on window left edge
|
||||
b.setX(0);
|
||||
b.setY(50);
|
||||
System.out.println(b.isVisible(100, 100) == true);
|
||||
// circle center on window top left corner
|
||||
b.setX(0);
|
||||
b.setY(0);
|
||||
System.out.println(b.isVisible(100, 100) == true);
|
||||
// circle center on window top right corner
|
||||
b.setX(99);
|
||||
b.setY(0);
|
||||
System.out.println(b.isVisible(100, 100) == true);
|
||||
// circle center on window bottom right corner
|
||||
b.setX(99);
|
||||
b.setY(99);
|
||||
System.out.println(b.isVisible(100, 100) == true);
|
||||
// circle center on window bottom left corner
|
||||
b.setX(0);
|
||||
b.setY(99);
|
||||
System.out.println(b.isVisible(100, 100) == true);
|
||||
// circle bottom edge on window top edge
|
||||
b.setX(50);
|
||||
b.setY(-10);
|
||||
System.out.println(b.isVisible(100, 100) == true);
|
||||
// circle left edge on window right edge
|
||||
b.setX(109);
|
||||
b.setY(50);
|
||||
System.out.println(b.isVisible(100, 100) == true);
|
||||
// circle top edge on window bottom edge
|
||||
b.setX(50);
|
||||
b.setY(109);
|
||||
System.out.println(b.isVisible(100, 100) == true);
|
||||
// circle right edge on window left edge
|
||||
b.setX(-10);
|
||||
b.setY(50);
|
||||
System.out.println(b.isVisible(100, 100) == true);
|
||||
// circle bottom right edge on window top left corner
|
||||
b.setX(-7);
|
||||
b.setY(-7);
|
||||
System.out.println(b.isVisible(100, 100) == true);
|
||||
// circle bottom left edge on window top right corner
|
||||
b.setX(106);
|
||||
b.setY(-7);
|
||||
System.out.println(b.isVisible(100, 100) == true);
|
||||
// circle top left edge on window bottom right corner
|
||||
b.setX(106);
|
||||
b.setY(106);
|
||||
System.out.println(b.isVisible(100, 100) == true);
|
||||
// circle top right edge on window bottom left corner
|
||||
b.setX(-7);
|
||||
b.setY(106);
|
||||
System.out.println(b.isVisible(100, 100) == true);
|
||||
// circle bottom edge beyond window top edge
|
||||
b.setX(50);
|
||||
b.setY(-11);
|
||||
System.out.println(b.isVisible(100, 100) == false);
|
||||
// circle left edge beyond window right edge
|
||||
b.setX(110);
|
||||
b.setY(50);
|
||||
System.out.println(b.isVisible(100, 100) == false);
|
||||
// circle top edge beyond window bottom edge
|
||||
b.setX(50);
|
||||
b.setY(110);
|
||||
System.out.println(b.isVisible(100, 100) == false);
|
||||
// circle right edge beyond window left edge
|
||||
b.setX(-11);
|
||||
b.setY(50);
|
||||
System.out.println(b.isVisible(100, 100) == false);
|
||||
// circle bottom right edge beyond window top left corner
|
||||
b.setX(-8);
|
||||
b.setY(-8);
|
||||
System.out.println(b.isVisible(100, 100) == false);
|
||||
// circle bottom left edge beyond window top right corner
|
||||
b.setX(107);
|
||||
b.setY(-8);
|
||||
System.out.println(b.isVisible(100, 100) == false);
|
||||
// circle top left edge beyond window bottom right corner
|
||||
b.setX(107);
|
||||
b.setY(107);
|
||||
System.out.println(b.isVisible(100, 100) == false);
|
||||
// circle top right edge beyond window bottom left corner
|
||||
b.setX(-8);
|
||||
b.setY(107);
|
||||
System.out.println(b.isVisible(100, 100) == false);
|
||||
b.setX(0);
|
||||
b.setY(0);
|
||||
System.out.println(b.isIn(2, 2) == true);
|
||||
System.out.println(b.isIn(10, 0) == true);
|
||||
System.out.println(b.isIn(0, 10) == true);
|
||||
System.out.println(b.isIn(7, 7) == true);
|
||||
System.out.println(b.isIn(11, 0) == false);
|
||||
System.out.println(b.isIn(0, 11) == false);
|
||||
System.out.println(b.isIn(8, 8) == false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
import java.awt.Graphics;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Model {
|
||||
//instance variables
|
||||
private int score;
|
||||
private ArrayList<IShape> bubbles;
|
||||
|
||||
//constructor
|
||||
public Model() {
|
||||
this.score = 0;
|
||||
this.bubbles = new ArrayList<IShape>(0);
|
||||
}
|
||||
|
||||
//methods
|
||||
public int getScore() {
|
||||
return this.score;
|
||||
}
|
||||
|
||||
public void addBubble(int w, int h) {
|
||||
Bubble bubble = new Bubble((int) (w * Math.random()), (int) (h * Math.random()));
|
||||
bubbles.add(bubble);
|
||||
}
|
||||
|
||||
public void moveAll(int dx, int dy) {
|
||||
for (IShape bubble : bubbles) {
|
||||
bubble.setX(bubble.getX() + dx);
|
||||
bubble.setY(bubble.getY() + dy);
|
||||
}
|
||||
}
|
||||
|
||||
public void clearInvisibles(int w, int h) {
|
||||
for (int i = bubbles.size() - 1; i >= 0; i--) {
|
||||
IShape b = bubbles.get(i);
|
||||
if (!b.isVisible(w, h)) {
|
||||
bubbles.remove(i);
|
||||
this.score--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteBubblesAtPoint(int x, int y) {
|
||||
for (int i = bubbles.size() - 1; i >= 0; i--) {
|
||||
IShape bubble = bubbles.get(i);
|
||||
if (bubble.isIn(x, y)) {
|
||||
bubbles.remove(i);
|
||||
score++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void drawAll(Graphics g) {
|
||||
for(IShape b: bubbles) {
|
||||
b.draw(g);
|
||||
}
|
||||
}
|
||||
|
||||
public static void testModel() {
|
||||
Model m = new Model();
|
||||
System.out.println(m.getScore() == 0);
|
||||
// Testing the bubbles arraylist by directly accessing
|
||||
// the arraylist inside the object m. This is not very clean (it only
|
||||
// works when the tests are done from inside the class itself) but it
|
||||
// allows us to test more methods.
|
||||
System.out.println(m.bubbles.size() == 0);
|
||||
// Add two bubbles.
|
||||
m.addBubble(100, 100);
|
||||
m.addBubble(100, 100);
|
||||
System.out.println(m.getScore() == 0);
|
||||
System.out.println(m.bubbles.size() == 2);
|
||||
// The two bubbles we have are somewhere in the square ([0, 99), [0, 99)).
|
||||
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);
|
||||
// Adding two bubbles again and moving them out of the window.
|
||||
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);
|
||||
// Adding two bubbles again and clicking into them to remove them.
|
||||
m.addBubble(10, 10);
|
||||
m.addBubble(10, 10);
|
||||
System.out.println(m.getScore() == -4);
|
||||
System.out.println(m.bubbles.size() == 2);
|
||||
// The two bubbles we have are somewhere in the square ([0, 9), [0, 9)).
|
||||
m.deleteBubblesAtPoint(5, 5);
|
||||
System.out.println(m.getScore() == -2);
|
||||
System.out.println(m.bubbles.size() == 0);
|
||||
// We cannot test the drawAll method because we don't have any graphics object.
|
||||
// Adding a bubble again and clearing everything.
|
||||
m.addBubble(100, 100);
|
||||
System.out.println(m.getScore() == -2);
|
||||
System.out.println(m.bubbles.size() == 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
|
||||
public abstract class Shape implements IShape{
|
||||
//instance variables
|
||||
private int x;
|
||||
private int y;
|
||||
private Color color;
|
||||
|
||||
//constructor
|
||||
public Shape(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.color = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getX() {
|
||||
return this.x;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getY() {
|
||||
return this.y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract boolean isVisible(int w, int h);
|
||||
|
||||
@Override
|
||||
public abstract boolean isIn(int x, int y);
|
||||
|
||||
@Override
|
||||
public void draw(Graphics g) {
|
||||
g.setColor(this.color);
|
||||
}
|
||||
|
||||
public static void testShape() {}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Shape.testShape();
|
||||
Bubble.testBubble();
|
||||
Model.testModel();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user