Results 1 to 8 of 8

Thread: Events in java,

  1. #1
    soulchyld is offline Newbies
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default Events in java,

    Hi, Im trying to develop an applet, that displays images and then swaps the images when the mouse goes over the image, I have one problem and that is adding the mouseListener, I have got it to work but it swaps the image when the mouse enters the applet and not the image, can anyone tell me where I have gone wrong with my code?
    as you can probably tell Im a newbie to programming in general,
    Java Code:
    import java.awt.*; 
    import java.applet.*; 
    // These classes are for Url's. 
    import java.net.*; 
    import java.awt.event.*;
    
    public class ImageExample extends Applet 
    implements MouseListener
    { 
     // names of my images 
         Image forward;	 
    	 Image reverse;
    	 Image left;
    	 Image right;
    	 Image forward2;
    	 
    	 Boolean forwardActive;
    	 
    	 	 
    
     // The applet base URL used to retrieve the images over the web
         URL base; 
    
     // This object will allow you to control loading 
         MediaTracker mt; 
    
         public void init()  
         { 
    	 
    	 
    	 
      // initialize the MediaTracker 
              mt = new MediaTracker(this);   
    
             try {     
                   base = getDocumentBase(); 
    			 } 
                 catch (Exception e) {} 
    
      // Here I am loading the image.   
                                 forward = getImage(base,"up.jpg"); 
    		  forward2 = getImage(base,"up2.jpg");
    		  reverse = getImage(base,"down.jpg");
    		  left = getImage(base,"left.jpg");
    		  right = getImage(base,"right.jpg");
    		  addMouseListener(this);
    
      // tell the MediaTracker to kep an eye on this image, and give it ID 1; 
              mt.addImage(forward,1);
    		  mt.addImage(forward2,5);
    		  mt.addImage(reverse,2);
    		  mt.addImage(left,3);
    		  mt.addImage(right,4);  
    
             try { 
                   mt.waitForAll(); 
                 } 
                 catch (InterruptedException  e) {}   
         } 
    
         public void paint(Graphics g)  
         { 
      // now we are going to draw the images on the screen 
      // (image name,x,y,observer); 
              g.drawImage(forward,100,20,this); 
    		  g.drawImage(reverse,100,200,this);
    		  g.drawImage(left,30,95,this);
    		  g.drawImage(right,205,95,this); 
    		  
    		  if (forwardActive)g.drawImage(forward2,100,20,this);
    		  else g.drawImage(forward,100,20,this);
         } 
    	 
    		public void mouseClicked (MouseEvent me) {} 
    		public void mouseEntered (MouseEvent me) {forwardActive = true; repaint();} 
    		public void mousePressed (MouseEvent me) {} 
    		public void mouseReleased (MouseEvent me) {}  
    		public void mouseExited (MouseEvent me) {forwardActive = false; repaint();}  
    	 
    	 	 
    
    }
    Thanks for all your help!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Don't use Applet, but rather use a JApplet. Create ImageIcons with your images, use a JLabel to hold the ImageIcons and add the MouseListener to the JLabel.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You could display the image on a Label and add the MouseListener to the Label and not the Applet.
    Java Code:
    catch (Exception e) {}
    Also, never never do this. At the very least make a call to printStackTrace so you can see if an excetpion occurred or not. I have seen many people post code like this and complain that their program does nothing. Reason is that an exception occurred but the program ignores it and continues on as if everything is OK.

  4. #4
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I have not used applet before, but as what I can see is you add mouselistener to Applet. If you can put Images into JPanel then add the mouselistener to it.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mine0926 View Post
    I have not used applet before, but as what I can see is you add mouselistener to Applet. If you can put Images into JPanel then add the mouselistener to it.
    It's probably not a good idea for the original poster to mix AWT (Applet) with Swing (the JPanel). If he decides to go to Swing, I don't see how using a JPanel would have an advantage over my suggestion to use a JLabel and simply swap ImageIcons.

  6. #6
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Oh! OK. Have not tried applet before, Thanks for correcting me.

  7. #7
    soulchyld is offline Newbies
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    Hi again, I have tried to use the JLabel with no luck, I think my code is correct but its not displaying anything , it only displays the label with text (when I modify the code), if I try to display the label with Icon nothing shows
    Java Code:
    import java.applet.*;
    import java.awt.*;
    import javax.swing.*;
    
    public class jLabels extends Applet
    {
    	
    
    	
    	public void init()
    	{
    	setLayout(new FlowLayout());
    	JLabel label1 = new JLabel("Java");
    	add(label1);
    	ImageIcon fwd = new ImageIcon("up.jpg");
    	JLabel forward = new JLabel(fwd);
    	add(forward);	
    	}
    }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The most common cause of a problem in this situation is that you're not looking for the image in the correct location. Since you're using an applet and will likely create a Jar file with the image in the jar file, I'd use resources, not files to get the image, and I'd make sure that the applet is located where I'm looking for it. Something like:

    Java Code:
       private static final String IMAGE_RESOURCE = "myImages/image.jpg";
    
       public void foo() throws IOException {
          BufferedImage image = ImageIO.read(getClass().getResourceAsStream(IMAGE_RESOURCE));
          ImageIcon icon = new ImageIcon(image);
          JLabel label = new JLabel(icon);
       }

Similar Threads

  1. Question on Java Graphics, KeyBoard Events and JFrame
    By loopsnhoops in forum New To Java
    Replies: 1
    Last Post: 02-10-2011, 04:12 AM
  2. How to trap java process termination events?
    By thalupularavi in forum SWT / JFace
    Replies: 0
    Last Post: 03-10-2010, 04:02 PM
  3. typed events vs untyped events.
    By Drun in forum SWT / JFace
    Replies: 0
    Last Post: 11-23-2009, 01:22 PM
  4. Need Help with events
    By Gatts79 in forum AWT / Swing
    Replies: 3
    Last Post: 09-23-2008, 04:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •