Results 1 to 20 of 38
- 08-09-2010, 05:13 PM #1
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
How to add MouseListener to animated applet?
Above is the code to my program, and my question is: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(); } } });
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!
- 08-09-2010, 05:38 PM #2
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.
- 08-09-2010, 05:51 PM #3
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
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.
- 08-09-2010, 05:53 PM #4
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
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; } }
- 08-09-2010, 06:01 PM #5
The code does not compile. Missing packages:
import com.yexibot.applet.Crawler;
import com.yexibot.init.App;
- 08-09-2010, 06:02 PM #6
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
- 08-09-2010, 06:04 PM #7
Are they needed? Can you post some small dummy versions that would allow compile and execution?
- 08-09-2010, 06:08 PM #8
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
http://www.2shared.com/file/QbJ64YpU/intellibot.html
source projectLast edited by crikey; 08-09-2010 at 06:12 PM.
- 08-09-2010, 06:22 PM #9
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:
My version of your code: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)
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() }
- 08-09-2010, 06:34 PM #10
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
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.
- 08-09-2010, 06:37 PM #11
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?
- 08-09-2010, 06:39 PM #12
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
Try downloading the project source posted above, and see what happens. This applet is for a game named "Runescape"
- 08-09-2010, 06:40 PM #13
Sorry, I'll leave that to you.
- 08-09-2010, 06:42 PM #14
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
Ah okay thanks anyways.
But what could be the problem? This applet is animated, which might effect the mouseclick listener?
- 08-09-2010, 06:44 PM #15
Don't know. Look at the Component class's methods Can you get any info on the applet's listeners?
- 08-09-2010, 06:53 PM #16
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
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?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}
- 08-09-2010, 07:36 PM #17
Sorry, I don't recognize what is your last post.
- 08-10-2010, 02:06 AM #18
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
So what should i do? Does anyone have any ideas?
- 08-10-2010, 02:13 AM #19
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.
- 08-10-2010, 02:28 AM #20
Senior Member
- Join Date
- Jul 2010
- Posts
- 124
- Rep Power
- 0
Similar Threads
-
Animation with Animated GIF
By JavaBean in forum Java 2DReplies: 3Last Post: 06-04-2011, 04:26 PM -
Extracting jpg from animated gifs
By Maz in forum Java 2DReplies: 3Last Post: 05-05-2011, 12:29 PM -
Help with Applet implementing mouselistener
By dsym@comcast.net in forum Java AppletsReplies: 3Last Post: 05-14-2009, 12:57 AM -
Playing an Animated GIF
By c0m4ndo45 in forum Java 2DReplies: 3Last Post: 04-24-2009, 10:47 AM -
Animated JPanels + Moving Pages
By PetalumaBoy in forum New To JavaReplies: 0Last Post: 02-16-2009, 11:09 AM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks