I am trying to make a graphics program that creates a GLabel called "Red" in a random position with a random color. This part I have done with the proper results. I also want, using the java.awt.event.* library, to have the GLabel change to the same color as its label when the mouse button is pressed.

The difficulty I am having is that I can assign the mousePressed event to either null or the GLabel, depending on where the mouse is clicked on the screen as a GObject. But, I cannot figure out how to attain the correct color from the GObject methods.

Could I make a GLabel constructor that would create an instance variable with the correct color, or am I stuck with the GLabel constructors that already exist in the acm.graphics library?

Ultimately this program will have 7 GLabels that will have random positions and random colors and when you mousePressed on any label, they will change to their correct color.

Here is my code so far. I've put a println() method to give me my mouse event data as well as object data.

Since GLabel and GObject are from a third party lib and not the standard Java API, only people that are familiar with that library will be able to help you.

August 31st, 2011, 03:03 AM

Mike_Chase

Re: GLabel methods with GObjects

I ended up skipping this project in "The Art and Science of Java" and went on to the next one and figured out what I was missing. I didn't realize I could use an IF statement to compare the references of the objects, such as IF (gobj = red) and change the colors that way. Anyway here is the new code that works for two colors. The books calls for 7 of them, but that is just an extension.

Code :

**Write a GraphicsProgram that creates GLabels for each of the color names RED,
ORANGE, YELLOW, GREEN, CYAN, BLUE, and MAGENTA, and then puts those labels up on
the screen in a random position and in a random color. It turns out to be difficult to
identify the color of such a label if the name says one thing, but its color is different.
Programmer: Mike Chase
Date: 8-23-2011
*
* Revision 1:
* -----------
* Modify the program you wrote in exercise 10 so that pressing the mouse button on
top of one of the GLabels temporarily resets its color to the one that matches its
name. Releasing the mouse button should choose a new random color for the label.
*/
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
import acm.util.*;
import java.awt.event.*;
public class FunnyColorNames extends GraphicsProgram{
public void run(){
double width = getWidth();
double height = getHeight();
red = new GLabel("Red");
red.setColor(rgen.nextColor());
red.setFont("Helvetica-24");
red.move(rgen.nextDouble(0,width - red.getWidth()), rgen.nextDouble(red.getAscent(), height + red.getAscent()));
add(red);
blue = new GLabel("Blue");
blue.setColor(rgen.nextColor());
blue.setFont("Helvetica-24");
blue.move(rgen.nextDouble(0,width - blue.getWidth()), rgen.nextDouble(blue.getAscent(), height - blue.getAscent()));
add(blue);
addMouseListeners();
}
// records mouse position to determine if null or GLabel is selected
public void mousePressed(MouseEvent e){
last = new GPoint(e.getPoint());
gobj = getElementAt(last);
println(last + " " + gobj);
if (gobj == red){
red.setColor(Color.RED);
} else if (gobj == blue){
blue.setColor(Color.BLUE);
}
}
public void mouseReleased(MouseEvent e){
if (gobj != null){
gobj.setColor(rgen.nextColor());
}
}
private RandomGenerator rgen = new RandomGenerator(); //creates random generated numbers and colors
private GObject gobj;
private GLabel red;
private GLabel blue;
private GPoint last;
}