This commit is contained in:
ldy
2026-03-01 23:18:55 -05:00
commit 67f753a5d1
3087 changed files with 218259 additions and 0 deletions

View File

@@ -0,0 +1,157 @@
import java.awt.Graphics;
public class Bubble extends Shape {
//instance variable
private double radius = 10;
//constructor
public Bubble(int x, int y) {
super(x, y);
}
@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 cx = getX();
double cy = getY();
if(x > cx - 10 && x < cx + 10 && y > cy - 10 && y < cy + 10) {
return true;
}
return false;
}
@Override
public void draw(Graphics g) {
drawOval(getX(), getY(), radius, radius);
}
//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);
}
}

View File

@@ -0,0 +1,63 @@
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ViewBubbles extends JPanel{
//instance variable
private Model model;
//constructor
public ViewBubbles(Model model) {
this.model = model;
this.setBackground(Color.WHITE);
//timer
Timer timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveUp(getWidth(), getHeight());
}
});
timer.start();
//mouse listener
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
clickBubbles(e.getX(), e.getY());
}
});
}
//methods
private void moveUp(int w, int h) {
model.moveAll(0, -1);
model.clearInvisibles(w, h);
model.addBubble(w, h);
int prevScore = model.getScore();
if (model.getScore() != prevScore) {
System.out.println("Current Score: " + model.getScore());
}
repaint();
}
private void clickBubbles(int x, int y) {
model.deleteBubblesAtPoint(x, y);
int prevScore = model.getScore();
if (model.getScore() != prevScore) {
System.out.println("Current Score: " + model.getScore());
}
repaint();
}
protected void paintComponent(Graphics g) {
model.drawAll(g);
}
}

View File

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

View File

@@ -0,0 +1,65 @@
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ViewBubbles extends JPanel{
//instance variable
private Model model;
//constructor
public ViewBubbles(Model model) {
this.model = model;
this.setBackground(Color.WHITE);
//timer
Timer timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveUp(getWidth(), getHeight());
}
});
timer.start();
//mouse listener
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
clickBubbles(e.getX(), e.getY());
}
});
}
//methods
private void moveUp(int w, int h) {
model.moveAll(0, -1);
int previousScore = model.getScore();
model.clearInvisibles(w, h);
model.addBubble(w, h);
int currentScore = model.getScore();
if (currentScore != previousScore) {
System.out.println("Current score: " + currentScore);
}
repaint();
}
private void clickBubbles(int x, int y) {
int previousScore = model.getScore();
model.deleteBubblesAtPoint(x, y);
int currentScore = model.getScore();
if (currentScore != previousScore) {
System.out.println("Current score: " + currentScore);
}
repaint();
}
protected void paintComponent(Graphics g) {
model.drawAll(g);
}
}

View File

@@ -0,0 +1,102 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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(IShape b: bubbles) {
if(b.getX() == x && b.getY() == y) {
bubbles.remove(b);
this.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);
}
}

View File

@@ -0,0 +1,101 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
new Bubble((int)(w * Math.random()), (int)(h * Math.random()));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.getY()+dy);
}
}
public void clearInvisibles(int w, int h) {
for(IShape b: bubbles) {
if(!b.isVisible(w, h)) {
bubbles.remove(b);
this.score--;
}
}
}
public void deleteBubblesAtPoint(int x, int y) {
for(IShape b: bubbles) {
if(b.getX() == x && b.getY() == y) {
bubbles.remove(b);
this.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);
}
}

View File

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

View File

@@ -0,0 +1,103 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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(IShape b: bubbles) {
if(b.getX() == x && b.getY() == y) {
bubbles.remove(b);
this.score++;
return;
}
}
}
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(0, 9);
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);
}
}

View File

@@ -0,0 +1,103 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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(IShape b: bubbles) {
if(b.getX() == x && b.getY() == y) {
bubbles.remove(b);
this.score++;
return;
}
}
}
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());
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());
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);
}
}

View File

@@ -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(color);
}
public static void testShape() {}
}

View File

