3 // This file is part of PosterDivider.
5 package jp.sourceforge.posterdivider;
7 import java.awt.Canvas;
9 import java.awt.Graphics;
10 import java.awt.image.*;
12 public class CanvasPreview extends Canvas {
13 private static final long serialVersionUID = 1L;
15 private ImageContainer ic;
16 private PdfGenerator.CreatePosterArgs args;
19 public void paint(Graphics g) {
22 g.setColor(Color.WHITE);
23 g.fillRect(0, 0, this.getWidth(), this.getHeight());
24 g.setColor(Color.GRAY);
25 g.drawRect(0, 0, this.getWidth() - 1, this.getHeight() - 1);
27 if (ic == null || args == null) {
31 BufferedImage awtImage = ic.getAwtImage();
34 float srcBoxX = this.getWidth() * 0.05f;
35 float srcBoxY = this.getHeight() * 0.05f;
36 float srcBoxWidth = this.getWidth() * 0.40f;
37 float srcBoxHeight = this.getHeight() * 0.90f;
38 // g.setColor(Color.GRAY);
39 // g.drawRect((int) srcBoxX, (int) srcBoxY, (int) srcBoxWidth,
40 // (int) srcBoxHeight);
42 float srcImageX, srcImageY, srcImageWidth, srcImageHeight, srcImageRate;
43 if (ic.getImageWidth() * srcBoxHeight > ic.getImageHeight()
47 srcImageWidth = srcBoxWidth;
48 srcImageRate = srcImageWidth / ic.getImageWidth();
49 srcImageHeight = ic.getImageHeight() * srcImageRate;
50 srcImageY = srcBoxY + (srcBoxHeight - srcImageHeight) / 2;
54 srcImageHeight = srcBoxHeight;
55 srcImageRate = srcImageHeight / ic.getImageHeight();
56 srcImageWidth = ic.getImageWidth() * srcImageRate;
57 srcImageX = srcBoxX + (srcBoxWidth - srcImageWidth) / 2;
59 int srcImageLeft = (int) srcImageX;
60 int srcImageTop = (int) srcImageY;
61 int srcImageRight = (int) (srcImageX + srcImageWidth);
62 int srcImageBottom = (int) (srcImageY + srcImageHeight);
63 if (awtImage != null) {
64 g.drawImage(awtImage, srcImageLeft, srcImageTop, srcImageRight,
65 srcImageBottom, 0, 0, awtImage.getWidth(), awtImage
69 float srcX = srcImageX + args.getSrcLeft() * srcImageRate;
70 // srcは左上原点だが、CreatePosterArgsのsrcは左下原点なのでここで解消する
71 float srcY = srcImageY + (ic.getImageHeight() - args.getSrcTop())
73 float srcWidth = args.getSrcWidth() * srcImageRate;
74 float srcHeight = args.getSrcHeight() * srcImageRate;
75 int srcLeft = (int) srcX;
76 int srcTop = (int) srcY;
77 int srcRight = (int) (srcX + srcWidth);
78 int srcBottom = (int) (srcY + srcHeight);
79 if (awtImage != null) {
81 g.setColor(new Color(1.0f, 1.0f, 1.0f, 0.5f));
82 g.fillRect(srcImageLeft, srcImageTop, srcImageRight - srcImageLeft,
83 srcTop - srcImageTop);
84 g.fillRect(srcImageLeft, srcTop, srcLeft - srcImageLeft, srcBottom
86 g.fillRect(srcRight, srcTop, srcImageRight - srcRight, srcBottom
88 g.fillRect(srcImageLeft, srcBottom, srcImageRight - srcImageLeft,
89 srcImageBottom - srcBottom);
91 g.setColor(Color.GRAY);
92 g.fillRect(srcLeft, srcTop, srcRight - srcLeft, srcBottom - srcTop);
94 g.setColor(Color.BLACK);
95 g.drawRect(srcImageLeft, srcImageTop, srcImageRight - srcImageLeft,
96 srcImageBottom - srcImageTop);
97 g.setColor(Color.BLACK);
98 g.drawRect(srcLeft, srcTop, srcRight - srcLeft, srcBottom - srcTop);
102 float posterBoxX = this.getWidth() * 0.55f;
103 float posterBoxY = this.getHeight() * 0.05f;
104 float posterBoxWidth = this.getWidth() * 0.40f;
105 float posterBoxHeight = this.getHeight() * 0.90f;
106 // g.setColor(Color.GRAY);
107 // g.drawRect((int) posterBoxX, (int) posterBoxY, (int) posterBoxWidth,
108 // (int) posterBoxHeight);
110 float posterX, posterY, posterWidth, posterHeight, posterRate;
111 if (args.getPosterWidth() * posterBoxHeight > args.getPosterHeight()
114 posterX = posterBoxX;
115 posterWidth = posterBoxWidth;
116 posterRate = posterWidth / args.getPosterWidth();
117 posterHeight = args.getPosterHeight() * posterRate;
118 posterY = posterBoxY + (posterBoxHeight - posterHeight) / 2;
121 posterY = posterBoxY;
122 posterHeight = posterBoxHeight;
123 posterRate = posterHeight / args.getPosterHeight();
124 posterWidth = args.getPosterWidth() * posterRate;
125 posterX = posterBoxX + (posterBoxWidth - posterWidth) / 2;
127 int postarLeft = (int) posterX;
128 int postarTop = (int) posterY;
129 int postarRight = (int) (posterX + posterWidth);
130 int postarBottom = (int) (posterY + posterHeight);
131 if (awtImage != null) {
132 g.drawImage(awtImage, postarLeft, postarTop, postarRight,
133 postarBottom, (int) (awtImage.getWidth()
134 * args.getSrcLeft() / ic.getImageWidth()),
135 (int) (awtImage.getHeight()
136 * (ic.getImageHeight() - args.getSrcTop()) / ic
137 .getImageHeight()), (int) (awtImage.getWidth()
138 * args.getSrcRight() / ic.getImageWidth()),
139 (int) (awtImage.getHeight()
140 * (ic.getImageHeight() - args.getSrcBottom()) / ic
141 .getImageHeight()), this);
143 g.setColor(Color.GRAY);
144 g.fillRect(postarLeft, postarTop, postarRight - postarLeft,
145 postarBottom - postarTop);
147 g.setColor(Color.BLACK);
148 g.drawRect(postarLeft, postarTop, postarRight - postarLeft,
149 postarBottom - postarTop);
151 int paperCountX = PdfGenerator.simulateOutputPaperCountX(args);
152 int paperCountY = PdfGenerator.simulateOutputPaperCountY(args);
154 if (paperCountX < 0 || paperCountY < 0) {
159 float paperX = posterX - args.getMarginLeft() * posterRate;
160 float paperY = posterY - args.getMarginTop() * posterRate;
161 float paperWidth = args.getPaperWidth() * posterRate;
162 float paperHeight = args.getPaperHeight() * posterRate;
163 float paperTextWidth = (args.getPaperWidth() - args.getMarginLeft() - args
166 float paperTextHeight = (args.getPaperHeight() - args.getMarginTop() - args
171 for (int i = 0; i < paperCountY; i++) {
173 for (int j = 0; j < paperCountX; j++) {
175 if ((i + j) % 2 == 0) {
176 g.setColor(Color.RED);
177 g.drawRect((int) x, (int) y, (int) paperWidth,
180 g.setColor(Color.BLUE);
181 g.drawRect((int) x + 1, (int) y + 1, (int) paperWidth,
186 y += paperTextHeight;
190 public ImageContainer getImageContainer() {
194 public void setImageContainer(ImageContainer ic) {
198 public PdfGenerator.CreatePosterArgs getArgs() {
202 public void setArgs(PdfGenerator.CreatePosterArgs args) {