Gå til innhold

Anbefalte innlegg

import java.awt.*;
import javax.swing.*;
import java.awt.print.*;

/** A simple utility class that lets you very simply print
*  an arbitrary component. Just pass the component to the
*  PrintUtilities.printComponent. The component you want to
*  print doesn't need a print method and doesn't have to
*  implement any interface or do anything special at all.
*  <P>
*  If you are going to be printing many times, it is marginally more 
*  efficient to first do the following:
*  <PRE>
*    PrintUtilities printHelper = new PrintUtilities(theComponent);
*  </PRE>
*  then later do printHelper.print(). But this is a very tiny
*  difference, so in most cases just do the simpler
*  PrintUtilities.printComponent(componentToBePrinted).
*
*  7/99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*  May be freely used or adapted.
*/

public class PrintUtilities implements Printable {
 private Component componentToBePrinted;

 public static void printComponent(Component c) {
   new PrintUtilities(c).print();
 }
 
 public PrintUtilities(Component componentToBePrinted) {
   this.componentToBePrinted = componentToBePrinted;
 }
 
 public void print() {
   PrinterJob printJob = PrinterJob.getPrinterJob();
   printJob.setPrintable(this);
   if (printJob.printDialog())
     try {
       printJob.print();
     } catch(PrinterException pe) {
       System.out.println("Error printing: " + pe);
     }
 }

 public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
   if (pageIndex > 0) {
     return(NO_SUCH_PAGE);
   } else {
     Graphics2D g2d = (Graphics2D)g;
     g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
     disableDoubleBuffering(componentToBePrinted);
     componentToBePrinted.paint(g2d);
     enableDoubleBuffering(componentToBePrinted);
     return(PAGE_EXISTS);
   }
 }

 /** The speed and quality of printing suffers dramatically if
  *  any of the containers have double buffering turned on.
  *  So this turns if off globally.
  *  @see enableDoubleBuffering
  */
 public static void disableDoubleBuffering(Component c) {
   RepaintManager currentManager = RepaintManager.currentManager(c);
   currentManager.setDoubleBufferingEnabled(false);
 }

 /** Re-enables double buffering globally. */
 
 public static void enableDoubleBuffering(Component c) {
   RepaintManager currentManager = RepaintManager.currentManager(c);
   currentManager.setDoubleBufferingEnabled(true);
 }
}

 

Fant dette som lar meg skrive ut en hvilken som helst Component. Problemet er at det ikke tas hensyn til arkstørrelsen.. Ønsker meg valg av arkformat og en skalering av utskriften til en side. Noen tips? :cry:

Lenke til kommentar
Videoannonse
Annonse

Dette gjorde trikset:

import java.awt.*;
import java.awt.geom.*;
import java.awt.print.*;
import javax.swing.*;

/** A simple utility class that lets you very simply print
*  an arbitrary component. Just pass the component to the
*  PrintUtilities.printComponent. The component you want to
*  print doesn't need a print method and doesn't have to
*  implement any interface or do anything special at all.
*  <P>
*  If you are going to be printing many times, it is marginally more 
*  efficient to first do the following:
*  <PRE>
*    PrintUtilities printHelper = new PrintUtilities(theComponent);
*  </PRE>
*  then later do printHelper.print(). But this is a very tiny
*  difference, so in most cases just do the simpler
*  PrintUtilities.printComponent(componentToBePrinted).
*
*  7/99 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*  May be freely used or adapted.
*/
public class PrintUtilities {
 private Component componentToBePrinted;

 public static void printComponent(Component c) {
   new PrintUtilities(c).print();
 }
 
 public PrintUtilities(Component componentToBePrinted) {
   this.componentToBePrinted = componentToBePrinted;
 }
 
 public void print() {
   PrinterJob printJob = PrinterJob.getPrinterJob();

Book book=new Book();
PageFormat documentPageFormat=new PageFormat();
documentPageFormat=printJob.pageDialog(documentPageFormat);
book.append(new Pg(componentToBePrinted),documentPageFormat);         
printJob.setPageable(book); 
   if (printJob.printDialog())
     try {
       	printJob.print();
     }
     catch(PrinterException pe) {
 	System.out.println("Error printing: " + pe);
     }
 }
}

/** A Printable page in a Book.
*  The page content is scaled to fit the page format.
*/
class Pg implements Printable {
private Component componentToBePrinted;

public Pg(Component componentToBePrinted) {
 this.componentToBePrinted = componentToBePrinted;
   }
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
 if(pageIndex>0) {
 	return(NO_SUCH_PAGE);
 }
 else {
 	Graphics2D g2d=(Graphics2D) g;
 	g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); //translate origin
 	disableDoubleBuffering(componentToBePrinted);
 	AffineTransform saveAT=g2d.getTransform(); //get the current transform
 	Rectangle componentBounds=componentToBePrinted.getBounds(null);
 	double scaleX=pageFormat.getImageableWidth()/componentBounds.width;
 	double scaleY=pageFormat.getImageableHeight()/componentBounds.height;
 	if(scaleX<1 || scaleY<1) {
   if(scaleX<scaleY) {
   	scaleY=scaleX;
   }
   else {
   	scaleX=scaleY;
   }
//    setSize( (float) (componentBounds.width * scaleX), (float) (componentBounds.height * scaleY) );
   g2d.scale(scaleX,scaleY); //scale print to page
 	}
 	componentToBePrinted.paint(g2d);
 	g2d.setTransform(saveAT); //restore original transform
 	enableDoubleBuffering(componentToBePrinted);
 	return(PAGE_EXISTS);
 }
}
 
/** The speed and quality of printing suffers dramatically if
 *  any of the containers have double buffering turned on.
 *  So this turns if off globally.
 *  @see enableDoubleBuffering
 */
public static void disableDoubleBuffering(Component c) {
 RepaintManager currentManager = RepaintManager.currentManager(c);
 currentManager.setDoubleBufferingEnabled(false);
}
 
/** Re-enables double buffering globally. */
public static void enableDoubleBuffering(Component c) {
 RepaintManager currentManager = RepaintManager.currentManager(c);
 currentManager.setDoubleBufferingEnabled(true);
}
}

 

Kanskje noen andre som har bruk for tilsvarende en gang..

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...