-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPrinter.java
More file actions
57 lines (47 loc) · 1.58 KB
/
Printer.java
File metadata and controls
57 lines (47 loc) · 1.58 KB
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
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
public class Printer implements Printable {
private static final int INCH = 72;
private Component component;
public void printComponent(Component component) {
this.component = component;
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
boolean ok = job.printDialog();
if (ok) {
try {
job.print();
} catch (PrinterException ex) {
}
}
}
@Override
public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) g;
Dimension imageableArea = new Dimension((int) pf.getWidth() - (2 * INCH), (int) pf.getHeight() - (2 * INCH));
double widthRatio = imageableArea.getWidth() / component.getWidth();
double heightRatio = imageableArea.getHeight() / component.getHeight();
// If the component fits within the imageable area
if (widthRatio > 1 && heightRatio > 1) {
g2d.translate(getCenteredX(pf, 1.0), INCH);
} else {
double scale = Math.min(widthRatio, heightRatio);
g2d.translate(getCenteredX(pf, scale), INCH);
g2d.scale(scale, scale);
}
component.printAll(g2d);
return PAGE_EXISTS;
}
private int getCenteredX(PageFormat pf, double scale) {
return (int) ((pf.getWidth() / 2.0) - (component.getSize().getWidth() * scale / 2.0));
}
}