1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package unbbayes.controller;
22
23 import java.awt.Graphics;
24 import java.awt.Graphics2D;
25 import java.awt.Rectangle;
26 import java.awt.geom.AffineTransform;
27 import java.awt.print.PageFormat;
28 import java.awt.print.Pageable;
29 import java.awt.print.Printable;
30
31 import javax.swing.JComponent;
32
33
34
35
36 public class PrintNet implements Printable, Pageable {
37
38 protected JComponent net;
39 protected PageFormat pageFormat;
40 protected Rectangle rect;
41 protected String title;
42
43 public PrintNet(String title, JComponent net, Rectangle rect, PageFormat pf) {
44 this.net = net;
45 this.title = title;
46 pageFormat = pf;
47 this.rect = rect;
48 }
49
50
51
52
53 public int print(Graphics g, PageFormat pf, int index) {
54 if (index == 0) {
55 paintComponent(g);
56 return Printable.PAGE_EXISTS;
57 }
58 return Printable.NO_SUCH_PAGE;
59 }
60
61
62
63
64 protected void paintComponent(Graphics g) {
65 double scaleX = (pageFormat.getImageableWidth()) / rect.width;
66 double scaleY = (pageFormat.getImageableHeight()-30) / (rect.height);
67 double scaleFactor = Math.min(Math.min(scaleX, scaleY), 1.0);
68
69 Graphics2D g2 = (Graphics2D)g;
70 Rectangle clipRect = g2.getClipBounds();
71 AffineTransform at = g2.getTransform();
72
73 int x = (int)(pageFormat.getImageableX());
74 int y = (int)(pageFormat.getImageableY());
75
76
77
78 g.drawString(title,x,y+15);
79 g2.translate(x - rect.getX(), y - rect.getY() + 30);
80 g2.setClip(rect);
81 g2.scale(scaleFactor, scaleFactor);
82 net.paint(g);
83 g2.setTransform(at);
84 g2.setClip(clipRect);
85 }
86
87
88
89
90
91 public int getNumberOfPages() {
92 return 1;
93 }
94
95 public Printable getPrintable(int index) {
96 return this;
97 }
98
99 public PageFormat getPageFormat(int index) {
100 return pageFormat;
101 }
102
103 }