@@ -0,0 +1,66 @@
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ViewBubbles extends JPanel{
//instance variable
private Model model;
//constructor
public ViewBubbles(Model model) {
this.model = model;
this.setBackground(Color.WHITE);
//timer
Timer timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveUp(getWidth(), getHeight());
}
});
timer.start();
//mouse listener
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
clickBubbles(e.getX(), e.getY());
}
});
}
//methods
private void moveUp(int w, int h) {
model.moveAll(0, -1);
int previousScore = model.getScore();
model.clearInvisibles(w, h);
model.addBubble(w, h);
int currentScore = model.getScore();
if (currentScore != previousScore) {
System.out.println("Current score: " + currentScore);
}
repaint();
}
private void clickBubbles(int x, int y) {
int previousScore = model.getScore();
model.deleteBubblesAtPoint(x, y);
int currentScore = model.getScore();
if (currentScore != previousScore) {
System.out.println("Current score: " + currentScore);
}
repaint();
}
protected void paintComponent(Graphics g) {
model.drawAll(g);
}
}

View File

@@ -0,0 +1,103 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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);
}
}

View File

@@ -0,0 +1,159 @@
import java.awt.Color;
import java.awt.Graphics;
public class Bubble extends Shape {
//instance variable
private double radius = 10;
//constructor
public Bubble(int x, int y) {
super(x, y);
}
@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 centerX = getX();
double centerY = getY();
double distance = Math.sqrt((x - centerX) * (x - centerX) + (y - centerY) * (y - centerY));
return distance <= radius;
}
@Override
public void draw(Graphics g) {
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);
}
}

View File

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

View File

@@ -0,0 +1,64 @@
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ViewBubbles extends JPanel{
//instance variable
private Model model;
//constructor
public ViewBubbles(Model model) {
this.model = model;
this.setBackground(Color.WHITE);
//timer
Timer timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveUp(getWidth(), getHeight());
}
});
timer.start();
//mouse listener
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
clickBubbles(e.getX(), e.getY());
}
});
}
//methods
private void moveUp(int w, int h) {
model.moveAll(0, -1);
model.clearInvisibles(w, h);
model.addBubble(w, h);
int prevScore = model.getScore();
if (model.getScore() != prevScore) {
System.out.println("Current Score: " + model.getScore());
}
repaint();
}
private void clickBubbles(int x, int y) {
model.deleteBubblesAtPoint(x, y);
int prevScore = model.getScore();
if (model.getScore() != prevScore) {
System.out.println("Current Score: " + model.getScore());
}
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
model.drawAll(g);
}
}

View File

@@ -0,0 +1,102 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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);
score--;
}
}
}
public void deleteBubblesAtPoint(int x, int y) {
for(IShape b: bubbles) {
if(b.getX() == x && b.getY() == y) {
bubbles.remove(b);
this.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);
}
}

View File

@@ -0,0 +1,103 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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);
}
}

View File

@@ -0,0 +1,103 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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(double x, double y) {
for (int i = bubbles.size() - 1; i >= 0; i--) {
Bubble 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);
}
}

View File

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

View File

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

View File

@@ -0,0 +1,103 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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);
score--;
}
}
}
public void deleteBubblesAtPoint(int x, int y) {
for(IShape b: bubbles) {
if(b.getX() == x && b.getY() == y) {
bubbles.remove(b);
this.score--;
return;
}
}
}
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);
}
}

View File

@@ -0,0 +1,103 @@
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) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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);
}
}

View File

@@ -0,0 +1,51 @@
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(color);
}
public static void testShape() {}
}

View File

@@ -0,0 +1,103 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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);
score--;
}
}
}
public void deleteBubblesAtPoint(int x, int y) {
for(IShape b: bubbles) {
if(b.getX() == x && b.getY() == y) {
bubbles.remove(b);
this.score--;
return;
}
}
}
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);
}
}

View File

@@ -0,0 +1,103 @@
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>();
}
// methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for (IShape b : bubbles) {
b.setX(b.getX() + dx);
b.setY(b.getY() + dy);
}
}
public void clearInvisibles(int w, int h) {
for (int i = bubbles.size() - 1; i >= 0; i--) {
IShape bubble = bubbles.get(i);
if (!bubble.isVisible(w, h)) {
bubbles.remove(i);
score--;
}
}
}
public void deleteBubblesAtPoint(int x, int y) {
for (int i = bubbles.size() - 1; i >= 0; i--) {
IShape b = bubbles.get(i);
if (b.getX() == x && b.getY() == 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);
}
}

