0

I have a rectangular GObject I want to follow my mouse across a screen. It is currently leaving a black line if moved across since the program does not remove any of the previous rectangles. The code is as follows

// 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();
        
    }
}

Where am I going wrong? The issue is with the mousemoved and init classes as far as I can tell.

1 Answer 1

0

I figured it out. Mouselisteners must be added to the mouseMoved method, and another method must be created for the mouselistener itself.

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

I looked at https://github.com/NatashaTheRobot/Stanford-CS-106A since I was completely stumped, and learned the new setLocation method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.