Write a Model class with the following UML specification: +----------------------------------------------+ | Model | +----------------------------------------------+ | - score: int | | - bubbles: ArrayList<IShape> | +----------------------------------------------+ | + Model() | | + getScore(): int | | + addBubble(int w, int h): void | | + moveAll(int dx, int dy): void | | + clearInvisibles(int w, int h): void | | + deleteBubblesAtPoint(int x, int y): void | | + drawAll(Graphics g): void | | + testModel(): void | +----------------------------------------------+ When a new model object is created, the score must be zero and the arraylist must be empty. The getScore method returns as result the current score for the game. The addBubble method adds a new bubble to the arraylist of bubbles. The position of the center of the new bubble is random but must be inside a window of width w and height h (the arguments of the addBubble method), like this: new Bubble((int)(w * Math.random()), (int)(h * Math.random())) The moveAll method moves the positions of all the bubbles in the arraylist of bubbles by the amount dx in the x direction and by the amount dy in the y direction. The clearInvisibles method takes as argument the width w and the height h of the window, and deletes from the arraylist of bubbles any bubble which is not visible in the window anymore. For each bubble which is deleted, the score decreases by 1.The deleteBubblesAtPoint method takes as argument the coordinates (x, y) of a point, and deletes from the arraylist of bubbles any bubble which contains this point (multiple bubbles might contain the point, because bubbles can overlap in the window). For each bubble which is deleted, the score increases by 1. The drawAll method draws all the bubbles in the arraylist of bub
时间: 2023-06-16 09:03:53 浏览: 100
深入理解Java基础之try-with-resource语法糖
les using the Graphics object g. Finally, the testModel method is a debugging method which creates a new Model object, adds five bubbles, moves them, clears the ones outside the window, deletes the ones at a certain point, and then prints the score.
Here's a possible implementation of the Model class:
import java.awt.Graphics;
import java.util.ArrayList;
public class Model {
private int score;
private ArrayList<IShape> bubbles;
public Model() {
score = 0;
bubbles = new ArrayList<IShape>();
}
public int getScore() {
return 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.move(dx, dy);
}
}
public void clearInvisibles(int w, int h) {
ArrayList<IShape> toRemove = new ArrayList<IShape>();
for (IShape bubble : bubbles) {
if (!bubble.isVisible(w, h)) {
toRemove.add(bubble);
score--;
}
}
bubbles.removeAll(toRemove);
}
public void deleteBubblesAtPoint(int x, int y) {
ArrayList<IShape> toRemove = new ArrayList<IShape>();
for (IShape bubble : bubbles) {
if (bubble.contains(x, y)) {
toRemove.add(bubble);
score++;
}
}
bubbles.removeAll(toRemove);
}
public void drawAll(Graphics g) {
for (IShape bubble : bubbles) {
bubble.draw(g);
}
}
public void testModel() {
Model model = new Model();
for (int i = 0; i < 5; i++) {
model.addBubble(800, 600);
}
model.moveAll(100, 50);
model.clearInvisibles(800, 600);
model.deleteBubblesAtPoint(400, 300);
System.out.println("Score: " + model.getScore());
}
}
Note that this implementation assumes the existence of classes Bubble and IShape, which represent a single bubble and a generic shape, respectively. These classes should implement the methods used by the Model class, such as move, isVisible, contains, and draw.
阅读全文