Page 1 of 2 12 LastLast
Results 1 to 20 of 38
  1. #1
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default How to add MouseListener to animated applet?

    Java Code:
    this.applet.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                	int x = (int) e.getX();
            		int y = (int) e.getY();
            		int xp1 = x + 1;
            		int xm1 = x - 1;
            		int yp1 = y + 1;
            		int ym1 = y - 1;
            		try {
    					Robot r = new Robot();
    					int c = r.getPixelColor(x,y).getRGB();
    					int c1 = r.getPixelColor(xp1, y).getRGB();
    					int c2 = r.getPixelColor(xm1, y).getRGB();
    					int c3 = r.getPixelColor(yp1, x).getRGB();
    					int c4 = r.getPixelColor(ym1, x).getRGB();
    					JOptionPane.showMessageDialog(null, "copy and paste this into your script:\n new Color(" +
    							c + "), new Color(" + c1 + "), new Color(" + c2 + "), new Color(" + c3 + "),new Color(" +
    							c4 + ")");
    					System.out.println("new Color(" +
    							c + "), new Color(" + c1 + "), new Color(" + c2 + "), new Color(" + c3 + "),new Color(" +
    							c4 + ")");
    				} catch (AWTException e1) {
    					
    					e1.printStackTrace();
    				}
    				
            		
                }
    	});
    Above is the code to my program, and my question is:
    How can you add make this MouseListener work? When i do click on the applet, nothing happens. What am I doing wrong? (This applet is inside a GUI that loads an applet). Thanks!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    what object is this.applet?

    Override the other mouse listener methods and add println()s to see if any are called.

    A general comment:
    Use @override before all methods that are overriding adapter methods to have the compiler check it is correct.

  3. #3
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Okay, it is now called, but it only works once when the applet screen turns white for a split second. But when all the colors load (this is a game), it does not work.

  4. #4
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    here is the full source of Gui.java:
    Java Code:
    package com.yexibot.ui;
    
    
    
    import java.applet.Applet;
    import java.applet.AppletContext;
    import java.applet.AppletStub;
    import java.awt.AWTException;
    import java.awt.Color;
    import java.awt.Robot;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLClassLoader;
    import java.util.HashMap;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    import com.yexibot.applet.Crawler;
    import com.yexibot.init.App;
    
    
    public class Gui extends JFrame implements AppletStub  {
    
    	String base = "http://world" + App.world +
    			".runescape.com";
    	String jarName;
    	String className;
    
    	public static Applet applet;
    
    	Crawler crawler = new Crawler();
    
    	HashMap<String, String> parameters = crawler.getParameters();
    
    	static URLClassLoader classLoader;
    
    	public void init() {
    		
    		initApplet();
    		this.applet.addMouseListener(new MouseAdapter() {
    			@Override
                public void mouseClicked(MouseEvent e) {
    				System.out.println("Has been called");
                	int x = (int) e.getX();
            		int y = (int) e.getY();
            		int xp1 = x + 1;
            		int xm1 = x - 1;
            		int yp1 = y + 1;
            		int ym1 = y - 1;
            		try {
    					Robot r = new Robot();
    					int c = r.getPixelColor(x,y).getRGB();
    					int c1 = r.getPixelColor(xp1, y).getRGB();
    					int c2 = r.getPixelColor(xm1, y).getRGB();
    					int c3 = r.getPixelColor(yp1, x).getRGB();
    					int c4 = r.getPixelColor(ym1, x).getRGB();
    					JOptionPane.showMessageDialog(null, "copy and paste this into your script:\n new Color(" +
    							c + "), new Color(" + c1 + "), new Color(" + c2 + "), new Color(" + c3 + "),new Color(" +
    							c4 + ")");
    					System.out.println("new Color(" +
    							c + "), new Color(" + c1 + "), new Color(" + c2 + "), new Color(" + c3 + "),new Color(" +
    							c4 + ")");
    				} catch (AWTException e1) {
    					
    					e1.printStackTrace();
    				}
    				
            		
                }
    	});
    		this.add(applet);
    		
    
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(762, 500);
    		
    		setTitle("YexiBot - A revolutionizing bot");
    		setVisible(true);
    	
    	}
    
    	public static URLClassLoader getClassLoader() {
    		return classLoader;
    	}
    
    	public URL getDocumentBase() {
    		try {
    			return new URL(base);
    		} catch (MalformedURLException ex) {
    			Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
    			return null;
    		}
    	}
    
    	public URL getCodeBase() {
    		try {
    			return new URL(base);
    		} catch (MalformedURLException ex) {
    			Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
    			return null;
    		}
    	}
    
    	public String getParameter(String name) {
    		return parameters.get(name);
    	}
    
    	public AppletContext getAppletContext() {
    		return null;
    	}
    
    	public void appletResize(int width, int height) {
    		this.applet.setSize(width, height);
    	}
    
    	private void initApplet() {
    		
    		try {
    			URL[] appletURL = {new URL(base + "/" + jarName)};
    
    			classLoader = new URLClassLoader(appletURL);
    			Class loader = classLoader.loadClass(className);
    			applet = (Applet) loader.newInstance();
    			this.appletResize(600, 400);
    			
    			
    			applet.setStub(this);
    			applet.init();
    			applet.start();
    		} catch (InstantiationException ex) {
    			Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
    		} catch (IllegalAccessException ex) {
    			Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
    		} catch (ClassNotFoundException ex) {
    			Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
    		} catch (MalformedURLException ex) {
    			Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex);
    		}
    		
    		
    	}
    
    	public void setJar(String name) {
    		jarName = name;
    	}
    
    	public void setMainClass(String name) {
    		className = name;
    	}
    
    
    	
    }

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    The code does not compile. Missing packages:
    import com.yexibot.applet.Crawler;
    import com.yexibot.init.App;

  6. #6
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    The code does not compile. Missing packages:
    import com.yexibot.applet.Crawler;
    import com.yexibot.init.App;
    Yeah, that's because those are in my project. Do you want me to upload them?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    Are they needed? Can you post some small dummy versions that would allow compile and execution?

  8. #8
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Last edited by crikey; 08-09-2010 at 06:12 PM.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    I made my own. I added a simple main to the class, created a jar file with a simple applet. Compile and executed it and it seems to work fine when I click on the applets display. The console output:
    Running: D:\Java\jdk1.6.0_02\bin\java.exe -classpath D:\JavaDevelopment;. -Xmx512M GuiForApplet

    Has been called
    new Color(-1118482), new Color(-1118482), new Color(-1118482), new Color(-1118482),new Color(-1118482)
    Has been called
    new Color(-16777216), new Color(-16777216), new Color(-16777216), new Color(-1),new Color(-1)
    Has been called
    new Color(-12229988), new Color(-12229732), new Color(-12295524), new Color(-1118482),new Color(-1118482)

    0 error(s)
    My version of your code:
    Java Code:
    /*
    How can you add make this MouseListener work? When i do click on the applet, nothing happens. 
    What am I doing wrong? (This applet is inside a GUI that loads an applet). Thanks!
    
    */
    import java.applet.Applet;
    import java.applet.AppletContext;
    import java.applet.AppletStub;
    import java.awt.AWTException;
    import java.awt.Color;
    import java.awt.Robot;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLClassLoader;
    import java.util.HashMap;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    /*
    import com.yexibot.applet.Crawler;
    import com.yexibot.init.App;
    */
    
    // Dummy classes
    class Crawler {
    }
    class App {
    }
    
    public class GuiForApplet extends JFrame implements AppletStub  {
    
    	String base = "file:"; // = "http://world" + App.world + ".runescape.com";
    	String jarName;
    	String className;
    
    	public static Applet applet;
    
    	Crawler crawler = new Crawler();
    
    	HashMap<String, String> parameters; // = crawler.getParameters();
    
    	static URLClassLoader classLoader;
    
    	public void init() {
    		
    		initApplet();
    		this.applet.addMouseListener(new MouseAdapter() {
    			@Override
                public void mouseClicked(MouseEvent e) {
    				  System.out.println("Has been called");
                	int x = e.getX();
               		int y = e.getY();
               		int xp1 = x + 1;
               		int xm1 = x - 1;
               		int yp1 = y + 1;
               		int ym1 = y - 1;
               		try {
       					Robot r = new Robot();
       					int c = r.getPixelColor(x,y).getRGB();
       					int c1 = r.getPixelColor(xp1, y).getRGB();
       					int c2 = r.getPixelColor(xm1, y).getRGB();
       					int c3 = r.getPixelColor(yp1, x).getRGB();
       					int c4 = r.getPixelColor(ym1, x).getRGB();
       					JOptionPane.showMessageDialog(null, "copy and paste this into your script:\n new Color(" +
       							c + "), new Color(" + c1 + "), new Color(" + c2 + "), new Color(" + c3 + "),new Color(" +
       							c4 + ")");
       					System.out.println("new Color(" +
       							c + "), new Color(" + c1 + "), new Color(" + c2 + "), new Color(" + c3 + "),new Color(" +
       							c4 + ")");
       				} catch (AWTException e1) {
       					
       					e1.printStackTrace();
       				}
    				
            		
                }
    	   });
    		this.add(applet);
    		
    
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(762, 500);
    		
    		setTitle("YexiBot - A revolutionizing bot");
    		setVisible(true);
    	
    	}
    
    	public static URLClassLoader getClassLoader() {
    		return classLoader;
    	}
    
    	public URL getDocumentBase() {
    		try {
    			return new URL(base);
    		} catch (MalformedURLException ex) {
    			Logger.getLogger(GuiForApplet.class.getName()).log(Level.SEVERE, null, ex);
    			return null;
    		}
    	}
    
    	public URL getCodeBase() {
    		try {
    			return new URL(base);
    		} catch (MalformedURLException ex) {
    			Logger.getLogger(GuiForApplet.class.getName()).log(Level.SEVERE, null, ex);
    			return null;
    		}
    	}
    
    	public String getParameter(String name) {
    		return parameters.get(name);
    	}
    
    	public AppletContext getAppletContext() {
    		return null;
    	}
    
    	public void appletResize(int width, int height) {
    		this.applet.setSize(width, height);
    	}
    
       //------------------------------------------------------
    	private void initApplet() {
    		
    		try {
    			URL[] appletURL = {new URL(base + "/" + jarName)};
    
    			classLoader = new URLClassLoader(appletURL);
    			Class loader = classLoader.loadClass(className);
    			applet = (Applet) loader.newInstance();
    			this.appletResize(600, 400);
    			
    			applet.setStub(this);
    			applet.init();
    			applet.start();
    		} catch (InstantiationException ex) {
    			Logger.getLogger(GuiForApplet.class.getName()).log(Level.SEVERE, null, ex);
    		} catch (IllegalAccessException ex) {
    			Logger.getLogger(GuiForApplet.class.getName()).log(Level.SEVERE, null, ex);
    		} catch (ClassNotFoundException ex) {
    			Logger.getLogger(GuiForApplet.class.getName()).log(Level.SEVERE, null, ex);
    		} catch (MalformedURLException ex) {
    			Logger.getLogger(GuiForApplet.class.getName()).log(Level.SEVERE, null, ex);
    		}
    		
    		
    	}
    
    	public void setJar(String name) {
    		jarName = name;
    	}
    
    	public void setMainClass(String name) {
    		className = name;
    	}
    
       //--------------------------------------------------------
       public static void main(String[] args) {
         GuiForApplet gfa = new GuiForApplet();
         gfa.setJar("TestJar.jar");
         gfa.setMainClass("AppletTest2");
         gfa.init();  // start it up
       } // end main()
    	
    }

  10. #10
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Thanks for your reply -
    Okay, i used your code, but it does not load the correct applet. It needs to load the one from the url. And when it does, it will only click on the popup message if it was clicked on before fully loading (when the screen turns white for a split second). it only returns with:
    Has been called
    new Color(-1), new Color(-1), new Color(-1), new Color(-1),new Color(-1)
    Those are all rgb values which are all white.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    I don't know about the applet you are loading. Can that applet be the problem?
    Does it work with a simple applet from your PC?

  12. #12
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Try downloading the project source posted above, and see what happens. This applet is for a game named "Runescape"

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    Sorry, I'll leave that to you.

  14. #14
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Ah okay thanks anyways.
    But what could be the problem? This applet is animated, which might effect the mouseclick listener?

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    Don't know. Look at the Component class's methods Can you get any info on the applet's listeners?

  16. #16
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Java Code:
    {colourid=0, affid=0, modewhat=0, advert=1, frombilling=false, objecttag=0, java_arguments=-Xmx102m -Dsun.java2d.noddraw=true, js=1, settings=wwGlrZHF5gKN6D3mDdihco3oPeYN2KFybL9hUUFqOvk, cachesubdirid=0, lang=0, modewhere=0, demoaddress=demoworld1.runescape.com, demoid=901, lobbyaddress=lobby10.runescape.com, separate_jvm=true, sitesettings_member=0, crashurl=http://www.runescape.com/slu.ws?j=1&crash=1, userFlow=0, worldflags=117, havefirefox=0, game=0, cookiehost=.runescape.com, unsignedurl=http://www.runescape.com/slu.ws?j=1, country=0, haveie6=0, cookieprefix=, sskey=, worldid=18, cabbase=loader-472865629.cab, force64mb=false, lobbyid=1109}
    There are the parameter passed onto the applet. I noticed near the beginning that it set nodraw = true. Could this be why i cant click on it and get the pixel information when it has loaded?

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    Sorry, I don't recognize what is your last post.

  18. #18
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    So what should i do? Does anyone have any ideas?

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    Did you try to get a list of the applet's mouse listeners?
    What did you see?

    Another idea. Create a bufferedimage and get a graphics object on it and call the applet's paint method with that graphics object to capture all of the applet's GUI.

  20. #20
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    Yes, that would work, but this game updates constantly, and the objective of it is to get pixels, remember them, then click them. So that wouldn't work, because it takes too much time.
    And what do you mean by "list of applet mouse listeners"? Thanks!

Page 1 of 2 12 LastLast

Similar Threads

  1. Animation with Animated GIF
    By JavaBean in forum Java 2D
    Replies: 3
    Last Post: 06-04-2011, 04:26 PM
  2. Extracting jpg from animated gifs
    By Maz in forum Java 2D
    Replies: 3
    Last Post: 05-05-2011, 12:29 PM
  3. Help with Applet implementing mouselistener
    By dsym@comcast.net in forum Java Applets
    Replies: 3
    Last Post: 05-14-2009, 12:57 AM
  4. Playing an Animated GIF
    By c0m4ndo45 in forum Java 2D
    Replies: 3
    Last Post: 04-24-2009, 10:47 AM
  5. Animated JPanels + Moving Pages
    By PetalumaBoy in forum New To Java
    Replies: 0
    Last Post: 02-16-2009, 11:09 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
  •