Results 1 to 9 of 9
Thread: Query on using MouseAdapter
- 06-26-2009, 02:44 PM #1
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?
- 06-26-2009, 03:30 PM #2
"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:
- 06-26-2009, 04:41 PM #3
^ 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. ;)
- 06-26-2009, 04:50 PM #4
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:
- 06-27-2009, 09:11 AM #5
Member
- Join Date
- Jun 2009
- Posts
- 2
- Rep Power
- 0
GOOD very good
I study now but java is very hard
- 06-30-2009, 01:12 PM #6
^ Thanks a lot for the good work, Ramya.
One question still remains..
In the code
The 'this' here refers to which object?this.addMouseListener (new MouseAdapter () {
- 07-01-2009, 05:22 PM #7
Bounce.
^ Can someone answer my query ?
- 07-01-2009, 06:18 PM #8
'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());
- 07-02-2009, 08:04 AM #9
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
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
-
Sql Query
By Nomad in forum JDBCReplies: 16Last Post: 02-20-2009, 01:58 PM -
I need help with a query.
By Daredemo in forum JDBCReplies: 1Last Post: 08-13-2008, 05:16 AM -
Help in Query
By geeta_ravikanti in forum JDBCReplies: 0Last Post: 03-31-2008, 01:16 PM -
Using sql:query tag
By Java Tip in forum Java TipReplies: 0Last Post: 01-15-2008, 03:13 PM -
Using sql:query tag
By Java Tip in forum Java TipReplies: 0Last Post: 01-14-2008, 09:31 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks