diff options
| author | Gregor Kleen <gkleen@yggdrasil.li> | 2015-11-26 05:04:01 +0000 |
|---|---|---|
| committer | Gregor Kleen <gkleen@yggdrasil.li> | 2015-11-26 05:04:01 +0000 |
| commit | 71baf46654a260fd9edf86e608707542282fc68a (patch) | |
| tree | 660e68a2dd45da909d9d73af4341186d87e70a95 | |
| parent | 1f6f843dd3d565b580698904f488a4739e4afa4d (diff) | |
| download | uni-71baf46654a260fd9edf86e608707542282fc68a.tar uni-71baf46654a260fd9edf86e608707542282fc68a.tar.gz uni-71baf46654a260fd9edf86e608707542282fc68a.tar.bz2 uni-71baf46654a260fd9edf86e608707542282fc68a.tar.xz uni-71baf46654a260fd9edf86e608707542282fc68a.zip | |
More links
| -rw-r--r-- | ws2015/eip/Position.java | 30 | ||||
| l---------[-rw-r--r--] | ws2015/eip/blaetter/04/GraphicsWindow.java | 394 | ||||
| l---------[-rw-r--r--] | ws2015/eip/blaetter/05/GraphicsWindow.java | 394 | ||||
| l---------[-rw-r--r--] | ws2015/eip/blaetter/06/GraphicsWindow.java | 394 | ||||
| l--------- | ws2015/eip/blaetter/06/Position.java | 1 |
5 files changed, 34 insertions, 1179 deletions
diff --git a/ws2015/eip/Position.java b/ws2015/eip/Position.java new file mode 100644 index 0000000..9fa5a23 --- /dev/null +++ b/ws2015/eip/Position.java | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | public class Position { | ||
| 2 | private final int x; | ||
| 3 | private final int y; | ||
| 4 | |||
| 5 | public Position(int x, int y) { | ||
| 6 | this.x = x; | ||
| 7 | this.y = y; | ||
| 8 | } | ||
| 9 | |||
| 10 | public int getX(){ | ||
| 11 | return this.x; | ||
| 12 | } | ||
| 13 | |||
| 14 | public int getY(){ | ||
| 15 | return this.y; | ||
| 16 | } | ||
| 17 | |||
| 18 | public Position translate(int dx, int dy){ | ||
| 19 | return new Position(this.x + dx, this.y + dy); | ||
| 20 | } | ||
| 21 | |||
| 22 | double distance(Position other) { | ||
| 23 | return Math.sqrt(Math.pow(this.x - other.x, 2) + Math.pow(this.y - other.y, 2)); | ||
| 24 | } | ||
| 25 | |||
| 26 | boolean istGleichZu(Position other) { | ||
| 27 | return this.x==other.x && this.y==other.y; | ||
| 28 | } | ||
| 29 | |||
| 30 | } | ||
diff --git a/ws2015/eip/blaetter/04/GraphicsWindow.java b/ws2015/eip/blaetter/04/GraphicsWindow.java index 2bb652d..0107b94 100644..120000 --- a/ws2015/eip/blaetter/04/GraphicsWindow.java +++ b/ws2015/eip/blaetter/04/GraphicsWindow.java | |||
| @@ -1,393 +1 @@ | |||
| 1 | package gameoflifetest; | ../../GraphicsWindow.java \ No newline at end of file | |
| 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/05/GraphicsWindow.java b/ws2015/eip/blaetter/05/GraphicsWindow.java index 2bb652d..0107b94 100644..120000 --- a/ws2015/eip/blaetter/05/GraphicsWindow.java +++ b/ws2015/eip/blaetter/05/GraphicsWindow.java | |||
| @@ -1,393 +1 @@ | |||
| 1 | package gameoflifetest; | ../../GraphicsWindow.java \ No newline at end of file | |
| 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/06/GraphicsWindow.java b/ws2015/eip/blaetter/06/GraphicsWindow.java index 2bb652d..0107b94 100644..120000 --- a/ws2015/eip/blaetter/06/GraphicsWindow.java +++ b/ws2015/eip/blaetter/06/GraphicsWindow.java | |||
| @@ -1,393 +1 @@ | |||
| 1 | package gameoflifetest; | ../../GraphicsWindow.java \ No newline at end of file | |
| 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/06/Position.java b/ws2015/eip/blaetter/06/Position.java new file mode 120000 index 0000000..aa3378e --- /dev/null +++ b/ws2015/eip/blaetter/06/Position.java | |||
| @@ -0,0 +1 @@ | |||
| ../../Position.java \ No newline at end of file | |||
