summaryrefslogtreecommitdiff
path: root/ws2015
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2015-11-20 19:31:29 +0100
committerGregor Kleen <gkleen@yggdrasil.li>2015-11-20 19:31:29 +0100
commit154ed6744254b0d7553e40071656b53382816123 (patch)
tree130e80ed8e1557b6abe6cae92c9cde37a6fefc2d /ws2015
parente86cb0c8dafc3c5c19d5e1030e969602dcf6f294 (diff)
downloaduni-154ed6744254b0d7553e40071656b53382816123.tar
uni-154ed6744254b0d7553e40071656b53382816123.tar.gz
uni-154ed6744254b0d7553e40071656b53382816123.tar.bz2
uni-154ed6744254b0d7553e40071656b53382816123.tar.xz
uni-154ed6744254b0d7553e40071656b53382816123.zip
fixed last commit
Diffstat (limited to 'ws2015')
-rw-r--r--ws2015/eip/blaetter/05/GraphicsWindow.java397
1 files changed, 397 insertions, 0 deletions
diff --git a/ws2015/eip/blaetter/05/GraphicsWindow.java b/ws2015/eip/blaetter/05/GraphicsWindow.java
new file mode 100644
index 0000000..b92fdaf
--- /dev/null
+++ b/ws2015/eip/blaetter/05/GraphicsWindow.java
@@ -0,0 +1,397 @@
1import java.util.ArrayList;
2import javax.swing.JFrame;
3import javax.swing.JPanel;
4import javax.swing.Timer;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.ActionListener;
8import java.awt.event.MouseAdapter;
9import java.awt.event.WindowAdapter;
10import java.awt.event.WindowEvent;
11import java.awt.event.MouseEvent;
12import java.awt.Graphics;
13import java.awt.Graphics2D;
14import java.awt.Shape;
15import java.awt.geom.RectangularShape;
16import java.awt.geom.GeneralPath;
17import java.awt.Point;
18import java.awt.Label;
19import java.awt.Font;
20import java.awt.Color;
21import java.awt.Rectangle;
22
23/**
24 Eine Klasse zu pädagogischen Zwecken.
25 Erlaubt die Eingabe von Punktkoordinaten
26 mittels Mausklicks, das Zeichnen einfacher
27 2D Objekte (java.awt.Shape), sowie die
28 Ausgabe von Texten in einer Statuszeile.
29 @version 3.042
30 @author Martin Hofmann und die EiP-Teams verschiedener Jahre
31 */
32
33public class GraphicsWindow {
34
35 private int width;
36 private int height;
37 private JFrame dasFenster;
38 private static int fensterZahl;
39 private static int fensterNr;
40 private Label label;
41 private GraphicsWindowPanel panel;
42 private Point mousePos;
43 private Color activeColor = Color.BLACK;
44 final private Color backColor = Color.WHITE;
45 MyMouseAdapter mouseListener;
46
47 /**
48 Erzeugt ein Fenster der Größe 640 auf 480 mit Textausgabe, Mauseingabe und Grafikausgabe.
49 */
50 public GraphicsWindow() {
51 this(640, 480);
52 }
53
54 /**
55 Erzeugt ein Fenster in vorgegebener Größe mit Textausgabe, Mauseingabe und Grafikausgabe.
56 @param width Breite des Fensters
57 @param height Höhe des Fensters
58 */
59 public GraphicsWindow(int width, int height) {
60 this.width = width;
61 this.height = height;
62 dasFenster = new JFrame();
63 dasFenster.setTitle("Grafikfenster " + ++fensterNr);
64 fensterZahl++;
65 dasFenster.setLocationByPlatform(true);
66 dasFenster.setSize(width,height+50);
67 dasFenster.addWindowListener(new WindowAdapter(){
68 public void windowClosing(WindowEvent e) {
69 dasFenster.dispose(); // nicht gleich alle Fenster abschiessen
70 if (--fensterZahl<1) System.exit(0);
71 }
72 });
73
74 label = new Label("Statuszeile...");
75 label.setFont(new Font("Helvetica", Font.PLAIN, 12));
76 dasFenster.getContentPane().add(label,"North" );
77
78 panel = new GraphicsWindowPanel();
79 //panel.setBackground(Color.cyan);
80 panel.addCommand(new SetColor(activeColor));
81 dasFenster.getContentPane().add(panel,"Center");
82 mousePos = new Point();
83 mouseListener = new MyMouseAdapter();
84 panel.addMouseListener(mouseListener);
85 clear();
86 dasFenster.setVisible(true);
87 }
88
89 /**
90 Gibt eine Zeichenkette oben im Fenster aus.
91 @param text diese Zeichenkette
92 */
93 public void setText(String text) {
94 label.setText(text);
95 }
96 /**
97 Liest den oben im Fenster angezeigten Text aus.
98 @return den Text
99 */
100 public String getText() {
101 return label.getText();
102 }
103 /**
104 Wartet auf einen Mausklick. Die Methode blockiert das
105 aufrufende Programm solange bis der Mausklick erfolgt ist.
106 @return die Koordinaten des angeklickten Punkts
107 */
108
109 public Point mouseClick() {
110 try{
111 synchronized(mouseListener){mouseListener.wait();}
112 }
113 catch(InterruptedException e){
114 e.printStackTrace();
115 }
116 return mousePos;
117 }
118
119 class MyMouseAdapter extends MouseAdapter {
120
121 /**
122 Beendet das Warten auf den Mausklick und verwertet die Koordinaten.
123 Diese Methode ist nicht für den Anwender bestimmt.
124 */
125
126 synchronized public void mouseClicked(MouseEvent e){
127 mousePos = e.getPoint();
128 notifyAll();
129 }
130 }
131
132
133 /**
134 Schaltet die Zeichenfarbe auf die Hintergrundfarbe um. Dies ist
135 das Mittel, um gezeichnete Linien wieder zu löschen.
136 */
137 public void switchToBackgroundColor(){
138 activeColor = backColor;
139 panel.addCommand(new SwitchToBackgroundColor(activeColor));
140 panel.repaint();
141 }
142
143 /**
144 Schaltet die Zeichenfarbe auf Schwarz um.
145 */
146 public void switchToForegroundColor(){
147 activeColor = Color.BLACK;
148 panel.addCommand(new SetColor(activeColor));
149 panel.repaint();
150 }
151
152
153 /** Liefert die aktuelle Zeichenfarbe.
154 @return die aktuelle Zeichenfarbe des GraphicsWindow. */
155 public Color getColor() {
156 // return panel.getGraphics().getColor(); // getGraphics() has unpleasant side-effects. :(
157 /* Fixed by adding another instance variable activeColor for now. */
158 return activeColor;
159 }
160
161 /**
162 Zeichnet eine Linie in der aktuellen Zeichenfarbe.
163 @param x Anfangspunkt
164 @param y Endpunkt
165 */
166 public void drawLine(Point x, Point y){
167 // Odering points reduces the amount of graphical artifacts in rendering the same object in different ways
168 Point x1 = x;
169 Point y1 = y;
170 if ((x.x > y.x) || ((x.x == y.x) && (x.y > y.y))) {
171 x1 = y;
172 y1 = x;
173 }
174 panel.addCommand(new DrawLine(x1,y1));
175 panel.repaint();
176 }
177
178 /**
179 Zeichnet einen Punkt in der aktuellen Zeichenfarbe.
180 @param p Punkt
181 */
182 public void drawPoint(Point p){
183 drawLine(p, p);
184 }
185
186 /**
187 Zeichnet einen Punkt in der aktuellen Zeichenfarbe.
188 @param p Punkt
189 */
190 public void drawStringAt(String s, Point p){
191 Command c = new DrawString(s,p);
192 panel.addCommand(c);
193 panel.repaint();
194 }
195
196 /**
197 Zeichnet ein geometrisches Objekt.
198 */
199 public void draw(Shape s) {
200 panel.addCommand(new Draw(s));
201 panel.repaint();
202 }
203
204 /**
205 Füllt ein geometrisches Objekt aus.
206 */
207 public void fill(Shape s) {
208 panel.addCommand(new Fill(s));
209 panel.repaint();
210 }
211
212 /** Das aufrufende Programm wird für ein gegebene Zeitspanne blockiert.
213 @param millis Die Zeitspanne in Millisekunden*/
214 public void sleep(long millis) {
215 try {Thread.sleep(millis);} catch (Exception e){}
216 }
217
218 /** Setzt die Zeichenfarbe. */
219 public void setColor(Color d) {
220 activeColor = d;
221 panel.addCommand(new SetColor(activeColor));
222 panel.repaint();
223 }
224
225 /**
226 Setzt die Zeichenfarbe auf einen Grauwert
227 @param shade Grauwert zwischen 0(schwarz) und 255(weiß)
228 */
229 public void setGrayColor(int shade) {
230 setColor(new Color(shade, shade, shade));
231 }
232
233 /**
234 Setzt die Zeichenfarbe für die Mandelbrot-Aufgabe
235 @param n Anzahl der Iterationen, die durch die Farbe symboliziert werdem soll
236 */
237 public void setMandelColor(int n) {
238 float r = (float) Math.min(1.0,((double) n / 9.0) );
239 float g = (float) Math.min(1.0,((double) n / 99.0) );
240 float b = (float) Math.min(1.0,((double) n / 999.0) );
241 setColor(new Color(r, g, b));
242 }
243
244 /** Löscht das Bild */
245 public void clear() {
246 Color oldActive = activeColor;
247 panel.clearAll();
248 this.switchToBackgroundColor();
249 fill(new Rectangle(0,0,width,height));
250 setColor(oldActive);
251 }
252
253 public void killIn(int secs) {
254 Timer t = new Timer(1000*secs, new ActionListener(){
255 @Override
256 public void actionPerformed(ActionEvent e) {dasFenster.dispose();}
257 }
258 );
259 t.setRepeats(false);
260 t.start();
261 }
262}
263
264
265class GraphicsWindowPanel extends JPanel
266{
267 private static final long serialVersionUID = 1L;
268 private ArrayList<Command> cl = new ArrayList<Command>();
269
270 public void paintComponent(Graphics g)
271 {
272 super.paintComponent(g);
273 Graphics2D g2D = (Graphics2D)g;
274
275 ArrayList<Command> cl = this.cl; // Kopie wegen Nebenläufigkeit von Swing
276 int size = cl.size();
277 for (int i=0; i<size; i++) {
278 Command c = cl.get(i);
279 if (c != null) c.execute(g2D);
280 }
281 }
282
283 void addCommand(Command c)
284 {
285 cl.add(c);
286 }
287
288 void clearAll()
289 {
290// try {
291// SwingUtilities.invokeAndWait(new Runnable() {
292// @Override
293// public void run() {
294 cl = new ArrayList<Command>();
295// }
296// });
297// } catch (InterruptedException e) {
298// // TODO Auto-generated catch block
299// e.printStackTrace();
300// } catch (InvocationTargetException e) {
301// // TODO Auto-generated catch block
302// e.printStackTrace();
303// }
304 }
305}
306
307
308abstract class Command //implements Serializable
309{
310 abstract void execute(Graphics2D g2D);
311
312 /** Clone a shape. This method is needed because Shape
313 * does not define clone(), although many shape classes do.
314 * Kopiert aus jsky-2.6 auf ftp.eso.org */
315 static Shape cloneShape(Shape s) {
316 // FIXME Add more specific shapes
317 if (s instanceof RectangularShape) {
318 return (RectangularShape) ((RectangularShape) s).clone();
319 } else {
320 return new GeneralPath(s);
321 }
322 }
323
324}
325
326class DrawLine extends Command {
327 Point von;
328 Point bis;
329 DrawLine(Point von, Point bis) {
330 /* Clonen der Punkte essentiell um Aliasingeffekte beim Redraw zu verhindern */
331 this.von = new Point(von);
332 this.bis = new Point(bis);
333 }
334 void execute(Graphics2D g2D)
335 {
336 g2D.drawLine(this.von.x,this.von.y,this.bis.x,this.bis.y);
337 }
338}
339
340class SwitchToForegroundColor extends Command {
341 SwitchToForegroundColor() {}
342 void execute(Graphics2D g2D) {
343 g2D.setColor(Color.black);
344 }
345}
346
347class SwitchToBackgroundColor extends Command {
348 Color backcolor;
349 SwitchToBackgroundColor(Color backcolor) {this.backcolor = backcolor;}
350 void execute(Graphics2D g2D) {
351 g2D.setColor(backcolor);
352 }
353}
354
355class SetColor extends Command {
356 Color color;
357 SetColor(Color color) {this.color = color;}
358 void execute(Graphics2D g2D) {
359 g2D.setColor(this.color);
360 }
361}
362
363
364class Draw extends Command {
365 Shape shape;
366 Draw(Shape shape) {this.shape = cloneShape(shape);}
367 void execute(Graphics2D g2D) {
368 g2D.draw(this.shape);
369 }
370}
371
372class Fill extends Command {
373 Shape shape;
374 Fill(Shape shape) {this.shape = cloneShape(shape);}
375 void execute(Graphics2D g2D) {
376 g2D.fill(this.shape);
377 }
378}
379
380class DrawString extends Command {
381 String string;
382 Point position;
383 DrawString(String string, Point position) {this.string = string; this.position = position;}
384 @Override
385 void execute(Graphics2D g2D) {
386 g2D.drawString(string, position.x, position.y);
387 }
388}
389
390
391
392
393
394
395
396
397