user interface - Printing of Java GUI -


i have created gui in java , have 1 button name print....on printing gui print takes long time print....please me of how reduce waiting time...thank you

i using following code printing

public void actionperformed(actionevent ae) {     if (ae.getsource() == bprint)     {         printerjob job = printerjob.getprinterjob();         job.setprintable(this);         boolean ok = job.printdialog();         system.out.println("here");         if (ok)         {             try             {                 job.print();             }             catch (printerexception ex)             {                 system.out.println(ex);             }         }     }  }  public int print(graphics g, pageformat pf, int page) throws printerexception {      if (page > 0)     {         return no_such_page;     }      graphics2d g2d = (graphics2d)g;     g2d.translate(pf.getimageablex(), pf.getimageabley());      p1.printall(g);      return page_exists; } 

it takes long time print. reduce running time performing printing in separate thread.

you have pass jframe (to print whole gui) or swing component wish print printing thread.

    printbutton.addactionlistener(new actionlistener() {         @override         public void actionperformed(actionevent event) {             new thread(new printactionlistener(frame)).start();         }     }); 

here's printer thread

import java.awt.graphics; import java.awt.graphics2d; import java.awt.image.bufferedimage; import java.awt.print.pageformat; import java.awt.print.printable; import java.awt.print.printerexception; import java.awt.print.printerjob;  import javax.swing.jpanel;  import com.ggl.sudoku.solver.view.sudokuframe;  public class printactionlistener implements runnable {      private sudokuframe frame;      public printactionlistener(sudokuframe frame) {         this.frame = frame;     }      @override     public void run() {         final bufferedimage image = createpanelimage();          printerjob printjob = printerjob.getprinterjob();         printjob.setprintable(new imageprintable(printjob, image));          if (printjob.printdialog()) {             try {                 printjob.print();             } catch (printerexception prt) {                 prt.printstacktrace();             }         }     }      private bufferedimage createpanelimage() {         jpanel panel = frame.getsudokupanel();         bufferedimage image = new bufferedimage(panel.getwidth(),                 panel.getheight(), bufferedimage.type_int_rgb);         graphics2d g = image.creategraphics();         panel.paint(g);         g.dispose();         return image;     }      public class imageprintable implements printable {          private double          x, y, width;          private int             orientation;          private bufferedimage   image;          public imageprintable(printerjob printjob, bufferedimage image) {             pageformat pageformat = printjob.defaultpage();             this.x = pageformat.getimageablex();             this.y = pageformat.getimageabley();             this.width = pageformat.getimageablewidth();             this.orientation = pageformat.getorientation();             this.image = image;         }          @override         public int print(graphics g, pageformat pageformat, int pageindex)                 throws printerexception {             if (pageindex == 0) {                 int pwidth = 0;                 int pheight = 0;                 if (orientation == pageformat.portrait) {                     pwidth = (int) math.min(width, (double) image.getwidth());                     pheight = pwidth * image.getheight() / image.getwidth();                 } else {                     pheight = (int) math.min(width, (double) image.getheight());                     pwidth = pheight * image.getwidth() / image.getheight();                 }                 g.drawimage(image, (int) x, (int) y, pwidth, pheight, null);                 return page_exists;             } else {                 return no_such_page;             }         }      }  } 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -