multithreading - Is this how you multithread with an autoclicker and GUI in Java? -
i looking create autoclicker jframe gui in java robot class. wondering if best way create gui in different threads remain responsive while autoclicker/robot performing actions?
i read in oracle documentation swing objects have own event dispatcher thread, maybe isn't necessary @ all? if so, it? in advance!
package engine; import javax.swing.*; import gui.ui; public class engine { private thread uithread, clickerthread; public ui ui; public autoclicker autoclicker; public engine(int width, int height) { ui = new ui(width, height); ui.setdefaultcloseoperation(jframe.exit_on_close); ui.setvisible(true); uithread = new thread(ui); uithread.start(); //autoclicker robot class autoclicker = new autoclicker(); clickerthread = new thread(autoclicker); clickerthread.start(); } } here's ui(and event handler) , autoclicker classes respectively in case helps:
package gui; import javax.swing.*; public class ui extends jframe implements runnable{ public int width, height; jbutton start, stop; public ui(int width, int height) { this.width = width; this.height = height; this.setlayout(null); this.setsize(width, height); } public void init() { start = new jbutton(); start.settext("start"); start.setbounds(100, 100, 100, 30); add(start); stop = new jbutton(); stop.settext("stop"); stop.setbounds(100, 140, 100, 30); add(stop); eventhandler ehandler = new eventhandler(this); start.addactionlistener(ehandler); stop.addactionlistener(ehandler); } @override public void run() { init(); } } eventhandler class
package gui; import java.awt.event.actionevent; import java.awt.event.actionlistener; public class eventhandler implements actionlistener{ private ui ui; public eventhandler(ui ui) { this.ui = ui; } public void actionperformed(actionevent actionevent) { if(actionevent.getsource().equals(ui.start)) { //start autoclicking system.out.println("start"); }else if(actionevent.getsource().equals(ui.stop)) { //stop autoclicking system.out.println("stop"); } } } the autoclicker class:
package engine; import java.awt.awtexception; import java.awt.robot; public class autoclicker implements runnable{ private robot robot; public void run() { try { robot = new robot(); //do mouse clicks , stuff } catch (awtexception e) { e.printstacktrace(); } } }
the event dispatching thread talking indeed thread should using javax.swing create responsive gui. when going display window, should schedule job edt via javax.swing.swingutilites class. example:
import javax.swing.*; public class helloworldswing { private static void createandshowgui() { // create , set window. jframe frame = new jframe("helloworldswing"); frame.setdefaultcloseoperation(jframe.exit_on_close); // add ubiquitous "hello world" label. jlabel label = new jlabel("hello world"); frame.getcontentpane().add(label); // display window. frame.pack(); frame.setvisible(true); } public static void main(string[] args) { // schedule job event-dispatching thread: // creating , showing application's gui. javax.swing.swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } } (code taken directly helloworldswing.java)
the important part here in main method: ensures other code free use main thread , gui doesn't unresponsive when, say, you're downloading large file (then again, should in own thread too, depending on situation).
so now, if took code supplied , put in main class:
import engine.*; public class main { public static void main(string[] args){ javax.swing.swingutilities.invokelater(new runnable() { public void run() { new engine(640, 400); } }); } } with this, gui runs in separate thread, in engine class 2 new threads created, resulting in edt having 2 sub-threads, in way. should eliminate thread ui; add components original window in separate thread, kind of overkill when think it.
now, autoclicker. should implement when 'start' button pressed, new thread spawned runnable loops long boolean true, clicking fast want. when 'stop' button pressed (my guess robot still active...), set boolean false. should within eventhandler.actionperformed method.
Comments
Post a Comment