java - Creating new Thread to update JLabel and setIcon() in said separate Thread -


trying image application sending array of bytes through socket, translating bufferedimage , setting jlabel in gui updates every 3 seconds.

i tried looking on forums questions regarding graphical update recurrent me , never seem right -- there's @ least 6 update methods in java graphical interface , every 1 tried won't work.

i know problem isn't in connection client because can save image receive imageio.write() , updates every 3 seconds image expecting receive. can't have java updating jlabel correctly without having go forums , ask people. such complex task guess. here code: http://pastebin.com/95nmglvz. doing project in netbeans there's lot of stuff in there unnecessary read not directly relate problem.

i think answer lies in creating separate thread update jlabel ever-changing bufferedimage received objectinputstream. mind giving me hand @ this? better code? swingworker, threading (wtf setdaemon(flag)?), runnable, timer, invokelater? tried of this. not correctly apparently.

edit1: tried answer immibis:

public void startrunning() { try { server = new serversocket(666, 10); connection = server.accept(); networkstatus("connected " + connection.getinetaddress().gethostname()); thread thr = new thread(new runnable() { @override public void run() { try { input = new objectinputstream(connection.getinputstream()); } catch (ioexception ex) { joptionpane.showmessagedialog(null, ex.tostring()); } } }); thr.start(); system.out.println(!connection.isinputshutdown()); while (connection.isconnected()) { try { byte[] byteimage = (byte[]) input.readobject(); inputstream in = new bytearrayinputstream(byteimage); final bufferedimage bi = imageio.read(in); jlabel_screen.seticon(new imageicon(bi)); imageio.write(bi, "jpg", new file("c:\\users\\user\\desktop\\test.jpg")); system.out.println("i'm working"); } catch (ioexception ex) { joptionpane.showmessagedialog(null, ex.tostring()); } catch (classnotfoundexception ex) { logger.getlogger(spyxserver.class.getname()).log(level.severe, null, ex); } } } catch (ioexception ex) { joptionpane.showmessagedialog(null, ex.tostring()); } }

it not work. says byte[] byteimage = (byte[]) input.readobject(); line has nullpointerexception. value can null return readobject(), meaning either input not initialized correctly or connection not synchronized. hope it's first option because wouldn't know how handle last.

edit2:

tried answer blazetopher:

public void startrunning() throws ioexception { server = new serversocket(666, 10); try { connection = server.accept(); networkstatus("connected " + connection.getinetaddress().gethostname()); input = new objectinputstream(connection.getinputstream()); while (true) { try { byte[] byteimage = (byte[]) input.readobject(); inputstream in = new bytearrayinputstream(byteimage); final bufferedimage bi = imageio.read(in); swingutilities.invokelater(new runnable() {//<----------- @override public void run() { jlabel_screen.seticon(new imageicon(bi)); } }); imageio.write(bi, "jpg", new file("c:\\users\\user\\desktop\\test.jpg")); system.out.println("i'm working"); } catch (ioexception ex) { joptionpane.showmessagedialog(null, ex.tostring()); } catch (classnotfoundexception ex) { logger.getlogger(spyxserver.class.getname()).log(level.severe, null, ex); } } } catch (eofexception eofexception) { networkstatus("connection closed. :("); } { input.close(); connection.close(); } }

using swingutilities.invokelater didn't work either. @ least program runs , can save image still can't update jlabel. running out of options here?

edit3: tried jordan's code:

@override public void paint(graphics g) { g.drawimage(biglobal, 0, 0, null); }

the gui kind of crashed , "drawing" components when had mouse cursor hovering it. when started code, did not crashed (+1) did not draw anything, when try hover cursor bufferedimage should painted. maybe should add revalidate() or repaint after calling overwritten paint(getgraphics()) inside startrunning() method?

edit4: while(true) code in may problem when use swingtimer gets out of sync client , crashes after first cycle. alternatives this?

generally speaking have producer/consumer pattern. producing images , wants consume images. normally, consumer wait on producer tell has been produced, in case, can use observer pattern instead, having producer notify consumer been produced (instead of waiting it)

we need someway producer communicate consumer...

public interface pictureconsumer {     public void newpicture(bufferedimage img); } 

you create implementation of in ui code, set icon property of jlabel

now, need produce images...

public class pictureproducer extends swingworker<object, bufferedimage> {      private pictureconsumer consumer;      public pictureproducer(pictureconsumer consumer) {         this.consumer = consumer;     }      @override     protected void process(list<bufferedimage> chunks) {         // interested in last image         bufferedimage img = chunks.get(chunks.size() - 1);         consumer.newpicture(img);     }      @override     protected object doinbackground() throws exception {         /*          whole setup worries me. why program acting           server?  why aren't pooling image producer?                   */         try (serversocket server = serversocketfactory.getdefault().createserversocket(666, 10)) {             try (socket socket = server.accept()) {                 try (objectinputstream ois = new objectinputstream(socket.getinputstream())) {                     // using `while (true)` bad idea, relying on fact                     // exception thrown when connection closed                     // bad idea.                     while (!socket.isclosed()) {                         // generally, i'd discourage using objectinputstream,                          // me, read directly                         // bytearrayinputstream...assuming sender sending                         // data byte stream ;)                         byte[] bytes = (byte[]) ois.readobject();                         try (bytearrayinputstream bis = new bytearrayinputstream(bytes)) {                             bufferedimage img = imageio.read(bis);                             publish(img);                         }                     }                 }             }         }         return null;     }      @override     protected void done() {         try {             get();         } catch (exception e) {             e.printstacktrace();             joptionpane.showmessagedialog(null, "image producer has failed: " + e.getmessage(), "error", joptionpane.error_message);         }     }  } 

see worker threads , swingworker more details

you can reverse example of (where server producing images , client consuming them) here


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -