/Users/lyon/j4p/src/j2d/rotator/AffineImageController.java
|
1 package j2d.rotator;
2
3 import futils.WriterUtil;
4 import gui.ClosableJFrame;
5 import gui.run.RunCheckBoxMenuItem;
6 import gui.run.RunMenu;
7 import gui.run.RunMenuItem;
8 import gui.run.RunSlider;
9 import ip.ppm.WritePPM;
10 import j2d.ShortImageBean;
11
12 import javax.swing.*;
13 import java.awt.*;
14 import java.awt.event.KeyEvent;
15 import java.awt.event.MouseEvent;
16 import java.awt.event.MouseListener;
17 import java.awt.geom.AffineTransform;
18
19 /**
20 * Created by IntelliJ IDEA.
21 * User: David Pavlik
22 * Date: May 8, 2004
23 * Time: 1:48:30 PM
24 */
25 public class AffineImageController
26 implements MouseListener {
27 private final AffineMediator affineMediator = new AffineMediator();
28 private ClosableJFrame imageFrame ;
29 private boolean rotateAboutMouse = false;
30 private JPanel labelPanel = getLabelPanel();
31 private JPanel sliderPanel = getSliderPanel();
32 private RunSlider translateX, translateY,
33 rotate, scaleX, scaleY, shearX, shearY;
34
35
36 public AffineImageController() {
37 affineMediator.getNewImage();
38 affineMediator.imagePanel.addMouseListener(this);
39 displayControlFrame();
40 imageFrame = getDisplayFrame();
41 }
42
43 private void displayControlFrame() {
44 Container c = affineMediator.controlFrame.getContentPane();
45 c.setLayout(new BorderLayout());
46 affineMediator.controlFrame.setSize(350, 400);
47 c.add(labelPanel, BorderLayout.WEST);
48 c.add(sliderPanel, BorderLayout.CENTER);
49 affineMediator.controlFrame.show();
50 }
51
52 private ClosableJFrame getDisplayFrame() {
53 ClosableJFrame imageFrame = new ClosableJFrame();
54 Container c = imageFrame.getContentPane();
55 c.setLayout(new BorderLayout());
56 c.add(getMenuBar(), BorderLayout.NORTH);
57 c.add(affineMediator.imagePanel, BorderLayout.CENTER);
58 int w = affineMediator.image.getWidth(affineMediator.imagePanel);
59 int h = affineMediator.image.getHeight(affineMediator.imagePanel);
60 imageFrame.setSize(w * 2, h * 2);
61 imageFrame.show();
62 return imageFrame;
63 }
64
65 private JMenuBar getMenuBar() {
66 JMenuBar mb = new JMenuBar();
67 mb.add(getFileMenu());
68 mb.add(getAffineMenu());
69
70 return mb;
71 }
72
73 private RunMenu getFileMenu() {
74 RunMenu rm = new RunMenu("[File");
75 rm.add(new RunMenuItem("[Open... {ctrl O}") {
76 public void run() {
77 imageFrame.dispose();
78 initializeSettings();
79 affineMediator.getNewImage();
80 imageFrame = getDisplayFrame();
81 }
82 });
83 JMenu saveAs = new JMenu("Save as... ");
84 saveAs.setMnemonic(KeyEvent.VK_S);
85 rm.add(saveAs);
86
87 saveAs.add(new RunMenuItem("[GIF...(not 24 bit!)") {
88 public void run() {
89 saveAsGif();
90 }
91 });
92 saveAs.add(new RunMenuItem("[PPM...") {
93 public void run() {
94 saveAsPPM();
95 }
96 });
97 rm.add(new RunMenuItem("[Exit {ctrl E}") {
98 public void run() {
99 System.exit(0);
100 }
101 });
102
103 return rm;
104 }
105
106 private void initializeSettings() {
107 //setSlidersToZero();
108 affineMediator.priorTransform = new AffineTransform();
109 }
110
111 private void saveAsGif() {
112 String fn = WriterUtil.getSaveFileName("Save as GIF");
113 if (fn == null) return;
114 saveAsGif(fn);
115 }
116
117 private void saveAsGif(String fn) {
118 try {
119 ip.vs.WriteGIF.DoIt(affineMediator.image, fn + ".gif");
120 } catch (Exception e) {
121 e.printStackTrace();
122 }
123 }
124
125 private void saveAsPPM() {
126 String fn = WriterUtil.getSaveFileName("Save as PPM");
127 if (fn == null) return;
128 saveAsPPM(fn);
129 }
130
131 private void saveAsPPM(String fn) {
132 ShortImageBean shortImageBean = new ShortImageBean();
133 shortImageBean.setImage(affineMediator.image);
134 WritePPM.doIt(shortImageBean.getR(), shortImageBean.getG(), shortImageBean.getB(), fn + ".ppm");
135 }
136
137 private RunMenu getAffineMenu() {
138 RunMenu rm = new RunMenu("[Affine Menu");
139 rm.add(new RunCheckBoxMenuItem("[Rotate About Mouse {ctrl R}") {
140 public void run() {
141 rotateAboutMouse = super.getState();
142 if (!rotateAboutMouse) {
143 //Finalize the location of the lastTransform and make available as priorTransform
144 final AffineTransform fixedLastTransform = affineMediator.lastTransform;
145 affineMediator.priorTransform = fixedLastTransform;
146 //Set the mousePoint to the image center going through the priorTransform
147 affineMediator.mousePoint = affineMediator.priorTransform.transform(
148 new Point(affineMediator.image.getWidth(affineMediator.imagePanel) / 2, affineMediator.image.getHeight(affineMediator.imagePanel) / 2), null);
149 //setSlidersToZero();
150 }
151 }
152 });
153
154 return rm;
155 }
156
157 public JPanel getLabelPanel() {
158 JPanel jp = new JPanel();
159 jp.setLayout(new GridLayout(0, 1));
160 //Labels are added to the panel in the order they occur in the vector
161 JLabel sliderLabels[] = affineMediator.affineBean.getSliderLabels();
162 for (int i = 0; i < 7; i++) {
163
164 jp.add(sliderLabels[i]);
165 }
166
167 return jp;
168 }
169
170 private JPanel getSliderPanel() {
171 RunSlider sliders[] = getInitialSliders(this);
172 JPanel jp = new JPanel();
173 jp.setLayout(new GridLayout(0, 1));
174 for (int i = 0; i < 7; i++) {
175 jp.add(sliders[i]);
176 }
177 return jp;
178 }
179
180 private static RunSlider[] getInitialSliders(
181 final AffineImageController affineImageController) {
182
183 affineImageController.translateX = new RunSlider(-300, 300, 0) {
184 public void run() {
185 affineImageController.translateX(getValue());
186 }
187 };
188 affineImageController.translateY = new RunSlider(-300, 300, 0) {
189 public void run() {
190 affineImageController.translateY(getValue());
191 }
192 };
193 affineImageController.rotate = new RunSlider(-180, 180, 0) {
194 public void run() {
195 affineImageController.rotate(getValue());
196 }
197 };
198 affineImageController.scaleX = new RunSlider(0, 200, 100) {
199 public void run() {
200 affineImageController.scaleX(getValue() / 100.0);
201 }
202 };
203 affineImageController.scaleY = new RunSlider(0, 200, 100) {
204 public void run() {
205 affineImageController.scaleY(getValue() / 100.0);
206 }
207 };
208 affineImageController.shearX = new RunSlider(-400, 400, 0) {
209 public void run() {
210 affineImageController.shearX(getValue() / 100.0);
211 }
212 };
213 affineImageController.shearY = new RunSlider(-400, 400, 0) {
214 public void run() {
215 affineImageController.shearY(getValue() / 100.0);
216 }
217 };
218
219
220 RunSlider rs[] = {
221 affineImageController.translateX, affineImageController.translateY,
222 affineImageController.rotate,
223 affineImageController.scaleX, affineImageController.scaleY,
224 affineImageController.shearX, affineImageController.shearY
225 };
226
227 return rs;
228 }
229
230
231
232 private void translateX(int x) {
233 affineMediator.location.setLocation(x, affineMediator.location.getY());
234 affineMediator.imagePanel.repaint();
235 }
236
237 private void translateY(int y) {
238 affineMediator.location.setLocation(affineMediator.location.getX(), y);
239 affineMediator.imagePanel.repaint();
240 }
241
242 private void scaleX(double sx) {
243 affineMediator.affineBean.getScale().setLocation(sx, affineMediator.affineBean.getScale().getY());
244 affineMediator.imagePanel.repaint();
245 }
246
247 private void scaleY(double sy) {
248 affineMediator.affineBean.getScale().setLocation(
249 affineMediator.affineBean.getScale().getX(), sy);
250 affineMediator.imagePanel.repaint();
251 }
252
253 private void shearX(double sx) {
254 affineMediator.affineBean.getShear().setLocation(sx, affineMediator.affineBean.getShear().getY());
255 affineMediator.imagePanel.repaint();
256 }
257
258 private void shearY(double sy) {
259 affineMediator.affineBean.getShear().setLocation(
260 affineMediator.affineBean.getShear().getX(), sy);
261 affineMediator.imagePanel.repaint();
262 }
263
264 private void rotate(double radians) {
265 affineMediator.affineBean.setTheta(radians);
266 affineMediator.imagePanel.repaint();
267 }
268
269 public void mouseClicked(MouseEvent e) {
270 if (!rotateAboutMouse) return;
271 //Finalize the location of the lastTransform and make available as priorTransform
272 final AffineTransform fixedPriorTransform = affineMediator.lastTransform;
273 affineMediator.priorTransform = fixedPriorTransform;
274
275 affineMediator.mousePoint = e.getPoint();
276 //setSlidersToZero();
277 affineMediator.imagePanel.repaint();
278 }
279
280 public void mousePressed(MouseEvent e) {
281 }
282
283 public void mouseReleased(MouseEvent e) {
284 }
285
286 public void mouseEntered(MouseEvent e) {
287 }
288
289 public void mouseExited(MouseEvent e) {
290 }
291
292 }
293
294