Results 1 to 5 of 5
  1. #1
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default MousePressed event help needed(CS106A )

    Hi frens,
    I have finished doing exercise 10 of chapter 8 of the book "Art and Science of java"(CS106A stanford).

    Exercise 10 was:
    XML 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.
    Now Iam stuck on exercise 11 which says:
    XML Code:
    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.
    The part I am stuck on is on Mousepressed method:

    Java Code:
     public void mousePressed(MouseEvent e){
    	
    		
    		 last = new GPoint(e.getPoint());
    		 gobj = getElementAt(last);
           		 gobj.setColor(Color.RED);		
    		 
    	 add(gobj);}
    What happens right now is all the labels (red,blue,green etc) turn red when I press mouse on them.

    I have tried putting "if" control statement but there is a bug,It makes no difference.

    Java Code:
    public void mousePressed(MouseEvent e){
    	
    		
    		 last = new GPoint(e.getPoint());
    		 gobj = getElementAt(last);
                    if (gobj==red){
           		 gobj.setColor(Color.RED);		
    		 
    	 add(gobj);} }

    Any idea what am I missing? Can we equal gobj which is a GObject and red which is a GLable.

    My full code is below:

    Java Code:
    /*File:presscolor.java
     * This program displays matching colour to the glabels when pressed but when 
     * released it shows random colours.
     * */
     import acm.graphics.*;
    import acm.util.*;
    import java.awt.color.*;
    import java.awt.Color;
    import java.awt.Font;
    import acm.program.*;
    import java.awt.*;
    import java.awt.event.*;
    
     public class presscolor extends GraphicsProgram{
    	 
    	 
    	 public void run(){
    		 
    		 
    		 GLabel red = new GLabel ("RED");
    		 red.setFont(new Font("Serif", Font.BOLD, 18));
    		 red.setColor(rgen.nextColor());
    		 GLabel orange = new GLabel ("ORANGE");
    		 orange.setFont(new Font("Serif", Font.BOLD, 18));
    		 orange.setColor(rgen.nextColor());
    		 GLabel yellow = new GLabel ("YELLOW");
    		 yellow.setFont(new Font("Serif", Font.BOLD, 18));
    		 yellow.setColor(rgen.nextColor());
    		 GLabel green = new GLabel ("GREEN");
    		 green.setFont(new Font("Serif", Font.BOLD, 18));
    		 green.setColor(rgen.nextColor());
    		 GLabel cyan = new GLabel ("CYAN");
    		 cyan.setFont(new Font("Serif", Font.BOLD, 18));
    		 cyan.setColor(rgen.nextColor());
    		 GLabel blue = new GLabel ("BLUE");
    		 blue.setFont(new Font("Serif", Font.BOLD, 18));
    		 blue.setColor(rgen.nextColor());
    		 GLabel magenta = new GLabel ("MAGENTA");
    		 magenta.setFont(new Font("Serif", Font.BOLD, 18));
    		 magenta.setColor(rgen.nextColor());
    		 
    		 double x = rgen.nextInt(500,900);
    		 double y = rgen.nextInt(400,500);
    		 double a = rgen.nextInt(50,900);
    		 double b = rgen.nextInt(50,500);
    		 double c = rgen.nextInt(20,900);
    		 double d = rgen.nextInt(10,500);
    		 double e = rgen.nextInt(90,900);
    		 double f = rgen.nextInt(0,500);
    		 double g = rgen.nextInt(2,900);
    		 double h = rgen.nextInt(3,500);
    		 double i = rgen.nextInt(40,900);
    		 double j = rgen.nextInt(100,500);
    		 double k = rgen.nextInt(20,900);
    		 double l = rgen.nextInt(200,500);
    		 
    		 
    	     add(red,x,y);
    	     add(yellow,a,b);
    		 add(green,c,d);
    		 add(cyan,e,f);
    		 add(magenta,g,h);
    		 add(orange,i,j);
    		 add(blue,k,l);
    		 addMouseListeners();
    		 
    		 
    	 }
    	 
    	 public void mousePressed(MouseEvent e){
    	
    		
    		 last = new GPoint(e.getPoint());
    		 gobj = getElementAt(last);
           
    		 
    		
    			
    		 gobj.setColor(Color.RED);
    		
    		 
    	 add(gobj);}
    	 
    	 
    		 
    	 
    	 private GPoint last;
            private GObject gobj;
    	 private GLabel red,yellow,cyan,green,magenta,orange,blue;
    	 private RandomGenerator rgen = new RandomGenerator();
     }

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    Since you are adding the same Mouse Listener on all the Labels, you'll need to hold a reference to the Color Labels you are adding the MouseListener to to compare which one threw the MouseEvent. You can either increase the scope of the Labels, or better yet, add separate MouseListeners to each Label so that each Label has its own MouseListener.

    Java Code:
         red.addMouseListener(new MouseListener(){
              //MouseListener code...
         });
         blue.addMouseListener(new MouseListener(){
              //MouseListener code...
         });

  3. #3
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    It works okay for the first label which is red with this following code:
    Java Code:
    /*File:presscolor.java
     * This program displays matching colour to the glabels when pressed but when 
     * released it shows random colours.
     * */
     import acm.graphics.*;
    import acm.util.*;
    import java.awt.color.*;
    import java.awt.Color;
    import java.awt.Font;
    import acm.program.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
     public class presscolor extends GraphicsProgram{
    	 
    	 
    	 public void run(){
    		 
    		 
    		 GLabel red = new GLabel ("RED");
    		 red.setFont(new Font("Serif", Font.BOLD, 18));
    		 red.setColor(rgen.nextColor());
    		 red.addMouseListener(this);
    
    		 GLabel orange = new GLabel ("ORANGE");
    		 orange.setFont(new Font("Serif", Font.BOLD, 18));
    		 orange.setColor(rgen.nextColor());
    		 
    		 
    		 GLabel yellow = new GLabel ("YELLOW");
    		 yellow.setFont(new Font("Serif", Font.BOLD, 18));
    		 yellow.setColor(rgen.nextColor());
    		 GLabel green = new GLabel ("GREEN");
    		 green.setFont(new Font("Serif", Font.BOLD, 18));
    		 green.setColor(rgen.nextColor());
    		 GLabel cyan = new GLabel ("CYAN");
    		 cyan.setFont(new Font("Serif", Font.BOLD, 18));
    		 cyan.setColor(rgen.nextColor());
    		 GLabel blue = new GLabel ("BLUE");
    		 blue.setFont(new Font("Serif", Font.BOLD, 18));
    		 blue.setColor(rgen.nextColor());
    		 GLabel magenta = new GLabel ("MAGENTA");
    		 magenta.setFont(new Font("Serif", Font.BOLD, 18));
    		 magenta.setColor(rgen.nextColor());
    		 
    		 double x = rgen.nextInt(500,900);
    		 double y = rgen.nextInt(400,500);
    		 double a = rgen.nextInt(50,900);
    		 double b = rgen.nextInt(50,500);
    		 double c = rgen.nextInt(20,900);
    		 double d = rgen.nextInt(10,500);
    		 double e = rgen.nextInt(90,900);
    		 double f = rgen.nextInt(0,500);
    		 double g = rgen.nextInt(2,900);
    		 double h = rgen.nextInt(3,500);
    		 double i = rgen.nextInt(40,900);
    		 double j = rgen.nextInt(100,500);
    		 double k = rgen.nextInt(20,900);
    		 double l = rgen.nextInt(200,500);
    		 
    		 
    	     add(red,x,y);
    	     add(yellow,a,b);
    		 add(green,c,d);
    		 add(cyan,e,f);
    		 add(magenta,g,h);
    		 add(orange,i,j);
    		 add(blue,k,l);
    		
    		 
    	 }
    	 
    	 public void mousePressed(MouseEvent e){
    	
    		
    		 last = new GPoint(e.getPoint());
    		 gobj = getElementAt(last);
           
    		 
    		
    			
    		 gobj.setColor(Color.RED);
    		
    		 
    	 add(gobj);}
    	 
    	 
    	 public void mousePressed1(MouseEvent e){
    			
    			
    		 last = new GPoint(e.getPoint());
    		 gobj = getElementAt(last);
           
    		 	
    			
    		 gobj.setColor(Color.ORANGE);
    		
    		 
    	 add(gobj);}	 
    	 
    	 private GPoint last;
         private GObject gobj,Nike;
    	 private GLabel red,yellow,cyan,green,magenta,orange,blue;
    	 private RandomGenerator rgen = new RandomGenerator();
     }
    But doesn't work with other labels. When I put this
    Java Code:
    red.addMouseListener(new MouseListener(){
              //MouseListener code...
         });
    there is a red under line below MouseListener of (new MouseListener(){

    Java Code:
     red.addMouseListener(new MouseListener(){
              //MouseListener code...
         });
         blue.addMouseListener(new MouseListener(){
              //MouseListener code...
         });
    are these all mouse listeners gonna trigger the same method mousePressed:
    Java Code:
    public void mousePressed(MouseEvent e){
    	
    		
    		 last = new GPoint(e.getPoint());
    		 gobj = getElementAt(last);
           
    		 
    		
    			
    		 gobj.setColor(Color.RED);
    		
    		 
    	 add(gobj);}

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    All those MouseListeners will need to implement their own methods.
    MouseListener (Java Platform SE 6)

    You'll wind up with something like this
    Java Code:
    		red.addMouseListener(new MouseListener(){
    			public void mouseClicked(MouseEvent e) {}
    			public void mouseEntered(MouseEvent e) {}
    			public void mouseExited(MouseEvent e) {}
    			public void mousePressed(MouseEvent e) {
    				((GMLabel)e.getSource()).setColor(Color.RED);
    			}
    			public void mouseReleased(MouseEvent e) {
    				((GMLabel)e.getSource()).setColor(presscolor.this.getRandomColor());
    			}			
    		});
    ... with each GMLabel's MouseListener handling its own logic.

  5. #5
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

Similar Threads

  1. Java Checkerboad question CS106A Stanford univerity
    By ccie007 in forum New To Java
    Replies: 71
    Last Post: 09-20-2013, 04:02 AM
  2. CS106A Stanford University
    By Learning Java in forum New To Java
    Replies: 116
    Last Post: 07-09-2011, 04:43 PM
  3. BigInteger Rational (CS106A-Stanford university)
    By ccie007 in forum New To Java
    Replies: 16
    Last Post: 10-01-2010, 07:16 PM
  4. Class exercise CS106A (Stanford university)
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 09-11-2010, 01:47 AM
  5. How to run java class in ECLIPSE(CS106A Stanford)
    By ccie007 in forum New To Java
    Replies: 17
    Last Post: 09-11-2010, 12:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •