The Artima Developer Community
Sponsored Link

Java Answers Forum
Deprecated methods

4 replies on 1 page. Most recent reply: Mar 6, 2002 11:56 PM by Samer I Awad

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Samer I Awad

Posts: 6
Nickname: samer
Registered: Mar, 2002

Deprecated methods Posted: Mar 4, 2002 1:11 PM
Reply to this message Reply
Advertisement
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

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Event;

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;
}
}

void addspot(int x,int y) {
xspots[currspots] = x;
yspots[currspots] = y;
currspots++;
repaint();
}

public void paint(Graphics g) {
g.setColor(Color.blue);
for (int i = 0; i < currspots; i++) {
g.fillOval(xspots - 10, yspots - 10, 20, 20);
}
}
}

//*********************************************
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

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());
}

public void paint(Graphics g) {
g.setColor(Color.blue);
for(int i=0 ; i<curpt ; i++)
g.fillOval(clickPoint.x-RADIUS,clickPoint.y-RADIUS,
RADIUS*2, RADIUS*2);
}

public void update(Graphics g){paint(g);}

/* inner class that extends MouseAdapter */
class MyMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent e) {
if(curpt<MAXPT){
clickPoint[curpt] = e.getPoint();
curpt++;
repaint();
}
}
}
}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Deprecated methods Posted: Mar 4, 2002 1:40 PM
Reply to this message Reply
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.

Samer I Awad

Posts: 6
Nickname: samer
Registered: Mar, 2002

Re: Deprecated methods Posted: Mar 6, 2002 11:18 AM
Reply to this message Reply
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???????????

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Deprecated methods Posted: Mar 6, 2002 1:48 PM
Reply to this message Reply
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.

Samer I Awad

Posts: 6
Nickname: samer
Registered: Mar, 2002

Re: Deprecated methods Posted: Mar 6, 2002 11:56 PM
Reply to this message Reply
thank you very much, you are the best :)

Flat View: This topic has 4 replies on 1 page
Topic: Creating an EXE File by using Sun Java Previous Topic   Next Topic Topic: Excel Spread Sheet data access

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use