1
14
15 package demo.yext.export;
16
17
18 import y.view.BackgroundRenderer;
19 import y.view.Graph2DView;
20
21 import javax.print.attribute.standard.MediaSize;
22 import javax.print.attribute.standard.MediaSizeName;
23 import java.awt.BasicStroke;
24 import java.awt.Color;
25 import java.awt.Graphics2D;
26 import java.awt.Rectangle;
27 import java.awt.Stroke;
28 import java.awt.Insets;
29 import java.awt.print.PageFormat;
30
31
32
39 public class PageBackgroundRenderer implements BackgroundRenderer {
40 private final Graph2DView view;
41 private PageFormat pageFormat;
42
43
47 public PageBackgroundRenderer( final Graph2DView view ) {
48 this.view = view;
49 pageFormat = new PageFormat();
50 PageFormatHelper.assignMediaSize(
51 pageFormat,
52 MediaSize.getMediaSizeForName(MediaSizeName.ISO_A4)
53 );
54 }
55
56 public void paint(Graphics2D gfx, int x, int y, int w, int h) {
57 final Color oldColor = gfx.getColor();
58 final Stroke oldStroke = gfx.getStroke();
59
60 final Rectangle rect = gfx.getClipBounds();
61 gfx.setColor(Color.LIGHT_GRAY);
62 gfx.fillRect(rect.x, rect.y, rect.width, rect.height);
63
64 final double z = view.getZoom();
65 final int paperWidth = (int)Math.ceil(pageFormat.getWidth());
66 final int paperHeight = (int)Math.ceil(pageFormat.getHeight());
67
68 gfx.setColor(Color.WHITE);
69 gfx.fillRect(0, 0, paperWidth, paperHeight);
70 gfx.setColor(Color.BLACK);
71 gfx.setStroke(new BasicStroke((float) (1.0 / z)));
72 gfx.drawRect(0, 0, paperWidth, paperHeight);
73 gfx.setColor(Color.LIGHT_GRAY);
74
75 final Insets margin = PageFormatHelper.getMargin(pageFormat);
76 if (margin.top > 0) {
77 final int _y = margin.top;
78 gfx.drawLine(margin.left, _y, paperWidth - margin.right, _y);
79 }
80 if (margin.bottom > 0) {
81 final int _y = paperHeight - margin.bottom;
82 gfx.drawLine(margin.left, _y, paperWidth - margin.right, _y);
83 }
84 if (margin.left > 0) {
85 final int _x = margin.left;
86 gfx.drawLine(_x, margin.top, _x, paperHeight - margin.bottom);
87 }
88 if (margin.right > 0) {
89 final int _x = paperWidth - margin.right;
90 gfx.drawLine(_x, margin.top, _x, paperHeight - margin.bottom);
91 }
92
93 gfx.setStroke(oldStroke);
94 gfx.setColor(oldColor);
95 }
96
97
100 public void setPageFormat( final PageFormat pageFormat ) {
101 if (pageFormat == null) {
102 throw new IllegalArgumentException("null");
103 }
104
105 this.pageFormat = pageFormat;
106
107 if (view != null) {
108 view.updateView();
109 }
110 }
111
112
115 public PageFormat getPageFormat() {
116 return pageFormat;
117 }
118 }
119