帮我看看这段代码有什么问题:public void drawAll() { for (IShape bubble : bubbles) { Graphics Graphics ; bubble.draw(Graphics); } }
时间: 2023-06-26 11:10:19 浏览: 135
这段代码有两个问题:
1. 在方法中声明的 `Graphics` 变量未被初始化,因此不能在 `bubble.draw(Graphics);` 中使用。应该在方法外部初始化 `Graphics`,并将其传递给 `drawAll()` 方法。
2. 在 `Graphics Graphics ;` 中,变量名和类型名重复了,应该使用不同的变量名。
以下是修正后的代码:
```
public void drawAll(Graphics g) {
for (IShape bubble : bubbles) {
bubble.draw(g);
}
}
```
相关问题
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
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.
帮我写出以下java代码: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. WARNING: when you use the remove method of Java’s ArrayList class to remove an element of an arraylist at index i, the arraylist immediately shifts down by one position all the elements with higher indexes to make the arraylist one element shorter. So, for example, when removing the element at index i, the element at index i+1 immediately moves to the position at index i, the element at index i+2 immediately moves to the position at index i+1, etc. This means that on the next iteration of the loop, when i has become i+1, the element that you will be testing at index i+1 is in fact the element that used to be at index i+2. Which means that the element that used to be at index i+1 (and which is now at index i) will never be tested! Therefore, when removing elements from an arraylist, if your loop starts at index 0 and goes up the indexes in the arraylist, then your loop will fail to test some elements! CONCLUSION: when removing elements from an arraylist, your loop must start from the END of the arraylist and go DOWN to index 0. 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 bubbles. Make sure you test as many methods of the Model class as poss
public class Model {
private ArrayList<Bubble> bubbles;
private int score;
public void clearInvisibles(int w, int h) {
for (int i = bubbles.size() - 1; i >= 0; i--) {
Bubble bubble = bubbles.get(i);
if (!bubble.isVisible(w, h)) {
bubbles.remove(i);
score--;
}
}
}
public void deleteBubblesAtPoint(int x, int y) {
Iterator<Bubble> iterator = bubbles.iterator();
while (iterator.hasNext()) {
Bubble bubble = iterator.next();
if (bubble.containsPoint(x, y)) {
iterator.remove();
score++;
}
}
}
public void drawAll() {
for (Bubble bubble : bubbles) {
bubble.draw();
}
}
}
class Bubble {
private int x;
private int y;
private int radius;
public boolean isVisible(int w, int h) {
return (x + radius >= 0 && x - radius <= w && y + radius >= 0 && y - radius <= h);
}
public boolean containsPoint(int x, int y) {
return (Math.pow((this.x - x), 2) + Math.pow((this.y - y), 2) <= Math.pow(radius, 2));
}
public void draw() {
// code to draw bubble
}
}
阅读全文
相关推荐
















