MultiThreading locking up Java Swing GUI -


i'm working on project multiple user paint program , have run problem gui locking up.

i have of program done except error, rather large program tried reproduce error in smaller program.

in smaller program, have 2 jframes. both of them can drawn in clicking , dragging mouse. secondary jframe thread sleeps 10 seconds , sends have drawn other frame displayed.

however, once main frame has received image secondary frame, gui locks , main frame can no longer drawn in.

i'm using swingutilities.invokelater() method work with. while looking answers, found swingworker class, wanted see if there simple solution before undertook large rewrite of code try make work swingworker.

thanks reading. code below. also, first time posting here. seemed having trouble formatting code, apologize in advance if comes out wrong. best fix it.

package gui_thread_test;  import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import java.awt.image; import java.awt.renderinghints; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.event.mousemotionadapter; import javax.swing.jcomponent;  public class drawpanel extends jcomponent {     private image canvas;     private int x, y, prevx, prevy;     graphics2d g2;      public drawpanel()     {         setdoublebuffered(false);         addmouselistener(new mouseadapter()         {             @override             public void mousepressed(mouseevent e )             {                 prevx = e.getx();                 prevy = e.gety();             }         });          addmousemotionlistener(new mousemotionadapter()         {             @override             public void mousedragged(mouseevent e)             {                 x = e.getx();                 y = e.gety();                 g2.drawline(prevx, prevy, x, y);                 repaint();                 prevx = x;                 prevy = y;             }         });     }      @override     public void paintcomponent(graphics g)     {         if (canvas == null)         {             canvas = createimage(getsize().width, getsize().height);             g2 = (graphics2d) canvas.getgraphics();             g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on);          }         g.drawimage(canvas, 0, 0, null);     }      public synchronized void updatecanvas(image _canvas)     {         canvas = _canvas;         repaint();     }      public image getimage()     {         return canvas;     } } 

-

package gui_thread_test;  import java.awt.dimension; import java.awt.graphics; import java.awt.graphics2d; import java.awt.image; import java.awt.renderinghints; import java.util.logging.level; import java.util.logging.logger; import javax.swing.jframe; import javax.swing.swingutilities;  public class threadtest extends jframe implements runnable {     private image canvas;     private graphics2d g2;     private drawpanel panel;     private drawpanel threadpanel;      public threadtest(drawpanel _panel)     {         super("secondary");         panel = _panel;          this.setdefaultcloseoperation(jframe.exit_on_close);         this.setpreferredsize(new dimension(500,500));         this.setminimumsize(new dimension(500,500));         this.setvisible(true);          threadpanel = new drawpanel();         this.add(threadpanel);     }       public void paintcomponent(graphics g)     {         if (canvas == null)         {             canvas = createimage(getsize().width, getsize().height);             g2 = (graphics2d) canvas.getgraphics();             g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on);         }         g.drawimage(canvas, 0, 0, null);             }      @override     public void run() {         //sleep thread 10 seconds give time draw on image         try {             thread.sleep(10000);         } catch (interruptedexception ex) {             logger.getlogger(threadtest.class.getname()).log(level.severe, null, ex);         }          swingutilities.invokelater(                 new runnable() {                     @override                     public void run() {                         //posts message server chat window                         panel.updatecanvas(threadpanel.getimage());                     }                 });     }  } 

-

package gui_thread_test;  import java.awt.dimension; import javax.swing.jframe;   public class gui_thread_test {      public static void main(string[] args)      {         jframe window = new jframe("main");         window.setdefaultcloseoperation(jframe.exit_on_close);         window.setpreferredsize(new dimension(500,500));         window.setminimumsize(new dimension(500,500));          drawpanel draw = new drawpanel();         window.add(draw);          thread testthread = new thread(new threadtest(draw));         testthread.start();           window.pack();         window.setvisible(true);     }  } 

use swing timer rather thread every 10 second transfer. use bufferedimages hold drawings , transfer drawings.

for example:

import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.point; import java.awt.dialog.modalitytype; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.image.bufferedimage;  import javax.swing.*;  public class guidoubledraw extends jpanel {    private static final int bi_width = 500;    private static final int bi_height = bi_width;    public static final color pen_color = color.black;    private bufferedimage backgroundimg = new bufferedimage(bi_width, bi_height,          bufferedimage.type_int_argb);     public guidoubledraw() {       mymouseadapter mouseadapter = new mymouseadapter();       addmouselistener(mouseadapter);       addmousemotionlistener(mouseadapter);    }     @override    protected void paintcomponent(graphics g) {       super.paintcomponent(g);       if (backgroundimg != null) {          g.drawimage(backgroundimg, 0, 0, this);       }    }     @override    public dimension getpreferredsize() {       return new dimension(bi_width, bi_height);    }     public bufferedimage getbackgroundimage() {       bufferedimage copyimg = new bufferedimage(bi_width, bi_height,             bufferedimage.type_int_argb);       graphics g = copyimg.getgraphics();       g.drawimage(backgroundimg, 0, 0, null);       g.dispose();       return copyimg;    }     public void setbackgroundimage(bufferedimage bimg) {       this.backgroundimg = bimg;       repaint();    }     private class mymouseadapter extends mouseadapter {       point previouspt = null;        @override       public void mousepressed(mouseevent mevt) {          previouspt = mevt.getpoint();       }        @override       public void mousedragged(mouseevent mevt) {          drawpt(mevt);       }        @override       public void mousereleased(mouseevent mevt) {          drawpt(mevt);       }        private void drawpt(mouseevent mevt) {          graphics g = backgroundimg.getgraphics();          point nextpt = mevt.getpoint();          g.setcolor(pen_color);          g.drawline(previouspt.x, previouspt.y, nextpt.x, nextpt.y);          g.dispose();          previouspt = nextpt;          repaint();       }     }     private static void createandshowgui() {       final guidoubledraw guidoubledraw1 = new guidoubledraw();       final guidoubledraw guidoubledraw2 = new guidoubledraw();        jframe frame = new jframe("draw 1");       frame.setdefaultcloseoperation(jframe.exit_on_close);       frame.getcontentpane().add(guidoubledraw1);       frame.pack();       frame.setlocationbyplatform(true);       frame.setvisible(true);        jdialog dialog = new jdialog(frame, "draw 2", modalitytype.modeless);       dialog.getcontentpane().add(guidoubledraw2);       dialog.pack();       dialog.setlocationrelativeto(null);       dialog.setvisible(true);        int timerdelay = 10 * 1000;       new timer(timerdelay, new actionlistener() {           @override          public void actionperformed(actionevent arg0) {             guidoubledraw1.setbackgroundimage(guidoubledraw2                   .getbackgroundimage());          }       }).start();    }     public static void main(string[] args) {       swingutilities.invokelater(new runnable() {          public void run() {             createandshowgui();          }       });    } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -