OSDN Git Service

chenge localization use resourcebundle
[posterdivider/PosterDivider.git] / src / jp / sourceforge / posterdivider / CanvasPreview.java
1 //
2 // CanvasPreview.java
3 // This file is part of PosterDivider.
4 //
5 package jp.sourceforge.posterdivider;
6
7 import java.awt.Canvas;
8 import java.awt.Color;
9 import java.awt.Graphics;
10 import java.awt.image.*;
11
12 public class CanvasPreview extends Canvas {
13         private static final long serialVersionUID = 1L;
14
15         private ImageContainer ic;
16         private PdfGenerator.CreatePosterArgs args;
17
18         @Override
19         public void paint(Graphics g) {
20                 super.paint(g);
21
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);
26
27                 if (ic == null || args == null) {
28                         // 未設定項目がある場合
29                         return;
30                 }
31                 BufferedImage awtImage = ic.getAwtImage();
32
33                 // 左側
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);
41
42                 float srcImageX, srcImageY, srcImageWidth, srcImageHeight, srcImageRate;
43                 if (ic.getImageWidth() * srcBoxHeight > ic.getImageHeight()
44                                 * srcBoxWidth) {
45                         // 横長
46                         srcImageX = srcBoxX;
47                         srcImageWidth = srcBoxWidth;
48                         srcImageRate = srcImageWidth / ic.getImageWidth();
49                         srcImageHeight = ic.getImageHeight() * srcImageRate;
50                         srcImageY = srcBoxY + (srcBoxHeight - srcImageHeight) / 2;
51                 } else {
52                         // 縦長
53                         srcImageY = srcBoxY;
54                         srcImageHeight = srcBoxHeight;
55                         srcImageRate = srcImageHeight / ic.getImageHeight();
56                         srcImageWidth = ic.getImageWidth() * srcImageRate;
57                         srcImageX = srcBoxX + (srcBoxWidth - srcImageWidth) / 2;
58                 }
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
66                                                         .getHeight(), this);
67                 }
68
69                 float srcX = srcImageX + args.getSrcLeft() * srcImageRate;
70                 // srcは左上原点だが、CreatePosterArgsのsrcは左下原点なのでここで解消する
71                 float srcY = srcImageY + (ic.getImageHeight() - args.getSrcTop())
72                                 * srcImageRate;
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) {
80                         // 範囲外の部分を塗りつぶす
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
85                                         - srcTop);
86                         g.fillRect(srcRight, srcTop, srcImageRight - srcRight, srcBottom
87                                         - srcTop);
88                         g.fillRect(srcImageLeft, srcBottom, srcImageRight - srcImageLeft,
89                                         srcImageBottom - srcBottom);
90                 } else {
91                         g.setColor(Color.GRAY);
92                         g.fillRect(srcLeft, srcTop, srcRight - srcLeft, srcBottom - srcTop);
93                 }
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);
99
100                 // 右側
101
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);
109
110                 float posterX, posterY, posterWidth, posterHeight, posterRate;
111                 if (args.getPosterWidth() * posterBoxHeight > args.getPosterHeight()
112                                 * posterBoxWidth) {
113                         // 横長
114                         posterX = posterBoxX;
115                         posterWidth = posterBoxWidth;
116                         posterRate = posterWidth / args.getPosterWidth();
117                         posterHeight = args.getPosterHeight() * posterRate;
118                         posterY = posterBoxY + (posterBoxHeight - posterHeight) / 2;
119                 } else {
120                         // 縦長
121                         posterY = posterBoxY;
122                         posterHeight = posterBoxHeight;
123                         posterRate = posterHeight / args.getPosterHeight();
124                         posterWidth = args.getPosterWidth() * posterRate;
125                         posterX = posterBoxX + (posterBoxWidth - posterWidth) / 2;
126                 }
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);
142                 } else {
143                         g.setColor(Color.GRAY);
144                         g.fillRect(postarLeft, postarTop, postarRight - postarLeft,
145                                         postarBottom - postarTop);
146                 }
147                 g.setColor(Color.BLACK);
148                 g.drawRect(postarLeft, postarTop, postarRight - postarLeft,
149                                 postarBottom - postarTop);
150
151                 int paperCountX = PdfGenerator.simulateOutputPaperCountX(args);
152                 int paperCountY = PdfGenerator.simulateOutputPaperCountY(args);
153
154                 if (paperCountX < 0 || paperCountY < 0) {
155                         // 設定値が不正
156                         return;
157                 }
158
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
164                                 .getMarginRight())
165                                 * posterRate;
166                 float paperTextHeight = (args.getPaperHeight() - args.getMarginTop() - args
167                                 .getMarginBottom())
168                                 * posterRate;
169
170                 float y = paperY;
171                 for (int i = 0; i < paperCountY; i++) {
172                         float x = paperX;
173                         for (int j = 0; j < paperCountX; j++) {
174                                 // 交互に色を変える
175                                 if ((i + j) % 2 == 0) {
176                                         g.setColor(Color.RED);
177                                         g.drawRect((int) x, (int) y, (int) paperWidth,
178                                                         (int) paperHeight);
179                                 } else {
180                                         g.setColor(Color.BLUE);
181                                         g.drawRect((int) x + 1, (int) y + 1, (int) paperWidth,
182                                                         (int) paperHeight);
183                                 }
184                                 x += paperTextWidth;
185                         }
186                         y += paperTextHeight;
187                 }
188         }
189
190         public ImageContainer getImageContainer() {
191                 return ic;
192         }
193
194         public void setImageContainer(ImageContainer ic) {
195                 this.ic = ic;
196         }
197
198         public PdfGenerator.CreatePosterArgs getArgs() {
199                 return this.args;
200         }
201
202         public void setArgs(PdfGenerator.CreatePosterArgs args) {
203                 this.args = args;
204         }
205 }