/Users/lyon/j4p/src/ip/gui/frames/PaintFrame.java
|
1 package ip.gui.frames;
2
3 import ip.gui.IconComponent;
4 import ip.gui.dialog.DoubleLog;
5 import ip.transforms.Points;
6
7 import java.awt.*;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.MouseEvent;
10 import java.awt.event.MouseListener;
11 import java.awt.event.MouseMotionListener;
12
13 public class PaintFrame extends BoundaryFrame
14 implements MouseListener, MouseMotionListener {
15 private IconFrame iconFrame = new IconFrame();
16 private Menu paintMenu = getMenu("Paint");
17 private MenuItem showIconFrame_mi = addMenuItem(paintMenu, "show paint bar");
18 private MenuItem eraseShapes_mi = addMenuItem(paintMenu, "Erase shapes");
19 private MenuItem resizeFrame_mi = addMenuItem(paintMenu, "[E-R]esize Frame");
20 private int x1, y1, x2, y2;
21 private Point anchor;
22
23 Points userPoints = new Points();
24
25 public void actionPerformed(ActionEvent e) {
26
27 if (match(e, resizeFrame_mi)) {
28 resizeFrame();
29 return;
30 }
31 if (match(e, eraseShapes_mi)) {
32 eraseShapes();
33 return;
34 }
35 if (match(e, showIconFrame_mi)) {
36 showIconFrame();
37 return;
38 }
39 super.actionPerformed(e);
40 }
41
42 public void resizeFrame() {
43 setSize(getImageWidth(), getImageHeight());
44 }
45
46 public void showIconFrame() {
47 Rectangle r = getBounds();
48 Dimension d = r.getSize();
49 iconFrame.setLocation(d.width, d.height);
50 iconFrame.setVisible(true);
51 }
52
53 public void paint(Graphics g) {
54 if (iconFrame != null) iconFrame.setLabels(getImageWidth(), getImageHeight(), red, green, blue);
55 super.paint(g);
56 if (userPoints == null) return;
57 userPoints.drawUserPoints(g);
58 }
59
60 public void eraseShapes() {
61 userPoints = new Points();
62 repaint();
63 }
64
65
66 public PaintFrame(String title) {
67 super(title);
68 getSpatialFilterMenu().add(paintMenu);
69 showIconFrame();
70 addMouseListener(this);
71 addMouseMotionListener(this);
72 }
73
74 public void mousePressed(MouseEvent e) {
75 setP1(e);
76 anchor = new Point(e.getX(), e.getY());
77 }
78
79 public void mouseExited(MouseEvent e) {
80 }
81
82 public void mouseEntered(MouseEvent e) {
83 }
84
85 public void mouseClicked(MouseEvent e) {
86 }
87
88 public void mouseReleased(MouseEvent e) {
89 IconComponent ic = iconFrame.getSelectedIcon();
90 if (ic == iconFrame.getXImageIcon())
91 userPoints.addPoint(new Point(e.getX(), e.getY()));
92 if (ic == iconFrame.getEyeDropperIcon()) {
93 iconFrame.setPosition(x1, y1);
94 getColor();
95 }
96 if (ic == iconFrame.getMarqeeIcon()) {
97 grabFrame(anchor.x, anchor.y, e.getX(), e.getY());
98 }
99 if (ic == iconFrame.getMagnifyingGlassIcon()) magnify();
100 repaint();
101 }
102
103 public void grabFrame(int x1, int y1, int x2, int y2) {
104 int w = Math.abs(x1 - x2);
105 int h = Math.abs(y1 - y2);
106 short _r[][] = new short[w][h];
107 short _g[][] = new short[w][h];
108 short _b[][] = new short[w][h];
109
110 for (int x = x1,ix = 0; x < x2; x++, ix++)
111 for (int y = y1,iy = 0; y < y2; y++, iy++) {
112 _r[ix][iy] = shortImageBean.getR()[x][y];
113 _g[ix][iy] = shortImageBean.getG()[x][y];
114 _b[ix][iy] = shortImageBean.getB()[x][y];
115 }
116 setChild(new TopFrame(
117 "clipped Region",
118 _r, _g, _b));
119 }
120
121 public void magnify() {
122 fishEye2(x1, y1, 1.0, 2);
123 }
124
125 // the old magnify...now private...
126 private void magnify2() {
127 int m = 0;
128 int n = 0;
129 short rs[][] = new short[getImageWidth() * 2][getImageHeight() * 2];
130 short gs[][] = new short[getImageWidth() * 2][getImageHeight() * 2];
131 short bs[][] = new short[getImageWidth() * 2][getImageHeight() * 2];
132
133 for (int i = 0; i < getImageWidth(); i++) {
134 for (int j = 0; j < getImageHeight(); j++) {
135 rs[m][n] = shortImageBean.getR()[i][j];
136 gs[m][n] = shortImageBean.getG()[i][j];
137 bs[m][n] = shortImageBean.getB()[i][j];
138 rs[m + 1][n] = shortImageBean.getR()[i][j];
139 gs[m + 1][n] = shortImageBean.getG()[i][j];
140 bs[m + 1][n] = shortImageBean.getB()[i][j];
141 rs[m][n + 1] = shortImageBean.getR()[i][j];
142 gs[m][n + 1] = shortImageBean.getG()[i][j];
143 bs[m][n + 1] = shortImageBean.getB()[i][j];
144 rs[m + 1][n + 1] = shortImageBean.getR()[i][j];
145 gs[m + 1][n + 1] = shortImageBean.getG()[i][j];
146 bs[m + 1][n + 1] = shortImageBean.getB()[i][j];
147 n += 2;
148 }
149 m += 2;
150 n = 0;
151 }
152 shortImageBean.setR(rs);
153 setG(gs);
154 setB(bs);
155 short2Image();
156 resizeFrame();
157 }
158
159 private int getX(MouseEvent e) {
160 return (int) (e.getX() * getImageWidth() / getSize().width);
161 }
162
163 private int getY(MouseEvent e) {
164 return (int) (e.getY() * getImageHeight() / getSize().height);
165 }
166
167 private Point scalePoint(Point p) {
168 return
169 new Point(p.x * getSize().width / getImageWidth(),
170 p.y = p.y * getSize().height / getImageHeight());
171 }
172
173 private void clearPel(int x, int y) {
174 shortImageBean.getR()[x][y] = 0;
175 shortImageBean.getG()[x][y] = 0;
176 shortImageBean.getB()[x][y] = 0;
177 }
178
179 public void erasePoint() {
180 clearPel(x1, y1);
181 short2Image();
182 }
183
184 private void getColor() {
185 red = shortImageBean.getR()[x1][y1];
186 green = shortImageBean.getG()[x1][y1];
187 blue = shortImageBean.getB()[x1][y1];
188 }
189
190 private short red = 0;
191 private short green = 0;
192 private short blue = 0;
193 private DoubleLog fishLog = null;
194
195 private void initFishLog() {
196 String title = "Fisheye Dialog";
197 int fieldSize = 6;
198 String prompts[] = {
199 "gamma",
200 "radius"};
201 String defaults[] = {
202 "2.1",
203 "32"};
204 fishLog = new
205 DoubleLog(this, title, prompts, defaults, fieldSize);
206 fishLog.setVisible(true);
207 }
208
209 public void handPoint() {
210 if (fishLog == null) initFishLog();
211 double d[] = fishLog.getUserInputAsDouble();
212 fishEye2(x1, y1, d[0], d[1]);
213 }
214
215 // The following can be accelerated by precomputing
216 // stuff. In the choice between clarity and speed,
217 // I choose clarity!
218 public void fishEye2(int xc, int yc, double gamma, double R) {
219 int w = getImageWidth();
220 int h = getImageHeight();
221 short rn[][] = new short[getImageWidth()][getImageHeight()];
222 short gn[][] = new short[getImageWidth()][getImageHeight()];
223 short bn[][] = new short[getImageWidth()][getImageHeight()];
224 double p[] = new double[2];
225 int red, green, blue;
226 int xp, yp, i, j;
227 for (int x = 0; x < w; x++)
228 for (int y = 0; y < h; y++) {
229 double dx = x - xc;
230 double dy = y - yc;
231 double radius = Math.sqrt(dx * dx + dy * dy);
232 // From [Holzmann] pp. 60
233 double u = Math.pow(radius, gamma) / R;
234 double a = Math.atan2(dy, dx);
235
236 p[0] = u * Math.cos(a);
237 p[1] = u * Math.sin(a);
238 xp = (int) p[0] + xc;
239 yp = (int) p[1] + yc;
240 if ((xp < w) && (yp < h) && (xp >= 0) && (yp >= 0)) {
241 rn[x][y] = shortImageBean.getR()[xp][yp];
242 gn[x][y] = shortImageBean.getG()[xp][yp];
243 bn[x][y] = shortImageBean.getB()[xp][yp];
244 }
245 }
246 shortImageBean.setR(rn);
247 setG(gn);
248 setB(bn);
249 short2Image();
250 }
251
252 public void pencilPoint() {
253 setColor(x1, y1);
254 short2Image();
255 }
256
257 public void brushPoint() {
258 setColor(x1, y1);
259 setColor(x1 + 1, y1 + 1);
260 setColor(x1 - 1, y1 - 1);
261 setColor(x1 - 1, y1 + 1);
262 setColor(x1 + 1, y1 - 1);
263 short2Image();
264 }
265
266 private void setColor(int x, int y) {
267 shortImageBean.getR()[x][y] = red;
268 shortImageBean.getG()[x][y] = green;
269 shortImageBean.getB()[x][y] = blue;
270 }
271
272 public void mouseDragged(MouseEvent e) {
273 e.consume();
274 IconComponent ic = iconFrame.getSelectedIcon();
275 if (ic == iconFrame.getEraserIcon()) erasePoint();
276 if (ic == iconFrame.getBrushIcon()) brushPoint();
277 if (ic == iconFrame.getPencilIcon()) pencilPoint();
278 setP1(e);
279 if (ic == iconFrame.getHandIcon()) handPoint();
280
281 repaint();
282 }
283
284 private void setP1(MouseEvent e) {
285 x1 = getX(e);
286 y1 = getY(e);
287 }
288
289 public void mouseMoved(MouseEvent e) {
290 }
291
292 public static void main(String args[]) {
293 PaintFrame pf = new PaintFrame("Paint Frame");
294 pf.show();
295 }
296
297
298 }