View File

@@ -0,0 +1,29 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
new Bubble((int)(w * Math.random()), (int)(h * Math.random()));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.getY()+dy);
}
}
}

View File

@@ -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(dx);
bubble.setY(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);
}
}

View File

@@ -0,0 +1,39 @@
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ViewBubbles extends JPanel{
//instance variable
private Model model;
//constructor
public ViewBubbles(Model model) {
this.model = model;
this.setBackground(Color.WHITE);
int w = this.getWidth();
int h = this.getHeight();
//mouse listener
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
this.clickBubbles(e.getX(), e.getY());
}
});
Timer timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent e) {
this.moveUp(w, h);
}
});
timer.start();
}
//methods
}

View File

@@ -0,0 +1,64 @@
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ViewBubbles extends JPanel{
//instance variable
private Model model;
//constructor
public ViewBubbles(Model model) {
this.model = model;
this.setBackground(Color.WHITE);
int w = this.getWidth();
int h = this.getHeight();
//mouse listener
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
clickBubbles(e.getX(), e.getY());
}
});
Timer timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveUp(w, h);
}
});
timer.start();
}
//methods
private void moveUp(int w, int h) {
model.moveAll(0, -1);
int previousScore = model.getScore();
model.clearInvisibles(w, h);
model.addBubble(w, h);
int currentScore = model.getScore();
if (currentScore != previousScore) {
System.out.println("Current score: " + currentScore);
}
repaint();
}
private void clickBubbles(int x, int y) {
int previousScore = model.getScore();
model.deleteBubblesAtPoint(x, y);
int currentScore = model.getScore();
if (currentScore != previousScore) {
System.out.println("Current score: " + currentScore);
}
repaint();
}
protected void paintComponent(Graphics g) {
model.drawAll(g);
}
}

View File

@@ -0,0 +1,51 @@
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(color);
}
public static void testShape() {}
}

View File

@@ -0,0 +1,103 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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);
}
}

View File

@@ -0,0 +1,103 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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(IShape b: bubbles) {
if(b.getX() == x && b.getY() == y) {
bubbles.remove(b);
this.score++;
return;
}
}
}
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);
}
}

View File

@@ -0,0 +1,51 @@
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(color);
}
public static void testShape() {}
}

View File

@@ -0,0 +1,66 @@
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ViewBubbles extends JPanel{
//instance variable
private Model model;
//constructor
public ViewBubbles(Model model) {
this.model = model;
this.setBackground(Color.WHITE);
//timer
Timer timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveUp(getWidth(), getHeight());
}
});
timer.start();
//mouse listener
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
clickBubbles(e.getX(), e.getY());
}
});
}
//methods
private void moveUp(int w, int h) {
model.moveAll(0, -1);
int previousScore = model.getScore();
model.clearInvisibles(w, h);
model.addBubble(w, h);
int currentScore = model.getScore();
if (currentScore != previousScore) {
System.out.println("Current score: " + currentScore);
}
repaint();
}
private void clickBubbles(int x, int y) {
int previousScore = model.getScore();
model.deleteBubblesAtPoint(x, y);
int currentScore = model.getScore();
if (currentScore != previousScore) {
System.out.println("Current score: " + currentScore);
}
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
model.drawAll(g);
}
}

View File

@@ -0,0 +1,103 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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(IShape b: bubbles) {
if(b.getX() == x && b.getY() == y) {
bubbles.remove(b);
this.score++;
return;
}
}
}
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);
}
}

View File

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

View File

@@ -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.paintComponent(g);
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);
}
}

View File

@@ -0,0 +1,41 @@
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ViewBubbles extends JPanel{
//instance variable
private Model model;
//constructor
public ViewBubbles(Model model) {
this.model = model;
this.setBackground(Color.WHITE);
int w = this.getWidth();
int h = this.getHeight();
//mouse listener
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
clickBubbles(e.getX(), e.getY());
}
});
Timer timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveUp(w, h);
}
});
timer.start();
}
//methods
private moveUp(int x, int y) {
}
}

View File

