Question: I can't figure out why I keep getting a compiler error for add(rect); line in code below. import acm.graphics.GRect; import acm.program.GraphicsProgram; import java.awt.Color; import java.awt.event.MouseEvent;

I can't figure out why I keep getting a compiler error for add(rect); line in code below.

import acm.graphics.GRect;

import acm.program.GraphicsProgram;

import java.awt.Color;

import java.awt.event.MouseEvent;

import acm.graphics.*;

import acm.program.*;

import java.awt.*;

import java.awt.event.*;

public class stampit extends GraphicsProgram {

public static void main(String[] args) {

new stampit().start();

}

/**

*

* @param me

*/

@Override

public void mousePressed(MouseEvent me) {

int mouseX = me.getX();

int mouseY = me.getY();

final int EXAMPLE_WIDTH = 50;

final int EXAMPLE_HEIGHT = 30;

GRect rect = new GRect(mouseX, mouseY, EXAMPLE_WIDTH, EXAMPLE_HEIGHT);

// if you would like to fill your shape(s) with color, here is an

// example:

rect.setFillColor(Color.PINK);

rect.setFilled(true);

}

// this adds the GRect to the app window.

add(rect)

@Override

public void run() {

double labelX = 0;

double labelY = 0;

final String TOP_MESSAGE = "Clicky-Click it!";

// This line is needed so that your mousePressed() method will run.

addMouseListeners();

// You do not need to change any of the code below. However, read through

// it to see how the centering was achieved.

GLabel topLabel = new GLabel(TOP_MESSAGE);

topLabel.setFont("Times-25");

// put it right at the top. The y coordinate of a GLabel is the

// "baseline", or the bottom of the text. So if you set its y to 0

// the text will be off the top of the window. Here we get the height

// of the GLabel and set the y to that, so all of it is visible.

// 0,0 is the top left corner of the app window.

labelY = topLabel.getHeight();

// here we center the label by subtracting the difference between the

// app width and the label width, and then dividing the space evenly

// on each side.

labelX = (this.getWidth() - topLabel.getWidth()) / 2;

// now we actually set the location of the label using the calculated coords.

topLabel.setLocation(labelX, labelY);

add(topLabel);

// This line is needed so that your mousePressed() method will run.

addMouseListeners();

// Draw a line down the middle and across the middle for testing purposes.

GLine midLine = new GLine(this.getWidth() / 2, 0, this.getWidth() / 2, this.getHeight());

add(midLine);

midLine = new GLine(0, this.getHeight() / 2, this.getWidth(), this.getHeight() / 2);

add(midLine);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!