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