I have 2 versions of applets that are doing exactly the same thing: if the user clicks on a certain part of the applet, it draws a red circle at that point, up to 10 circles.
The first applet (Spots) is using Deprecated methods from java 1.02 (Component.mouseDown()).
The other (AdapterSpots) is using Component.addMouseListener(new MouseAdapter(){//...} ).
What is the difference between them? and why is the second one is better than the first, if it is ? Thanks for ur help Samer
public class Spots extends java.applet.Applet { final int MAXSPOTS = 10; int xspots[] = new int[MAXSPOTS]; int yspots[] = new int[MAXSPOTS]; int currspots = 0;
public void init() { setBackground(Color.white); }
public boolean mouseDown(Event evt, int x, int y) { if (currspots < MAXSPOTS) { addspot(x,y); return true; } else { System.out.println("Too many spots."); return false; } }
public class AdapterSpots extends Applet { Point clickPoint[] = new Point[10]; int curpt = 0; static final int RADIUS = 7; static final int MAXPT = 10;
public void init() { addMouseListener(new MyMouseAdapter()); }
The listener is a more general way of solving this sort of problem. You will see in the API that listeners are used now quite commonly. Once you become accustomed to using it, it serves you well in many other places. That is why the other method is phased out in favor of a listener. Leaving both methods in would just muddy matters about which should be used.
By the way, your second applet is also better because it uses an array of Points instead of two arrays of int. It would be even better if it were a list of Spots.
Thanks for your answer. What's happening really is that i'm trying to teach my friend java programming and i reached to that point. what should do should teach her both methods or just the lestners???????????
Just the listeners! Since the others are deprecated, they won't be used anymore.
If, by chance, she ends up working on a legacy system, it will be easy to pick up the old way later. Anyway, that way is not difficult to understand, it just gets sloppy with accretion, whereas the listener technique can stay more consistent across different implementations.