Solved

Calling from a Class

Posted on 2007-11-29
14
180 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Arrays.asList  VS  ArrayList 4 60
java stored proc example 9 32
How to convert String matching to regex in java 4 33
Opening PDF on button click and fill new document 2 39
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

809 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