Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2009
    Posts
    4
    Rep Power
    0

    Default Query on using MouseAdapter

    I have a basic program like this :

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    
    public class Rings extends Applet
                          implements MouseListener {
    
       private int x = 100, y = 100;  // Coordinates of the mouse click
    
       public void init() {
          addMouseListener(this);  ----------------------------------------------> <1>
    }
    
       public void paint(Graphics g) {
          int count = 0;
          while (count < 100) {
             int radius = 5*count;
             int diameter = 2*radius;
             g.drawOval(x-radius, y-radius, diameter, diameter);
             count = count+1;
          }
       }
    
       public void mouseClicked(MouseEvent e) {
         x = e.getX();
         y = e.getY();
         repaint();
       }
    
       public void mouseExited(MouseEvent e) { }
    
       public void mouseEntered(MouseEvent e) { }
    
       public void mousePressed(MouseEvent e) { }
    
       public void mouseReleased(MouseEvent e) { }
    }

    I'd like to know exactly what happens at Program-Point <1>.
    Also, I've tried to use MouseAdapter for the same instead of MouseListener, but somehow couldn't succeed. Can someone help in that regard as well?

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    804
    Rep Power
    6

    Default

    "MouseListener" is an interface and "MouseAdapter" is an abstract class.

    If we implement "MouseListener", then we need to define all the mouse handler methods whether we are using or not using.

    In order to overcome this problem Sun introduced Adapter which is abstract in turn implements "MouseListener".Using this "MouseAdapter" class we can use whatever methods we need orginally that we can implement. This abstract class defines null methods for them all, so you can only have to define methods for events you care about.

    Go thru thism link for details
    MouseAdapter (Java 2 Platform SE v1.4.2)
    Last edited by RamyaSivakanth; 06-26-2009 at 03:34 PM.
    Ramya:cool:

  3. #3
    Join Date
    Jun 2009
    Posts
    4
    Rep Power
    0

    Default

    ^ I do know what a MouseAdapter is..

    I just wanted to see how it's used in this particular program. If someone could type out the same program using MouseAdapter I'd be more than happy.

    And, I need the <exact> explanation of Program-Point <1> as well. ;)

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    804
    Rep Power
    6

    Default

    Hi,
    U cant extends more than one class in Java.You have to go for anonymous inner class .Go thru the code changes in Bold and gothru tutorial regarding anonymous inner class

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.Applet;
    
    public class Rings extends Applet
     {
       private int x = 100, y = 100;  // Coordinates of the mouse click
    
       public void init() {
    [B]
    	               this.addMouseListener (new MouseAdapter () {
    		   public void mouseClicked(MouseEvent e) {
    		   x = e.getX();
    		   y = e.getY();
                                 repaint();
    		  }//mouseClicked
    
                              });[/B]
    
    	}
    
       public void paint(Graphics g) {
          int count = 0;
          while (count < 100) {
             int radius = 5*count;
             int diameter = 2*radius;
             g.drawOval(x-radius, y-radius, diameter, diameter);
             count = count+1;
          }
       }
    
     	
    
      [B] //No need to define all these below because of we are using adapter	
      // public void mouseExited(MouseEvent e) { }
    
      // public void mouseEntered(MouseEvent e) { }
    
      // public void mousePressed(MouseEvent e) { }
    
      // public void mouseReleased(MouseEvent e) { }[/B]
    }
    Last edited by RamyaSivakanth; 06-26-2009 at 04:55 PM.
    Ramya:cool:

  5. #5
    specert is offline Member
    Join Date
    Jun 2009
    Posts
    2
    Rep Power
    0

    Default

    GOOD very good
    I study now but java is very hard

  6. #6
    Join Date
    Jun 2009
    Posts
    4
    Rep Power
    0

    Default

    ^ Thanks a lot for the good work, Ramya.

    One question still remains..
    In the code
    this.addMouseListener (new MouseAdapter () {
    The 'this' here refers to which object?

  7. #7
    Join Date
    Jun 2009
    Posts
    4
    Rep Power
    0

    Default

    Bounce.
    ^ Can someone answer my query ?

  8. #8
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    'this' here refers to which object?
    The enclosing class/object.
    In a non-static context you can find out with
    Java Code:
    System.out.println("this = " + this.getClass().getSimpleName());

  9. #9
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    what happens at Program-Point <1>.
    Adds the specified mouse listener to receive mouse events from this component

    'this' should be your applet

Similar Threads

  1. Sql Query
    By Nomad in forum JDBC
    Replies: 16
    Last Post: 02-20-2009, 01:58 PM
  2. I need help with a query.
    By Daredemo in forum JDBC
    Replies: 1
    Last Post: 08-13-2008, 05:16 AM
  3. Help in Query
    By geeta_ravikanti in forum JDBC
    Replies: 0
    Last Post: 03-31-2008, 01:16 PM
  4. Using sql:query tag
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-15-2008, 03:13 PM
  5. Using sql:query tag
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-14-2008, 09:31 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
  •