Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: GLabel methods with GObjects

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Location
    Louisiana
    Posts
    8
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default GLabel methods with GObjects

    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.
    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(0,height - red.getAscent()));
     
    		add(red);
     
    		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);
     
     
    	}
     
     
    private RandomGenerator rgen = new RandomGenerator(); //creates random generated numbers and colors
     
    private GObject gobj;
    private GLabel red;
    private GPoint last;
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: GLabel methods with GObjects

    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.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Location
    Louisiana
    Posts
    8
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.
    **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;
    }

Similar Threads

  1. Overriding Methods
    By tcstcs in forum Java Theory & Questions
    Replies: 5
    Last Post: June 2nd, 2011, 04:23 AM
  2. How do get and set methods work??
    By merdzins in forum Object Oriented Programming
    Replies: 2
    Last Post: December 25th, 2010, 03:17 AM
  3. I need help with METHODS!!!
    By Slone in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 14th, 2010, 08:33 AM
  4. methods help
    By hockey87 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 9th, 2010, 11:57 PM
  5. Re-using methods?
    By Morevan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 26th, 2010, 05:04 PM