Solved

Calling from a Class

Posted on 2007-11-29
14
182 Views
Last Modified: 2010-03-30
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

0
Comment
Question by:gameness
  • 9
  • 5
14 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 20378216
MouseEvents e = new MouseEvents();
e.init();
0
 
LVL 92

Expert Comment

by:objects
ID: 20378223
and to add it as a mouse listrener


addMouseMotionLiustener(e);
0
 

Author Comment

by:gameness
ID: 20378257
Where in the public class should I add the code?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 20378371
depends on what u want to do :)
0
 

Author Comment

by:gameness
ID: 20378487
I want the MouseEvents class to run from the time the main applet loads, to the time the window is closed :)
0
 
LVL 92

Expert Comment

by:objects
ID: 20378537
why is it an applet then, it has some display stuff as well. Whats that for?
0
 

Author Comment

by:gameness
ID: 20378567
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
 
LVL 92

Expert Comment

by:objects
ID: 20378614
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
 

Author Comment

by:gameness
ID: 20378621
How?  I'm not very good at this...  
0
 
LVL 92

Expert Comment

by:objects
ID: 20378647
using the addMouseMotionListener call i posted earlier
0
 
LVL 92

Expert Comment

by:objects
ID: 20378679
> It loads a graphical applet on top of it.  

It doesn't actually.
0
 
LVL 92

Expert Comment

by:objects
ID: 20378866
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
 

Author Comment

by:gameness
ID: 20382509
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
 
LVL 92

Expert Comment

by:objects
ID: 20386693
you should just need to add your mouse listener to the existing applet

Applet applet = getAppletContext().getApplet(nameOfOtherApplet);
applet.addMouseMotionListener(myListener);
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question