@@ -0,0 +1,64 @@
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ViewBubbles extends JPanel{
//instance variable
private Model model;
//constructor
public ViewBubbles(Model model) {
this.model = model;
this.setBackground(Color.WHITE);
int w = this.getWidth();
int h = this.getHeight();
//mouse listener
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
clickBubbles(e.getX(), e.getY());
}
});
Timer timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveUp(w, h);
}
});
timer.start();
}
//methods
private void moveUp(int w, int h) {
model.moveAll(0, 1);
int previousScore = model.getScore();
model.clearInvisibles(w, h);
model.addBubble(w, h);
int currentScore = model.getScore();
if (currentScore != previousScore) {
System.out.println("Current score: " + currentScore);
}
repaint();
}
private void clickBubbles(int x, int y) {
int previousScore = model.getScore();
model.deleteBubblesAtPoint(x, y);
int currentScore = model.getScore();
if (currentScore != previousScore) {
System.out.println("Current score: " + currentScore);
}
repaint();
}
protected void paintComponent(Graphics g) {
model.drawAll(g);
}
}

View File

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

View File

@@ -0,0 +1,54 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
new Bubble((int)(w * Math.random()), (int)(h * Math.random()));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.getY()+dy);
}
}
public void clearInvisibles(int w, int h) {
for(IShape b: bubbles) {
if(!b.isVisible(w, h)) {
bubbles.remove(b);
this.score--;
}
}
}
public void deleteBubblesAtPoint(int x, int y) {
for(IShape b: bubbles) {
if(b.getX() == x && b.getY() == y) {
bubbles.remove(b);
this.score--;
}
}
}
public void drawAll(Graphics g) {
for(IShape b: bubbles) {
b.draw(g);
}
}
}

View File

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

View File

@@ -0,0 +1,17 @@
import java.awt.Color;
public class Shape {
//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());
}
}

View File

@@ -0,0 +1,37 @@
import java.awt.Graphics;
public class Bubble extends Shape {
//instance variable
private double radius = 10;
//constructor
public Bubble(int x, int y) {
super(x, y);
}
@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 cx = getX();
double cy = getY();
if(x > cx - 10 && x < cx + 10 && y > cy - 10 && y < cy + 10) {
return true;
}
return false;
}
@Override
public void draw(Graphics g) {
drawOval(getX(), getY(), 10, 10);
}
}

View File

@@ -0,0 +1,64 @@
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ViewBubbles extends JPanel{
//instance variable
private Model model;
//constructor
public ViewBubbles(Model model) {
this.model = model;
this.setBackground(Color.WHITE);
int w = this.getWidth();
int h = this.getHeight();
//mouse listener
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
clickBubbles(e.getX(), e.getY());
}
});
Timer timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveUp(w, h);
}
});
timer.start();
}
//methods
private void moveUp(int w, int h) {
model.moveAll(0, -1);
int previousScore = model.getScore();
model.clearInvisibles(w, h);
model.addBubble(w, h);
int currentScore = model.getScore();
if (currentScore != previousScore) {
System.out.println("Current score: " + currentScore);
}
repaint();
}
private void clickBubbles(int x, int y) {
int previousScore = model.getScore();
model.deleteBubblesAtPoint(x, y);
int currentScore = model.getScore();
if (currentScore != previousScore) {
System.out.println("Current score: " + currentScore);
}
repaint();
}
protected void paintComponent(Graphics g) {
model.drawAll(g);
}
}

View File

@@ -0,0 +1,47 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
new Bubble((int)(w * Math.random()), (int)(h * Math.random()));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.getY()+dy);
}
}
public void clearInvisibles(int w, int h) {
for(IShape b: bubbles) {
if(!b.isVisible(w, h)) {
bubbles.remove(b);
this.score--;
}
}
}
public void deleteBubblesAtPoint(int x, int y) {
for(IShape b: bubbles) {
if(b.getX() == x && b.getY() == y) {
bubbles.remove(b);
this.score--;
}
}
}
}

View File

@@ -0,0 +1,102 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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(IShape b: bubbles) {
if (b.isIn(x, y)) {
bubbles.remove(b);
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);
}
}

View File

@@ -0,0 +1,103 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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);
score--;
}
}
}
public void deleteBubblesAtPoint(int x, int y) {
for(IShape b: bubbles) {
if(b.getX() == x && b.getY() == y) {
bubbles.remove(b);
this.score--;
return;
}
}
}
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);
}
}

