Solved

Calling from a Class

Posted on 2007-11-29
14
178 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
Comment Utility
MouseEvents e = new MouseEvents();
e.init();
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
and to add it as a mouse listrener


addMouseMotionLiustener(e);
0
 

Author Comment

by:gameness
Comment Utility
Where in the public class should I add the code?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
depends on what u want to do :)
0
 

Author Comment

by:gameness
Comment Utility
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
Comment Utility
why is it an applet then, it has some display stuff as well. Whats that for?
0
 

Author Comment

by:gameness
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
How?  I'm not very good at this...  
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
using the addMouseMotionListener call i posted earlier
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> It loads a graphical applet on top of it.  

It doesn't actually.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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
Comment Utility
you should just need to add your mouse listener to the existing applet

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now