mousePressed
Hi. I am trying to create an applet, where if the user clicks between a pixel range, it opens up a web browser. Unfortunately, I do not know what package to import, and where to extend my applet from.
import ...
public class ... extends MouseListener, (and what?)
{
...
}
Besides, what is ActionListener and ItemListener?
Best regards,
Ben.
[392 byte] By [
pentacle1] at [2007-11-11 7:38:55]

# 1 Re: mousePressed
you do not extend the class. You implement it.
public class SomeClass implements MouseListener
{
/...
}
MouseListener, MouseMotionListener, ActionListener, etc. are all interfaces. They have certain methods you must add into your class when you implement them.
This is probably something along the lines of what you want to do.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class SomeClass extends Applet implements MouseListener
{
public void init() {
/** add a MouseListener to the component */
addMouseListener(this);
}
public boolean inCoordinates(Point p) {
// check to see if p is in the coordinates.. change the ints to what you want
return (p.x > 100) && (p.y > 100) && (p.x < 200) && (p.y < 200);
}
public void openBrowser() {
// open the browser
}
/** the following methods are required by the interface */
/**
* invoked when the mouse button has
* been clicked (pressed and released)
* on a component
*/
public void mouseClicked(MouseEvent me) {
if (inCoordinates(me.getPoint())) {
openBrowser();
}
}
/**
* invoked when the mouse enters a component
*/
public void mouseEntered(MouseEvent me) {
}
/**
* invoked when the mouse exits a component
*/
public void mouseExited(MouseEvent me) {
}
/**
* invoked when a mouse button has been
* pressed on a component
*/
public void mousePressed(MouseEvent me) {
}
/**
* invoked when a mouse button has been
* released on a component
*/
public void mouseReleased(MouseEvent me) {
}
}
destin at 2007-11-11 22:37:45 >

# 2 Re: mousePressed
yes, up to that, i am clear.
but doesn't the applet need some kind of java.net.*?
(i thought the openBrowser() function would need to have that?)
Ben
# 6 Re: mousePressed
So this applet, running within a browser window, is going to open another browser window? Well, you're right: implement the java.applet.AppletContext.showDoucment( url ) method ... which requires the import of java.net.URL
http://www.croftsoft.com/library/tutorials/browser/
nspils at 2007-11-11 22:42:52 >
