[英]Issues with moving a GObject in Java
我有一個矩形 GObject 我想在屏幕上跟隨鼠標。 如果移動它,它當前會留下一條黑線,因為程序不會刪除任何先前的矩形。 代碼如下
// TODO: comment this program
import acm.graphics.*; // GOval, GRect, etc.
import acm.program.*; // GraphicsProgram
import acm.util.*; // RandomGenerator
import java.awt.*; // Color
import java.awt.event.*; // MouseEvent
public class Breakout extends BreakoutProgram {
public void run() {
// Set the canvas size. In your code, remember to ALWAYS use getWidth()
// and getHeight() to get the screen dimensions, not these constants!
setCanvasSize(CANVAS_WIDTH, CANVAS_HEIGHT);
// TODO: finish this program
brickBuilder();
}
//This method creates the bricks in the program. It will also set the color for the bricks.
//Input: The constants for brick columns,brick width,brick separation, brick y offset, brick height,brick columns and brick rows
//Output: The brick display
private void brickBuilder() {
int i=0;//i will count columns
int j=-1;//j will count rows, set to -1 or the color display will break as well as y-coordinate
while(j<(NBRICK_ROWS-1)) {
j++;
i-=i;//i must reset or while loop will only execute once for columns
double x_coordinate = getWidth()/2-(NBRICK_COLUMNS*BRICK_WIDTH+((NBRICK_COLUMNS-1)*BRICK_SEP))/2;
double y_coordinate = BRICK_Y_OFFSET;
y_coordinate +=j*(BRICK_HEIGHT+BRICK_SEP);
while(i<NBRICK_COLUMNS) {
i++;
x_coordinate +=(BRICK_WIDTH+BRICK_SEP);
GRect brickWall = new GRect(x_coordinate,y_coordinate,BRICK_WIDTH,BRICK_HEIGHT);
brickWall.setFilled(true);
switch((j+1)%10) {//switch will set the color, uses remainder to decide color of brick
case 1:
brickWall.setFillColor(Color.RED);
break;
case 2:
brickWall.setFillColor(Color.RED);
break;
case 3:
brickWall.setFillColor(Color.ORANGE);
break;
case 4:
brickWall.setFillColor(Color.ORANGE);
break;
case 5:
brickWall.setFillColor(Color.YELLOW);
break;
case 6:
brickWall.setFillColor(Color.YELLOW);
break;
case 7:
brickWall.setFillColor(Color.GREEN);
break;
case 8:
brickWall.setFillColor(Color.GREEN);
break;
case 9:
brickWall.setFillColor(Color.CYAN);
break;
case 0:
brickWall.setFillColor(Color.CYAN);
break;
}
add(brickWall);
}
}
}
public void mouseMoved(MouseEvent e) {
GRect paddle = new GRect(e.getX(), getHeight()-PADDLE_Y_OFFSET, PADDLE_WIDTH, PADDLE_HEIGHT);
paddle.setFilled(true);
add(paddle);
}
public void init() {
addMouseListeners();
}
}
我哪里錯了? 據我所知,問題在於 mousemoved 和 init 類。
我想到了。 必須將鼠標監聽器添加到 mouseMoved 方法中,並且必須為鼠標監聽器本身創建另一個方法。
// TODO: comment this program
import acm.graphics.*; // GOval, GRect, etc.
import acm.program.*; // GraphicsProgram
import acm.util.*; // RandomGenerator
import java.awt.*; // Color
import java.awt.event.*; // MouseEvent
public class Breakout extends BreakoutProgram {
private GRect paddle;
public void run() {
// Set the window's title bar text
setTitle("CS 106A Breakout");
// Set the canvas size. In your code, remember to ALWAYS use getWidth()
// and getHeight() to get the screen dimensions, not these constants!
setCanvasSize(CANVAS_WIDTH, CANVAS_HEIGHT);
// TODO: finish this program
brickBuilder();
buildPaddle();
}
//This method creates the bricks in the program. It will also set the color for the bricks.
//Input: The constants for brick columns,brick width,brick separation, brick y offset, brick height,brick columns and brick rows
//Output: The brick display
private void brickBuilder() {
int i=0;//i will count columns
int j=-1;//j will count rows, set to -1 or the color display will break as well as y-coordinate
while(j<(NBRICK_ROWS-1)) {
j++;
i-=i;//i must reset or while loop will only execute once for columns
double x_coordinate = getWidth()/2-(NBRICK_COLUMNS*BRICK_WIDTH+((NBRICK_COLUMNS-1)*BRICK_SEP))/2;
double y_coordinate = BRICK_Y_OFFSET;
y_coordinate +=j*(BRICK_HEIGHT+BRICK_SEP);
while(i<NBRICK_COLUMNS) {
i++;
x_coordinate +=(BRICK_WIDTH+BRICK_SEP);
GRect brickWall = new GRect(x_coordinate,y_coordinate,BRICK_WIDTH,BRICK_HEIGHT);
brickWall.setFilled(true);
switch((j+1)%10) {//switch will set the color, uses remainder to decide color of brick
case 1:
brickWall.setFillColor(Color.RED);
break;
case 2:
brickWall.setFillColor(Color.RED);
break;
case 3:
brickWall.setFillColor(Color.ORANGE);
break;
case 4:
brickWall.setFillColor(Color.ORANGE);
break;
case 5:
brickWall.setFillColor(Color.YELLOW);
break;
case 6:
brickWall.setFillColor(Color.YELLOW);
break;
case 7:
brickWall.setFillColor(Color.GREEN);
break;
case 8:
brickWall.setFillColor(Color.GREEN);
break;
case 9:
brickWall.setFillColor(Color.CYAN);
break;
case 0:
brickWall.setFillColor(Color.CYAN);
break;
}
add(brickWall);
}
}
}
private void buildPaddle() {
paddle = new GRect(0, getHeight()-PADDLE_Y_OFFSET, PADDLE_WIDTH, PADDLE_HEIGHT);
paddle.setFilled(true);
add(paddle);
addMouseListeners();
}
public void mouseMoved(MouseEvent e) {
paddle.setLocation(e.getX(), getHeight()-PADDLE_Y_OFFSET);
}
}
我看了https://github.com/NatashaTheRobot/Stanford-CS-106A ,因為我完全被難住了,並學習了新的 setLocation 方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.