1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
package Apps;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.print.*;
/**
* Utility class for printing swing components.
*
* @author <a href="http://www.sics.se/~martins">Martin Svensson</a>
* @version 1.0
*/
public class PrintUtilities implements Printable {
private Component component;
private int method;
float scale;
/**
* Prints a component without any rescaling.
*
*/
public final static int AS_IS = 0;
/**
* Scales a component to fit the book format.
*
*/
public final static int SCALE_TO_FIT = 1;
/**
* Scale so that the component will fit on one output page.
*
*/
public final static int BOOK_HEIGHT = 2;
/**
* Scale so that the width is best fitted.
*
*/
public final static int BOOK_WIDTH = 3;
/**
* Scale to Perfect A4.
*
*/
public final static int A4_LANDS = 4;
/**
* Print a componet using a particular scaling method
*
* @param c the <code>Component</code> to print
* @param method the scaling method to use
*/
public static final int PORTRAIT = PageFormat.PORTRAIT,
PAYSAGE = PageFormat.LANDSCAPE;
private boolean showDialog = true;
public int orientation = 0;
public static void printComponent(Component c, int method) {
new PrintUtilities(c, method).print();
}
private PrintUtilities(Component component, int method) {
this.component = component;
this.method = method;
}
private Book makeBook(PageFormat pf){
pf.setOrientation(PageFormat.LANDSCAPE);
Book book = new Book();
book.append(this, pf, getPageCount(pf)-1);
System.out.println(book.getNumberOfPages());
return book;
}
private void print(){
PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat pf = printJob.defaultPage();
scale = calculateScale(pf);
printJob.setPageable(makeBook(pf));
if (printJob.printDialog())
try{
printJob.print();
}
catch(PrinterException pe){System.out.println("Error printing: " + pe);}
}
private float calculateScale(PageFormat pf){
float scl = 1f;
switch(method){
case BOOK_HEIGHT:
scl = (float) pf.getImageableHeight()/component.getSize().height;
break;
/*case BOOK_WIDTH:
scl = (float) pf.getImageableWidth()/component.getSize().width;
break;*/
case SCALE_TO_FIT:
scl = (float) Math.min(pf.getImageableWidth()/component.getSize().width,
pf.getImageableHeight()/component.getSize().height);
break;
case A4_LANDS:
scl = (float) 220/500;//pf.getImageableWidth()/component.getSize().width;//=0.40
break;
case AS_IS:
scl = 1; //no scaling
break;
}
if(scl > 1)
scl = 1; //we only resize the component if it is to large
return scl;
}
private int getPageCount(PageFormat pf){
//calculate scale:
if(method == BOOK_HEIGHT){
return (int) Math.ceil((220/500));//scale*component.getSize().width)/pf.getImageableWidth());//0.40*1472/537
}
/*else if(method == BOOK_WIDTH){
return (int) Math.ceil((scale*component.getSize().height)/pf.getImageableHeight());
}*/
else if(method == A4_LANDS){
return (int) Math.ceil((scale*component.getSize().width)/350);
}
else{ //for the other two methods only one page will be printed
return 1;
}
}
/**
* Prints a page.
*
* @param g contains what is to be printed
* @param pf the format of the output page (i.e., A4, letter,...)
* @param page number
* @return PAGE_EXISTS
*/
public int print(Graphics g, PageFormat pf, int page) {
if (page >= getPageCount(pf)){
return(NO_SUCH_PAGE);
}
else{
Graphics2D g2 = (Graphics2D)g;
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.clip(new Rectangle2D.Double(0,0,pf.getImageableWidth(), pf.getImageableHeight()));
//translate and scale:
switch(method){
/* case BOOK_WIDTH:
g2.translate(0, -page*pf.getImageableHeight());
break;*/
case BOOK_HEIGHT:
g2.translate(-page*pf.getImageableWidth(),0);
break;
case A4_LANDS:
g2.translate(0, -page*pf.getImageableHeight());
break;
}
g2.scale(scale, scale);
//finally do the printing:
disableDoubleBuffering(component);
component.paint(g2);
enableDoubleBuffering(component);
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.
*
* @param c Componet to be printed
* @see #enableDoubleBuffering
*/
public static void disableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
}
/**
* Re-enables double buffering globally.
*
* @param c the component to reenable
*/
public static void enableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);
}
} |
Partager