mousePressed


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: mousePressed

  1. #1
    Join Date
    Jan 2006
    Posts
    15

    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.

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    you do not extend the class. You implement it.

    Code:
    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.

    Code:
    
    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) {
        }
    }
    Last edited by destin; 01-03-2006 at 08:54 PM.

  3. #3
    Join Date
    Jan 2006
    Posts
    15
    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

  4. #4
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    yeah you'll probably have to import java.net.*;

  5. #5
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    No, an applet does not, per se, need to import java.net. The web page which "contains" the applet must have the <applet> tags set.

  6. #6
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    To open up a web browser you don't need java.net?

  7. #7
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center