Results 1 to 8 of 8
Thread: Events in java,
- 05-23-2011, 01:58 AM #1
Newbies
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
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,
Thanks for all your help!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();} }
-
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.
- 05-23-2011, 02:46 AM #3
You could display the image on a Label and add the MouseListener to the Label and not the Applet.
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.Java Code:catch (Exception e) {}
- 05-23-2011, 02:49 AM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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.
-
- 05-23-2011, 03:21 AM #6
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Oh! OK. Have not tried applet before, Thanks for correcting me.
- 05-24-2011, 12:51 AM #7
Newbies
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
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); } }
-
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
-
Question on Java Graphics, KeyBoard Events and JFrame
By loopsnhoops in forum New To JavaReplies: 1Last Post: 02-10-2011, 03:12 AM -
How to trap java process termination events?
By thalupularavi in forum SWT / JFaceReplies: 0Last Post: 03-10-2010, 03:02 PM -
typed events vs untyped events.
By Drun in forum SWT / JFaceReplies: 0Last Post: 11-23-2009, 12:22 PM -
Need Help with events
By Gatts79 in forum AWT / SwingReplies: 3Last Post: 09-23-2008, 03:18 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks