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,195 @@
!SESSION 2023-05-20 16:36:36.249 -----------------------------------------------
eclipse.buildId=4.26.0.20221201-1200
java.version=17.0.6
java.vendor=Eclipse Adoptium
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=en_US
Framework arguments: -product org.eclipse.epp.package.jee.product -product org.eclipse.epp.package.jee.product
Command-line arguments: -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.jee.product -data E:\JAVA 2023\Assignment6_2230026071 -product org.eclipse.epp.package.jee.product
!ENTRY ch.qos.logback.classic 1 0 2023-05-20 16:36:37.323
!MESSAGE Activated before the state location was initialized. Retry after the state location is initialized.
!ENTRY ch.qos.logback.classic 1 0 2023-05-20 16:36:38.253
!MESSAGE Logback config file: E:\JAVA 2023\Assignment6_2230026071\.metadata\.plugins\org.eclipse.m2e.logback\logback.2.1.0.20221015-0744.xml
!ENTRY org.eclipse.jface 2 0 2023-05-20 16:36:40.023
!MESSAGE Keybinding conflicts occurred. They may interfere with normal accelerator operation.
!SUBENTRY 1 org.eclipse.jface 2 0 2023-05-20 16:36:40.023
!MESSAGE A conflict occurred for CTRL+SHIFT+T:
Binding(CTRL+SHIFT+T,
ParameterizedCommand(Command(org.eclipse.jdt.ui.navigate.open.type,Open Type,
Open a type in a Java editor,
Category(org.eclipse.ui.category.navigate,Navigate,null,true),
WorkbenchHandlerServiceHandler("org.eclipse.jdt.ui.navigate.open.type"),
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.ui.contexts.window,,,system)
Binding(CTRL+SHIFT+T,
ParameterizedCommand(Command(org.eclipse.lsp4e.symbolinworkspace,Go to Symbol in Workspace,
,
Category(org.eclipse.lsp4e.category,Language Servers,null,true),
WorkbenchHandlerServiceHandler("org.eclipse.lsp4e.symbolinworkspace"),
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.ui.contexts.window,,,system)
!SUBENTRY 1 org.eclipse.jface 2 0 2023-05-20 16:36:40.023
!MESSAGE A conflict occurred for ALT+SHIFT+R:
Binding(ALT+SHIFT+R,
ParameterizedCommand(Command(org.eclipse.jdt.ui.edit.text.java.rename.element,Rename - Refactoring ,
Rename the selected element,
Category(org.eclipse.jdt.ui.category.refactoring,Refactor - Java,Java Refactoring Actions,true),
WorkbenchHandlerServiceHandler("org.eclipse.jdt.ui.edit.text.java.rename.element"),
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.ui.contexts.window,,,system)
Binding(ALT+SHIFT+R,
ParameterizedCommand(Command(org.eclipse.ui.edit.rename,Rename,
Rename the selected item,
Category(org.eclipse.ui.category.file,File,null,true),
WorkbenchHandlerServiceHandler("org.eclipse.ui.edit.rename"),
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.ui.contexts.window,,,system)
!ENTRY org.eclipse.egit.ui 2 0 2023-05-20 16:36:43.796
!MESSAGE Warning: The environment variable HOME is not set. The following directory will be used to store the Git
user global configuration and to define the default location to store repositories: 'C:\Users\Danie'. If this is
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
!SESSION 2023-05-20 18:42:59.502 -----------------------------------------------
eclipse.buildId=4.26.0.20221201-1200
java.version=17.0.6
java.vendor=Eclipse Adoptium
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=en_US
Framework arguments: -product org.eclipse.epp.package.jee.product
Command-line arguments: -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.jee.product
!ENTRY ch.qos.logback.classic 1 0 2023-05-20 18:43:00.612
!MESSAGE Activated before the state location was initialized. Retry after the state location is initialized.
!ENTRY ch.qos.logback.classic 1 0 2023-05-20 18:43:01.732
!MESSAGE Logback config file: E:\JAVA 2023\Assignment6_2230026071\.metadata\.plugins\org.eclipse.m2e.logback\logback.2.1.0.20221015-0744.xml
!ENTRY org.eclipse.jface 2 0 2023-05-20 18:43:03.989
!MESSAGE Keybinding conflicts occurred. They may interfere with normal accelerator operation.
!SUBENTRY 1 org.eclipse.jface 2 0 2023-05-20 18:43:03.989
!MESSAGE A conflict occurred for CTRL+SHIFT+T:
Binding(CTRL+SHIFT+T,
ParameterizedCommand(Command(org.eclipse.jdt.ui.navigate.open.type,Open Type,
Open a type in a Java editor,
Category(org.eclipse.ui.category.navigate,Navigate,null,true),
WorkbenchHandlerServiceHandler("org.eclipse.jdt.ui.navigate.open.type"),
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.ui.contexts.window,,,system)
Binding(CTRL+SHIFT+T,
ParameterizedCommand(Command(org.eclipse.lsp4e.symbolinworkspace,Go to Symbol in Workspace,
,
Category(org.eclipse.lsp4e.category,Language Servers,null,true),
WorkbenchHandlerServiceHandler("org.eclipse.lsp4e.symbolinworkspace"),
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.ui.contexts.window,,,system)
!SUBENTRY 1 org.eclipse.jface 2 0 2023-05-20 18:43:03.990
!MESSAGE A conflict occurred for ALT+SHIFT+R:
Binding(ALT+SHIFT+R,
ParameterizedCommand(Command(org.eclipse.jdt.ui.edit.text.java.rename.element,Rename - Refactoring ,
Rename the selected element,
Category(org.eclipse.jdt.ui.category.refactoring,Refactor - Java,Java Refactoring Actions,true),
WorkbenchHandlerServiceHandler("org.eclipse.jdt.ui.edit.text.java.rename.element"),
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.ui.contexts.window,,,system)
Binding(ALT+SHIFT+R,
ParameterizedCommand(Command(org.eclipse.ui.edit.rename,Rename,
Rename the selected item,
Category(org.eclipse.ui.category.file,File,null,true),
WorkbenchHandlerServiceHandler("org.eclipse.ui.edit.rename"),
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.ui.contexts.window,,,system)
!ENTRY org.eclipse.egit.ui 2 0 2023-05-20 18:43:07.533
!MESSAGE Warning: The environment variable HOME is not set. The following directory will be used to store the Git
user global configuration and to define the default location to store repositories: 'C:\Users\Danie'. If this is
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
!SESSION 2023-05-22 15:22:56.906 -----------------------------------------------
eclipse.buildId=4.26.0.20221201-1200
java.version=17.0.6
java.vendor=Eclipse Adoptium
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=en_US
Framework arguments: -product org.eclipse.epp.package.jee.product
Command-line arguments: -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.jee.product
!ENTRY ch.qos.logback.classic 1 0 2023-05-22 15:22:58.273
!MESSAGE Activated before the state location was initialized. Retry after the state location is initialized.
!ENTRY ch.qos.logback.classic 1 0 2023-05-22 15:22:59.455
!MESSAGE Logback config file: E:\JAVA 2023\Assignment6_2230026071\.metadata\.plugins\org.eclipse.m2e.logback\logback.2.1.0.20221015-0744.xml
!ENTRY org.eclipse.jface 2 0 2023-05-22 15:23:01.820
!MESSAGE Keybinding conflicts occurred. They may interfere with normal accelerator operation.
!SUBENTRY 1 org.eclipse.jface 2 0 2023-05-22 15:23:01.820
!MESSAGE A conflict occurred for CTRL+SHIFT+T:
Binding(CTRL+SHIFT+T,
ParameterizedCommand(Command(org.eclipse.jdt.ui.navigate.open.type,Open Type,
Open a type in a Java editor,
Category(org.eclipse.ui.category.navigate,Navigate,null,true),
WorkbenchHandlerServiceHandler("org.eclipse.jdt.ui.navigate.open.type"),
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.ui.contexts.window,,,system)
Binding(CTRL+SHIFT+T,
ParameterizedCommand(Command(org.eclipse.lsp4e.symbolinworkspace,Go to Symbol in Workspace,
,
Category(org.eclipse.lsp4e.category,Language Servers,null,true),
WorkbenchHandlerServiceHandler("org.eclipse.lsp4e.symbolinworkspace"),
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.ui.contexts.window,,,system)
!SUBENTRY 1 org.eclipse.jface 2 0 2023-05-22 15:23:01.820
!MESSAGE A conflict occurred for ALT+SHIFT+R:
Binding(ALT+SHIFT+R,
ParameterizedCommand(Command(org.eclipse.jdt.ui.edit.text.java.rename.element,Rename - Refactoring ,
Rename the selected element,
Category(org.eclipse.jdt.ui.category.refactoring,Refactor - Java,Java Refactoring Actions,true),
WorkbenchHandlerServiceHandler("org.eclipse.jdt.ui.edit.text.java.rename.element"),
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.ui.contexts.window,,,system)
Binding(ALT+SHIFT+R,
ParameterizedCommand(Command(org.eclipse.ui.edit.rename,Rename,
Rename the selected item,
Category(org.eclipse.ui.category.file,File,null,true),
WorkbenchHandlerServiceHandler("org.eclipse.ui.edit.rename"),
,,true),null),
org.eclipse.ui.defaultAcceleratorConfiguration,
org.eclipse.ui.contexts.window,,,system)
!ENTRY org.eclipse.egit.ui 2 0 2023-05-22 15:23:04.755
!MESSAGE Warning: The environment variable HOME is not set. The following directory will be used to store the Git
user global configuration and to define the default location to store repositories: 'C:\Users\Danie'. If this is
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
!ENTRY org.eclipse.oomph.ui 4 0 2023-05-22 15:23:11.337
!MESSAGE java.lang.ExceptionInInitializerError
!STACK 0
java.lang.ExceptionInInitializerError
at org.eclipse.oomph.setup.ui.recorder.RecorderManager.<clinit>(RecorderManager.java:121)
at org.eclipse.oomph.setup.ui.SetupUIPlugin.performStartup(SetupUIPlugin.java:216)
at org.eclipse.oomph.setup.ui.EarlyStartup.earlyStartup(EarlyStartup.java:23)
at org.eclipse.oomph.internal.ui.EarlyStart.run(EarlyStart.java:119)
at org.eclipse.oomph.internal.ui.EarlyStart$1.run(EarlyStart.java:98)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: java.lang.IllegalStateException: BundleContext is no longer valid org.eclipse.oomph.p2.core_1.24.0.v20230214-0752 [652]
at org.eclipse.osgi.internal.framework.BundleContextImpl.checkValid(BundleContextImpl.java:1031)
at org.eclipse.osgi.internal.framework.BundleContextImpl.ungetService(BundleContextImpl.java:701)
at org.eclipse.oomph.p2.internal.core.AgentManagerImpl.getCurrentAgent(AgentManagerImpl.java:194)
at org.eclipse.oomph.p2.core.P2Util.getCurrentProvisioningAgent(P2Util.java:112)
at org.eclipse.oomph.setup.internal.core.util.SetupCoreUtil.<clinit>(SetupCoreUtil.java:157)
... 6 more

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