View File

@@ -0,0 +1,103 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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(IShape b: bubbles) {
if(b.getX() == x && b.getY() == y) {
bubbles.remove(b);
this.score++;
return;
}
}
}
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);
}
}

View File

@@ -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(color);
}
public static void testShape() {}
}

View File

@@ -0,0 +1,103 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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(IShape b: bubbles) {
if(b.getX() == x && b.getY() == y) {
bubbles.remove(b);
this.score--;
return;
}
}
}
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);
}
}

View File

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

View File

@@ -0,0 +1,159 @@
import java.awt.Color;
import java.awt.Graphics;
public class Bubble extends Shape {
//instance variable
private double radius = 10;
//constructor
public Bubble(int x, int y) {
super(x, y);
}
@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 centerX = getX();
double centerY = getY();
double distance = Math.sqrt((x - centerX) * (x - centerX) + (y - centerY) * (y - centerY));
return distance <= radius;
}
@Override
public void draw(Graphics g) {
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);
}
}

View File

@@ -0,0 +1,158 @@
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) {
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);
}
}

View File

@@ -0,0 +1,62 @@
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ViewBubbles extends JPanel{
//instance variable
private Model model;
//constructor
public ViewBubbles(Model model) {
this.model = model;
this.setBackground(Color.WHITE);
//mouse listener
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
clickBubbles(e.getX(), e.getY());
}
});
Timer timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveUp(getWidth(), getHeight());
}
});
timer.start();
}
//methods
private void moveUp(int w, int h) {
model.moveAll(0, -1);
int previousScore = model.getScore();
model.clearInvisibles(w, h);
model.addBubble(w, h);
int currentScore = model.getScore();
if (currentScore != previousScore) {
System.out.println("Current score: " + currentScore);
}
repaint();
}
private void clickBubbles(int x, int y) {
int previousScore = model.getScore();
model.deleteBubblesAtPoint(x, y);
int currentScore = model.getScore();
if (currentScore != previousScore) {
System.out.println("Current score: " + currentScore);
}
repaint();
}
protected void paintComponent(Graphics g) {
model.drawAll(g);
}
}

View File

@@ -0,0 +1,37 @@
import java.awt.Graphics;
public class Bubble extends Shape {
//instance variable
private double radius = 10;
//constructor
public Bubble(int x, int y) {
super(x, y);
}
@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 cx = getX();
double cy = getY();
if(x > cx - 10 && x < cx + 10 && y > cy - 10 && y < cy + 10) {
return true;
}
return false;
}
@Override
public void draw(Graphics g) {
drawOval(getX(), getY(), 10);
}
}

View File

@@ -0,0 +1,102 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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(IShape b: bubbles) {
if(b.getX() == x && b.getY() == y) {
bubbles.remove(b);
this.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());
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);
}
}

View File

@@ -0,0 +1,159 @@
import java.awt.Color;
import java.awt.Graphics;
public class Bubble extends Shape {
//instance variable
private double radius = 10;
//constructor
public Bubble(int x, int y) {
super(x, y);
}
@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 centerX = getX();
double centerY = getY();
double distance = Math.sqrt((x - centerX) * (x - centerX) + (y - centerY) * (y - centerY));
return distance <= radius;
}
@Override
public void draw(Graphics g) {
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);
}
}

View File

@@ -0,0 +1,59 @@
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ViewBubbles extends JPanel{
//instance variable
private Model model;
//constructor
public ViewBubbles(Model model) {
this.model = model;
this.setBackground(Color.WHITE);
int w = this.getWidth();
int h = this.getHeight();
//mouse listener
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
clickBubbles(e.getX(), e.getY());
}
});
Timer timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveUp(w, h);
}
});
timer.start();
}
//methods
private void moveUp(int w, int h) {
model.moveAll(0, -1);
int previousScore = model.getScore();
model.clearInvisibles(w, h);
model.addBubble(w, h);
int currentScore = model.getScore();
if (currentScore != previousScore) {
System.out.println("Current score: " + currentScore);
}
repaint();
}
private void clickBubbles(int x, int y) {
int previousScore = model.getScore();
model.deleteBubblesAtPoint(x, y);
int currentScore = model.getScore();
if (currentScore != previousScore) {
System.out.println("Current score: " + currentScore);
}
repaint();
}
}

