Solved

Printing in Threads

Posted on 1999-01-25
3
371 Views
Last Modified: 2008-03-04
I need the user to be able to manuever within a GUI while the user is printing (from Java).
Im trying to get Java2D printing to run in a separate thread.
Java 2D printing sometimes takes 2-3 minutes to run, thats alot of time for a user to be staring at the screen.
I have a print method (in the GUI class) that has this code
fragment:

try  {
     PrinterJob printerjob= PrinterJob.getPrinterJob();
     PageFormat pf= printerjob.defaultPage();
     printerjob.setPrintable(new PrintGraphPainter(linegraph.getImages()),pf);
     if (printerjob.printDialog())  {  
          printerjob.print();                    
     }
}
catch (PrinterException pe)  {
     System.out.println("Print Error");
     pe.printStackTrace();
}

Heres the PrintGraphPainter class:
//PrintGraphPainter.java
import java.awt.*;
import java.awt.print.*;

class PrintGraphPainter implements Printable   {

     Image[] images;

     public PrintGraphPainter(Image[] images)  {
          this.images=images;
     }
     public synchronized int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException  {

          if (pageIndex < 1)   {
               Dimension f= new Dimension((int)pf.getImageableWidth(),(int)pf.getImageableHeight());
               int XIMG=(int) (f.width-10);   int YIMG=(int)(XIMG * .5042);
               int XIMG2=(int)(f.width-10);   int YIMG2=(int)(XIMG2 * .1433);
               int XIMG3=(int)(f.width-10);   int YIMG3=(int)(XIMG3 * .07163);
               g.drawImage(images[0],(int)pf.getImageableX(),(int)pf.getImageableY(),XIMG3,YIMG3,null);
               g.drawImage(images[1],(int)pf.getImageableX(),(int)pf.getImageableY()+YIMG3,XIMG,YIMG,null);  
               g.drawImage(images[2],(int)pf.getImageableX(),(int)pf.getImageableY()+YIMG3+YIMG,XIMG2,YIMG2,null);
               g.drawRect((int)pf.getImageableX(),(int)pf.getImageableY(),XIMG,YIMG+YIMG2+YIMG3);  
               return Printable.PAGE_EXISTS;
          }
          else
               return Printable.NO_SUCH_PAGE;
     }
}

Do I make the PrintGraphPainter class implement the Runnable interface?

Im using JDK1.2 on Win95.
0
Comment
Question by:mbunkows
  • 2
3 Comments
 
LVL 1

Expert Comment

by:malexiev
ID: 1234639
I'm not familiar with Java 2D but I think you should put your "slow" part of code in a thread
like this :

Thread t = new Thread () {
  public void run() {
     try  {
       PrinterJob printerjob= PrinterJob.getPrinterJob();
       PageFormat pf= printerjob.defaultPage();
       printerjob.setPrintable(new PrintGraphPainter(linegraph.getImages()),pf);
       if (printerjob.printDialog())  {  
           printerjob.print();                      
       }
    }
    catch (PrinterException pe)  {
      System.out.println("Print Error");
      pe.printStackTrace();
    }
  }
};

t.start();
0
 

Author Comment

by:mbunkows
ID: 1234640
Good thing I asked I thought this was actually going to take some work!
Post an answer and I'll give you an A
Thanks!
0
 
LVL 1

Accepted Solution

by:
malexiev earned 100 total points
ID: 1234641
Here it is :

I'm not familiar with Java 2D but I think you should put your "slow" part of code in a thread
like this :

      Thread t = new Thread () {
        public void run() {
           try  {
             PrinterJob printerjob= PrinterJob.getPrinterJob();
             PageFormat pf= printerjob.defaultPage();
             printerjob.setPrintable(new PrintGraphPainter(linegraph.getImages()),pf);
             if (printerjob.printDialog())  {    
                 printerjob.print();                      
             }
          }
          catch (PrinterException pe)  {
            System.out.println("Print Error");
            pe.printStackTrace();
          }
        }
      };
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 72
jdbc error in jsp application 20 63
eclipse apache tomcat admin console 52 121
jsp error 6 26
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question