|
1 | 1 | package org.teachingextensions.WindowUtils; |
2 | 2 |
|
3 | | -import java.awt.BasicStroke; |
4 | | -import java.awt.Cursor; |
5 | | -import java.awt.Graphics; |
6 | | -import java.awt.Graphics2D; |
7 | | -import java.awt.Image; |
8 | | -import java.awt.RenderingHints; |
9 | | -import java.awt.geom.AffineTransform; |
10 | | -import java.awt.geom.Line2D; |
| 3 | +import org.teachingextensions.logo.Turtle; |
| 4 | +import org.teachingextensions.logo.Turtle.Animals; |
| 5 | +import org.teachingextensions.logo.utils.EventUtils.MouseLeftClickListener; |
| 6 | +import org.teachingextensions.logo.utils.EventUtils.MouseRightClickListener; |
| 7 | +import org.teachingextensions.logo.utils.InterfaceUtils.TurtlePainter; |
| 8 | +import org.teachingextensions.logo.utils.InterfaceUtils.TurtleTrailPainter; |
| 9 | +import org.teachingextensions.logo.utils.InterfaceUtils.TurtleWindow; |
| 10 | +import org.teachingextensions.logo.utils.LineAndShapeUtils.Paintable; |
| 11 | + |
| 12 | +import javax.swing.*; |
| 13 | +import java.awt.*; |
11 | 14 | import java.net.URL; |
| 15 | +import java.util.ArrayList; |
12 | 16 |
|
13 | | -import javax.swing.ImageIcon; |
| 17 | +public class TurtlePanel { |
| 18 | + private Turtle turtle; |
| 19 | + private Image image; |
| 20 | + private TurtleWindow window; |
| 21 | + private TurtleTrailPainter trailPainter; |
| 22 | + private TurtlePainter turtlePainter; |
14 | 23 |
|
15 | | -import org.teachingextensions.logo.Turtle; |
16 | | -import org.teachingextensions.logo.Turtle.Animals; |
17 | | -import org.teachingextensions.logo.utils.LineAndShapeUtils.LineSegment; |
18 | | - |
19 | | -public class TurtlePanel extends ProgramWindow |
20 | | -{ |
21 | | - private static final long serialVersionUID = 3272676059303477850L; |
22 | | - private Turtle turtle; |
23 | | - private Image image; |
24 | | - private Animals animal; |
25 | | - public TurtlePanel() |
26 | | - { |
27 | | - } |
28 | | - public TurtlePanel(String string) |
29 | | - { |
30 | | - super(string); |
31 | | - } |
32 | | - public void setTurtle(Turtle turtle) |
33 | | - { |
| 24 | + public TurtlePanel() { |
| 25 | + this(null, null); |
| 26 | + } |
| 27 | + |
| 28 | + public TurtlePanel(String title) { |
| 29 | + this(title, null); |
| 30 | + } |
| 31 | + |
| 32 | + public TurtlePanel(String title, Turtle turtle) { |
| 33 | + this.window = new TurtleWindow(title); |
| 34 | + configureWindow(turtle); |
34 | 35 | this.turtle = turtle; |
35 | 36 | } |
36 | | - @Override |
37 | | - public void paint(Graphics g) |
38 | | - { |
39 | | - super.paint(g); |
40 | | - paintLines((Graphics2D) g); |
41 | | - paintTurtle((Graphics2D) g); |
42 | | - } |
43 | | - private void paintLines(Graphics2D g) |
44 | | - { |
45 | | - if (turtle == null) { return; } |
46 | | - g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
47 | | - g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); |
48 | | - g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); |
49 | | - g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); |
50 | | - for (LineSegment l : turtle.getTrail()) |
51 | | - { |
52 | | - if (l != null) |
53 | | - { |
54 | | - g.setColor(l.getColor()); |
55 | | - g.setStroke(new BasicStroke(l.getWidth(), BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)); |
56 | | - g.draw(new Line2D.Double(l.getStart().x, l.getStart().y, l.getEnd().x, l.getEnd().y)); |
57 | | - } |
58 | | - } |
| 37 | + |
| 38 | + public void setTurtle(Turtle turtle) { |
| 39 | + clearWindow(); |
| 40 | + configureWindow(turtle); |
| 41 | + this.turtle = turtle; |
59 | 42 | } |
60 | | - private void paintTurtle(Graphics2D g) |
61 | | - { |
62 | | - if (turtle == null || turtle.isHidden()) { return; } |
63 | | - Image image = getImage(); |
64 | | - int xCenter = image.getWidth(null) / 2; |
65 | | - int yCenter = image.getHeight(null) / 2; |
66 | | - int x = turtle.getX() - xCenter; |
67 | | - int y = turtle.getY() - yCenter; |
68 | | - AffineTransform rotate = AffineTransform.getRotateInstance(Math.toRadians(turtle.getHeadingInDegrees()), |
69 | | - xCenter, yCenter); |
70 | | - AffineTransform move = AffineTransform.getTranslateInstance(x, y); |
71 | | - move.concatenate(rotate); |
72 | | - g.drawImage(image, move, null); |
73 | | - } |
74 | | - public synchronized Image getImage() |
75 | | - { |
76 | | - if (image == null) |
77 | | - { |
| 43 | + |
| 44 | + public synchronized Image getImage() { |
| 45 | + if (image == null) { |
78 | 46 | setAnimal(Animals.Turtle); |
79 | 47 | } |
80 | 48 | return image; |
81 | 49 | } |
82 | | - public synchronized Animals getAnimal() |
83 | | - { |
84 | | - if (animal == null) |
85 | | - { |
86 | | - setAnimal(Animals.Turtle); |
87 | | - } |
88 | | - return animal; |
89 | | - } |
90 | | - public synchronized void setAnimal(Animals animal) |
91 | | - { |
92 | | - this.animal = animal; |
| 50 | + |
| 51 | + public synchronized void setAnimal(Animals animal) { |
93 | 52 | String name = animal + ".png"; |
94 | 53 | URL resource = this.getClass().getResource(name); |
95 | | - if (resource == null) |
96 | | - { |
| 54 | + if (resource == null) { |
97 | 55 | resource = this.getClass().getClassLoader().getResource(name); |
98 | 56 | } |
99 | | - if (resource == null) { throw new IllegalStateException("Could not find animal: " + name); } |
100 | | - image = new ImageIcon(resource).getImage(); |
| 57 | + if (resource == null) { |
| 58 | + throw new IllegalStateException("Could not find animal: " + name); |
| 59 | + } |
| 60 | + this.image = new ImageIcon(resource).getImage(); |
| 61 | + clearWindow(); |
| 62 | + configureWindow(this.turtle); |
101 | 63 | } |
102 | | - public void setCursor(int cursor) |
103 | | - { |
104 | | - this.setCursor(Cursor.getPredefinedCursor(cursor)); |
| 64 | + |
| 65 | + public void setCursor(int cursor) { |
| 66 | + this.window.setCursor(Cursor.getPredefinedCursor(cursor)); |
105 | 67 | } |
106 | | - public void ___() |
107 | | - { |
| 68 | + |
| 69 | + public void ___() { |
108 | 70 | // blank for the DeepDive |
109 | 71 | } |
| 72 | + |
| 73 | + protected void clearWindow() { |
| 74 | + this.window.remove(this.trailPainter) |
| 75 | + .remove(this.turtlePainter); |
| 76 | + } |
| 77 | + |
| 78 | + protected void configureWindow(Turtle turtle) { |
| 79 | + this.trailPainter = new TurtleTrailPainter(turtle); |
| 80 | + this.turtlePainter = new TurtlePainter(turtle, this.getImage()); |
| 81 | + this.window.add(this.trailPainter) |
| 82 | + .add(this.turtlePainter); |
| 83 | + } |
| 84 | + |
| 85 | + public TurtleWindow getWindow() { |
| 86 | + return window; |
| 87 | + } |
| 88 | + |
| 89 | + public void setColor(Color color) { |
| 90 | + this.getWindow().setColor(color); |
| 91 | + } |
| 92 | + |
| 93 | + public void addButton(JButton leftButton) { |
| 94 | + this.getWindow().addButton(leftButton); |
| 95 | + } |
| 96 | + |
| 97 | + public void setBackground(Color color) { |
| 98 | + this.getWindow().setBackground(color); |
| 99 | + } |
| 100 | + |
| 101 | + public void addMouseLeftClickListener(MouseLeftClickListener listener) { |
| 102 | + this.getWindow().addMouseLeftClickListener(listener); |
| 103 | + } |
| 104 | + |
| 105 | + public void addMouseRightClickListener(MouseRightClickListener listener) { |
| 106 | + this.getWindow().addMouseRightClickListener(listener); |
| 107 | + } |
| 108 | + |
| 109 | + public void setBackgroundImage(String picture) { |
| 110 | + this.getWindow().setBackgroundImage(picture); |
| 111 | + } |
| 112 | + |
| 113 | + public ArrayList<Paintable> getAdditional() { |
| 114 | + return this.window.additional; |
| 115 | + } |
110 | 116 | } |
| 117 | + |
0 commit comments