Solved

Calling from a Class

Posted on 2007-11-29
14
179 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
micro services spring boot application error 3 148
stackato and cloud 4 74
jar file executable 12 43
servlet example issue 6 32
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

867 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

12 Experts available now in Live!

Get 1:1 Help Now