Calling from a Class

Ok, this should be an easy one:

In the attached code, I have a class called "MouseEvents".  How do I call from it in the main public class (called "MultiApplet")?  
import java.applet.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.net.MalformedURLException;
 
public class MultiApplet extends Applet {
  public Vector applets = new Vector();
 
  public void addApplet(Applet applet) {
    // Create a new, "artificial" stub for the subapplet
    AppletStub stub = new MultiAppletStub(this);
 
    // Give it to the subapplet to use
    applet.setStub(stub);
 
    // Start up the subapplet
    applet.init();
 
    // Finally, store it in the list
    applets.addElement(applet);
  }
 
  public Applet addApplet(String className) 
      throws ClassNotFoundException, IllegalAccessException,
             InstantiationException {
 
    // Create an instance of the named applet
    Class clas = Class.forName(className);
    Applet applet = (Applet)clas.newInstance();
 
    // Add it to the system
    addApplet(applet);
 
    // Return it
    return applet;
  }
 
  public void start() {
    for (Enumeration e=applets.elements(); e.hasMoreElements();) {
      ((Applet)e.nextElement()).start();
    }
  }
 
  public void stop() {
    for (Enumeration e=applets.elements(); e.hasMoreElements();) {
      ((Applet)e.nextElement()).stop();
    }
  }
 
  public void destroy() {
    for (Enumeration e=applets.elements(); e.hasMoreElements();) {
      ((Applet)e.nextElement()).destroy();
    }
  }
 
  public void appletResize(int width, int height) {
    System.out.println( "ach" );
  }
}
 
class MultiAppletStub implements AppletStub {
  private Applet applet;
 
  public MultiAppletStub(Applet applet) {
    this.applet = applet;
  }
 
  public boolean isActive() {
    return applet.isActive();
  }
 
  public URL getDocumentBase() {
    return applet.getDocumentBase();
  }
 
  public URL getCodeBase() {
	return applet.getCodeBase();
  }
 
  public String getParameter(String name) {
    return applet.getParameter( name );
  }
 
  public AppletContext getAppletContext() {
	return applet.getAppletContext();
  }
 
  public void appletResize(int width, int height) {
  }
}
 
class MouseEvents extends Applet
    implements MouseMotionListener, ActionListener {
    Timer timer;
    int mouseX = 0, mouseY = 0;
    private int count;
 
    public void init() {
        addMouseMotionListener(this);
 
        timer = new Timer(1000, this);
        timer.start();
        count = 10;
    }
 
    public void mouseDragged(MouseEvent me) {
        mouseX = me.getX();
        mouseY = me.getY();
        repaint();
    }
 
    public void mouseMoved(MouseEvent me) {
        count = 10;
        timer.restart();
        repaint();
    }
 
    public void paint(Graphics g) {
		g.drawString(String.valueOf(count), mouseX, mouseY);
    }
 
    public void actionPerformed(ActionEvent e) {
        count = Math.max(0, --count);
		showStatus("Timer: " + String.valueOf(count));
		
		if (count == 0) try {
				getAppletContext().showDocument(new URL("http://www.google.com"));
			}
			catch (MalformedURLException e1) {
				// Ignore
			}
        repaint();
    }
 
    private class Timer extends Thread {
        private final int interval;
        private final ActionListener listener;
        private volatile boolean run = true;
 
        public Timer(int interval, ActionListener listener) {
            this.interval = interval;
            this.listener = listener;
        }
 
        public void restart() {
            interrupt();
        }
 
        public void stopTimer() {
            run = false;
            interrupt();
        }
 
        public void run() {
            while(run) {
                try {
                    Thread.sleep(interval);
                    listener.actionPerformed(new ActionEvent(this, 0, null));
                } catch (InterruptedException e) {
                    interrupted();
                }
            }
        }
    }
}

Open in new window

gamenessAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
objectsConnect With a Mentor Commented:
MouseEvents e = new MouseEvents();
e.init();
0
 
objectsCommented:
and to add it as a mouse listrener


addMouseMotionLiustener(e);
0
 
gamenessAuthor Commented:
Where in the public class should I add the code?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
objectsCommented:
depends on what u want to do :)
0
 
gamenessAuthor Commented:
I want the MouseEvents class to run from the time the main applet loads, to the time the window is closed :)
0
 
objectsCommented:
why is it an applet then, it has some display stuff as well. Whats that for?
0
 
gamenessAuthor Commented:
It loads a graphical applet on top of it.  What I'm testing is whether or not I can use the mouse listener while manipulating the graphical applet.  
0
 
objectsCommented:
you can, but it doesn't need to be an applet to achieve that. you just add you listener to the component you want to listen for mouse events on.
0
 
gamenessAuthor Commented:
How?  I'm not very good at this...  
0
 
objectsCommented:
using the addMouseMotionListener call i posted earlier
0
 
objectsCommented:
> It loads a graphical applet on top of it.  

It doesn't actually.
0
 
objectsCommented:
if all you want to do is add a MouseMotionListener then you just need to call addMouseMotionListener() method on the component you want to listen to, passing it your motion listener.
0
 
gamenessAuthor Commented:
Here's exactly what I want to do...

I want to load a simple applet...  but I want to make sure that the user is actively using it (i.e. by moving the mouse).  I have a timer that counts down from X seconds, and resets each time the mouse is moved.  If the mouse hasn't moved in X seconds, the user is redirected to a timeout page.  

I have all the code to do it...  but putting it together is the problem LOL.  I just started messing with Java last weekend, so I'm absolutely clueless.  I'm getting all my answers from here!
0
 
objectsCommented:
you should just need to add your mouse listener to the existing applet

Applet applet = getAppletContext().getApplet(nameOfOtherApplet);
applet.addMouseMotionListener(myListener);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.