|
1 | 1 | package org.teachingextensions.logo.utils.MazeUtils; |
2 | 2 |
|
3 | | -/****************************************************************************** |
4 | | - * Compilation: javac StdDraw.java |
5 | | - * Execution: java StdDraw |
6 | | - * Dependencies: none |
7 | | - * |
8 | | - * Standard drawing library. This class provides a basic capability for |
9 | | - * creating drawings with your programs. It uses a simple graphics model that |
10 | | - * allows you to create drawings consisting of points, lines, and curves |
11 | | - * in a window on your computer and to save the drawings to a file. |
12 | | - * |
13 | | - * Todo |
14 | | - * ---- |
15 | | - * - Add support for gradient fill, etc. |
16 | | - * - Fix setCanvasSize() so that it can only be called once. |
17 | | - * - On some systems, drawing a line (or other shape) that extends way |
18 | | - * beyond canvas (e.g., to infinity) dimensions does not get drawn. |
19 | | - * |
20 | | - * Remarks |
21 | | - * ------- |
22 | | - * - don't use AffineTransform for rescaling since it inverts |
23 | | - * images and strings |
24 | | - * |
25 | | - ******************************************************************************/ |
26 | 3 | import java.awt.BasicStroke; |
27 | 4 | import java.awt.Color; |
28 | 5 | import java.awt.FileDialog; |
|
65 | 42 | import javax.swing.KeyStroke; |
66 | 43 |
|
67 | 44 | /** |
68 | | - * The {@code StdDraw} class provides a basic capability for |
69 | | - * creating drawings with your programs. It uses a simple graphics model that |
70 | | - * allows you to create drawings consisting of points, lines, squares, |
71 | | - * circles, and other geometric shapes in a window on your computer and |
72 | | - * to save the drawings to a file. Standard drawing also includes |
73 | | - * facilities for text, color, pictures, and animation, along with |
74 | | - * user interaction via the keyboard and mouse. |
75 | 45 | * <p> |
76 | 46 | * <b>Getting started.</b> |
77 | | - * To use standard drawing, you must have <tt>StdDraw.class</tt> in your |
78 | | - * Java classpath. If you used our autoinstaller, you should be all set. |
79 | | - * Otherwise, download |
80 | | - * <a href = "http://introcs.cs.princeton.edu/java/stdlib/StdDraw.java">StdDraw.java</a> |
81 | | - * and put a copy in your working directory. |
82 | 47 | * <p> |
83 | | - * Now, type the following short program into your editor: |
| 48 | + * Type the following short program into your editor: |
84 | 49 | * <pre> |
85 | 50 | * public class TestStdDraw { |
86 | 51 | * public static void main(String[] args) { |
|
92 | 57 | * } |
93 | 58 | * } |
94 | 59 | * </pre> |
95 | | - * If you compile and execute the program, you should see a window |
96 | | - * appear with a thick magenta line and a blue point. |
97 | | - * This program illustrates the two main types of methods in standard |
98 | | - * drawing—methods that draw geometric shapes and methods that |
99 | | - * control drawing parameters. |
100 | | - * The methods {@code StdDraw.line()} and {@code StdDraw.point()} |
101 | | - * draw lines and points; the methods {@code StdDraw.setPenRadius()} |
102 | | - * and {@code StdDraw.setPenColor()} control the line thickness and color. |
103 | 60 | * <p> |
104 | 61 | * <b>Points and lines.</b> |
105 | 62 | * You can draw points and line segments with the following methods: |
|
442 | 399 | */ |
443 | 400 | public final class StdDraw implements ActionListener, MouseListener, MouseMotionListener, KeyListener |
444 | 401 | { |
445 | | - /** |
446 | | - * The color black. |
447 | | - */ |
448 | 402 | public static final Color BLACK = Color.BLACK; |
449 | | - /** |
450 | | - * The color blue. |
451 | | - */ |
452 | 403 | public static final Color BLUE = Color.BLUE; |
453 | | - /** |
454 | | - * The color cyan. |
455 | | - */ |
456 | 404 | public static final Color CYAN = Color.CYAN; |
457 | | - /** |
458 | | - * The color dark gray. |
459 | | - */ |
460 | 405 | public static final Color DARK_GRAY = Color.DARK_GRAY; |
461 | | - /** |
462 | | - * The color gray. |
463 | | - */ |
464 | 406 | public static final Color GRAY = Color.GRAY; |
465 | | - /** |
466 | | - * The color green. |
467 | | - */ |
468 | 407 | public static final Color GREEN = Color.GREEN; |
469 | | - /** |
470 | | - * The color light gray. |
471 | | - */ |
472 | 408 | public static final Color LIGHT_GRAY = Color.LIGHT_GRAY; |
473 | | - /** |
474 | | - * The color magenta. |
475 | | - */ |
476 | 409 | public static final Color MAGENTA = Color.MAGENTA; |
477 | | - /** |
478 | | - * The color orange. |
479 | | - */ |
480 | 410 | public static final Color ORANGE = Color.ORANGE; |
481 | | - /** |
482 | | - * The color pink. |
483 | | - */ |
484 | 411 | public static final Color PINK = Color.PINK; |
485 | | - /** |
486 | | - * The color red. |
487 | | - */ |
488 | 412 | public static final Color RED = Color.RED; |
489 | | - /** |
490 | | - * The color white. |
491 | | - */ |
492 | 413 | public static final Color WHITE = Color.WHITE; |
493 | | - /** |
494 | | - * The color yellow. |
495 | | - */ |
496 | 414 | public static final Color YELLOW = Color.YELLOW; |
497 | 415 | /** |
498 | 416 | * Shade of blue used in <em>Introduction to Programming in Java</em>. |
@@ -557,15 +475,31 @@ public final class StdDraw implements ActionListener, MouseListener, MouseMotion |
557 | 475 | // time in milliseconds (from currentTimeMillis()) when we can draw again |
558 | 476 | // used to control the frame rate |
559 | 477 | private static long nextDraw = -1; |
560 | | - // singleton pattern: client can't instantiate |
561 | 478 | private StdDraw() |
562 | 479 | { |
563 | 480 | } |
564 | | - // static initializer |
565 | 481 | static |
566 | 482 | { |
567 | 483 | init(); |
568 | 484 | } |
| 485 | + public static void main(String[] args) |
| 486 | + { |
| 487 | + StdDraw.square(.2, .8, .1); |
| 488 | + StdDraw.filledSquare(.8, .8, .2); |
| 489 | + StdDraw.circle(.8, .2, .2); |
| 490 | + StdDraw.setPenColor(StdDraw.BOOK_RED); |
| 491 | + StdDraw.setPenRadius(.02); |
| 492 | + StdDraw.arc(.8, .2, .1, 200, 45); |
| 493 | + StdDraw.setPenRadius(); |
| 494 | + StdDraw.setPenColor(StdDraw.BOOK_BLUE); |
| 495 | + double[] x = {.1, .2, .3, .2}; |
| 496 | + double[] y = {.2, .3, .2, .1}; |
| 497 | + StdDraw.filledPolygon(x, y); |
| 498 | + StdDraw.setPenColor(StdDraw.BLACK); |
| 499 | + StdDraw.text(0.2, 0.5, "black text"); |
| 500 | + StdDraw.setPenColor(StdDraw.WHITE); |
| 501 | + StdDraw.text(0.8, 0.8, "white text"); |
| 502 | + } |
569 | 503 | /** |
570 | 504 | * Sets the canvas (drawing area) to be 512-by-512 pixels. |
571 | 505 | * This also erases the current drawing and resets the coordinate system, |
@@ -1539,7 +1473,6 @@ public static void save(String filename) |
1539 | 1473 | throw new NullPointerException(); |
1540 | 1474 | File file = new File(filename); |
1541 | 1475 | String suffix = filename.substring(filename.lastIndexOf('.') + 1); |
1542 | | - // png files |
1543 | 1476 | if (suffix.toLowerCase().equals("png")) |
1544 | 1477 | { |
1545 | 1478 | try |
@@ -1787,27 +1720,4 @@ public void keyReleased(KeyEvent e) |
1787 | 1720 | keysDown.remove(e.getKeyCode()); |
1788 | 1721 | } |
1789 | 1722 | } |
1790 | | - /** |
1791 | | - * Test client. |
1792 | | - */ |
1793 | | - public static void main(String[] args) |
1794 | | - { |
1795 | | - StdDraw.square(.2, .8, .1); |
1796 | | - StdDraw.filledSquare(.8, .8, .2); |
1797 | | - StdDraw.circle(.8, .2, .2); |
1798 | | - StdDraw.setPenColor(StdDraw.BOOK_RED); |
1799 | | - StdDraw.setPenRadius(.02); |
1800 | | - StdDraw.arc(.8, .2, .1, 200, 45); |
1801 | | - // draw a blue diamond |
1802 | | - StdDraw.setPenRadius(); |
1803 | | - StdDraw.setPenColor(StdDraw.BOOK_BLUE); |
1804 | | - double[] x = {.1, .2, .3, .2}; |
1805 | | - double[] y = {.2, .3, .2, .1}; |
1806 | | - StdDraw.filledPolygon(x, y); |
1807 | | - // text |
1808 | | - StdDraw.setPenColor(StdDraw.BLACK); |
1809 | | - StdDraw.text(0.2, 0.5, "black text"); |
1810 | | - StdDraw.setPenColor(StdDraw.WHITE); |
1811 | | - StdDraw.text(0.8, 0.8, "white text"); |
1812 | | - } |
1813 | 1723 | } |
0 commit comments