View File

@@ -0,0 +1,103 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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);
score--;
}
}
}
public void deleteBubblesAtPoint(int x, int y) {
for(IShape b: bubbles) {
if(b.getX() == x && b.getY() == y) {
bubbles.remove(b);
this.score--;
return;
}
}
}
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);
}
}

View File

@@ -0,0 +1,159 @@
import java.awt.Color;
import java.awt.Graphics;
public class Bubble extends Shape {
//instance variable
private double radius = 10;
//constructor
public Bubble(int x, int y) {
super(x, y);
}
@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 centerX = getX();
double centerY = getY();
double distance = Math.sqrt((x - centerX) * (x - centerX) + (y - centerY) * (y - centerY));
return distance <= radius;
}
@Override
public void draw(Graphics g) {
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);
}
}

View File

@@ -0,0 +1,51 @@
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(color);
}
public static void testShape() {}
}

View File

@@ -0,0 +1,66 @@
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ViewBubbles extends JPanel{
//instance variable
private Model model;
//constructor
public ViewBubbles(Model model) {
this.model = model;
this.setBackground(Color.WHITE);
//timer
Timer timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveUp(getWidth(), getHeight());
}
});
timer.start();
//mouse listener
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
clickBubbles(e.getX(), e.getY());
}
});
}
//methods
private void moveUp(int w, int h) {
model.moveAll(0, -1);
int previousScore = model.getScore();
model.clearInvisibles(w, h);
model.addBubble(w, h);
int currentScore = model.getScore();
if (currentScore != previousScore) {
System.out.println("Current score: " + currentScore);
}
repaint();
}
private void clickBubbles(int x, int y) {
int previousScore = model.getScore();
model.deleteBubblesAtPoint(x, y);
int currentScore = model.getScore();
if (currentScore != previousScore) {
System.out.println("Current score: " + currentScore);
}
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
model.drawAll(g);
}
}

View File

@@ -0,0 +1,26 @@
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ViewBubbles extends JPanel{
//instance variable
private Model model;
//constructor
public ViewBubbles(Model model) {
this.model = model;
int w = this.getWidth();
int h = this.getHeight();
Timer timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent e) {
this.moveUp(w, h);
}
});
timer.start();
}
//methods
}

View File

@@ -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 centerX = getX();
double centerY = getY();
double distance = Math.sqrt((x - centerX) * (x - centerX) + (y - centerY) * (y - centerY));
return distance <= radius;
}
public void draw(Graphics g) {
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);
}
}

View File

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

View File

@@ -0,0 +1,102 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
new Bubble((int)(w * Math.random()), (int)(h * Math.random()));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.getY()+dy);
}
}
public void clearInvisibles(int w, int h) {
for (int i = bubbles.size() - 1; i >= 0; i--) {
IShape bubble = bubbles.get(i);
if (!bubble.isVisible(w, h)) {
bubbles.remove(i);
score--;
}
}
}
public void deleteBubblesAtPoint(int x, int y) {
for(IShape b: bubbles) {
if(b.getX() == x && b.getY() == y) {
bubbles.remove(b);
this.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);
}
}

View File

@@ -0,0 +1,103 @@
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>();
}
//methods
public int getScore() {
return this.score;
}
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int)(w * Math.random()), (int)(h * Math.random())));
}
public void moveAll(int dx, int dy) {
for(IShape b: bubbles) {
b.setX(b.getX()+dx);
b.setY(b.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);
score--;
}
}
}
public void deleteBubblesAtPoint(int x, int y) {
for (int i = bubbles.size() - 1; i >= 0; i--) {
IShape b = bubbles.get(i);
if (b.getX() == x && b.getY() == 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);
}
}

View File

@@ -0,0 +1,158 @@
import java.awt.Color;
import java.awt.Graphics;
public class Bubble extends Shape {
//instance variable
private double radius = 10;
//constructor
public Bubble(int x, int y) {
super(x, y);
}
@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 centerX = getX();
double centerY = getY();
double distance = Math.sqrt((x - centerX) * (x - centerX) + (y - centerY) * (y - centerY));
return distance <= radius;
}
public void draw(Graphics g) {
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);
}
}

Some files were not shown because too many files have changed in this diff Show More