Kr Younger 6 gadus atpakaļ
vecāks
revīzija
d5b82f1440

Canvas.java → Picture/Canvas.java Parādīt failu


Circle.java → Picture/Circle.java Parādīt failu


+ 86
- 0
Picture/Picture.java Parādīt failu

@@ -0,0 +1,86 @@
1
+
2
+/**
3
+ * This class represents a simple picture. You can draw the picture using
4
+ * the draw method. But wait, there's more: being an electronic picture, it
5
+ * can be changed. You can set it to black-and-white display and back to
6
+ * colors (only after it's been drawn, of course).
7
+ *
8
+ * This class was written as an early example for teaching Java with BlueJ.
9
+ * 
10
+ * @author  Michael Kölling and David J. Barnes
11
+ * @version 1.1  (24 May 2001)
12
+ */
13
+public class Picture
14
+{
15
+    private Square wall;
16
+    private Square window;
17
+    private Triangle roof;
18
+    private Circle sun;
19
+
20
+    /**
21
+     * Constructor for objects of class Picture
22
+     */
23
+    public Picture()
24
+    {
25
+        // nothing to do... instance variables are automatically set to null
26
+    }
27
+
28
+    /**
29
+     * Draw this picture.
30
+     */
31
+    public void draw()
32
+    {
33
+        wall = new Square();
34
+        wall.moveVertical(80);
35
+        wall.changeSize(100);
36
+        wall.makeVisible();
37
+
38
+        window = new Square();
39
+        window.changeColor("black");
40
+        window.moveHorizontal(20);
41
+        window.moveVertical(100);
42
+        window.makeVisible();
43
+
44
+        roof = new Triangle();
45
+        roof.changeSize(50, 140);
46
+        roof.moveHorizontal(60);
47
+        roof.moveVertical(70);
48
+        roof.makeVisible();
49
+
50
+        sun = new Circle();
51
+        sun.changeColor("yellow");
52
+        sun.moveHorizontal(180);
53
+        sun.moveVertical(-10);
54
+        sun.changeSize(60);
55
+        sun.makeVisible();
56
+    }
57
+
58
+    /**
59
+     * Change this picture to black/white display
60
+     */
61
+    public void setBlackAndWhite()
62
+    {
63
+        if(wall != null)   // only if it's painted already...
64
+        {
65
+            wall.changeColor("black");
66
+            window.changeColor("white");
67
+            roof.changeColor("black");
68
+            sun.changeColor("black");
69
+        }
70
+    }
71
+
72
+    /**
73
+     * Change this picture to use color display
74
+     */
75
+    public void setColor()
76
+    {
77
+        if(wall != null)   // only if it's painted already...
78
+        {
79
+            wall.changeColor("red");
80
+            window.changeColor("black");
81
+            roof.changeColor("green");
82
+            sun.changeColor("yellow");
83
+        }
84
+    }
85
+
86
+}

+ 44
- 0
Picture/README.TXT Parādīt failu

@@ -0,0 +1,44 @@
1
+Project "picture"
2
+Authors: Michael Kölling and David J. Barnes
3
+
4
+This project is part of the material for the book
5
+
6
+   Objects First with Java - A Practical Introduction using BlueJ
7
+   David J. Barnes and Michael Kolling
8
+   Pearson Education, 2002
9
+
10
+It is discussed in chapter 1.
11
+    
12
+This is a very simple project to demonstrate some characteristics of
13
+objects.
14
+
15
+You can create various shapes, and you will see, if you do, that those
16
+shapes are drawn on screen (in a window that we call the "canvas").
17
+
18
+You can then manipulate these objects: change their position, size and 
19
+colour. Try it out: create a few different squares, triangles and circles.
20
+
21
+This project is designed as a first example of object-oriented programming.
22
+It illustrates a number of concepts:
23
+
24
+ - a Java project (application) is a collection of classes
25
+ - objects can be created from classes
26
+ - from any one class, many objects may be created
27
+ - objects have operations (methods)
28
+ - operations can have parameters
29
+ - parameters have types (at least String and int)
30
+ - objects hold data (fields)
31
+ - the operations and fields are common to all objects
32
+ - the values stored in the fields can be different for each object
33
+
34
+The project also demonstrates
35
+
36
+ - BlueJ object creation
37
+ - interactive method invocation
38
+ - parameter passing
39
+
40
+A good second project to look at after this is "picture", which adds a class
41
+to those ones in this project. That class (named "Picture") uses the shapes
42
+to draw a picture. It can be used to experiment with coding.
43
+
44
+Michael Kölling, July 2000

Square.java → Picture/Square.java Parādīt failu


Triangle.java → Picture/Triangle.java Parādīt failu


+ 78
- 0
Picture/package.bluej Parādīt failu

@@ -0,0 +1,78 @@
1
+#BlueJ package file
2
+dependency1.from=Circle
3
+dependency1.to=Canvas
4
+dependency1.type=UsesDependency
5
+dependency2.from=Picture
6
+dependency2.to=Square
7
+dependency2.type=UsesDependency
8
+dependency3.from=Picture
9
+dependency3.to=Triangle
10
+dependency3.type=UsesDependency
11
+dependency4.from=Picture
12
+dependency4.to=Circle
13
+dependency4.type=UsesDependency
14
+dependency5.from=Triangle
15
+dependency5.to=Canvas
16
+dependency5.type=UsesDependency
17
+dependency6.from=Square
18
+dependency6.to=Canvas
19
+dependency6.type=UsesDependency
20
+editor.fx.0.height=722
21
+editor.fx.0.width=800
22
+editor.fx.0.x=2086
23
+editor.fx.0.y=284
24
+objectbench.height=101
25
+objectbench.width=461
26
+package.divider.horizontal=0.6
27
+package.divider.vertical=0.8007380073800738
28
+package.editor.height=427
29
+package.editor.width=674
30
+package.editor.x=2011
31
+package.editor.y=179
32
+package.frame.height=600
33
+package.frame.width=800
34
+package.numDependencies=6
35
+package.numTargets=5
36
+package.showExtends=true
37
+package.showUses=true
38
+project.charset=UTF-8
39
+readme.height=58
40
+readme.name=@README
41
+readme.width=47
42
+readme.x=10
43
+readme.y=10
44
+target1.height=40
45
+target1.name=Circle
46
+target1.showInterface=false
47
+target1.type=ClassTarget
48
+target1.width=80
49
+target1.x=110
50
+target1.y=120
51
+target2.height=40
52
+target2.name=Picture
53
+target2.showInterface=false
54
+target2.type=ClassTarget
55
+target2.width=80
56
+target2.x=420
57
+target2.y=160
58
+target3.height=40
59
+target3.name=Canvas
60
+target3.showInterface=false
61
+target3.type=ClassTarget
62
+target3.width=80
63
+target3.x=20
64
+target3.y=180
65
+target4.height=40
66
+target4.name=Triangle
67
+target4.showInterface=false
68
+target4.type=ClassTarget
69
+target4.width=80
70
+target4.x=270
71
+target4.y=60
72
+target5.height=40
73
+target5.name=Square
74
+target5.showInterface=false
75
+target5.type=ClassTarget
76
+target5.width=80
77
+target5.x=190
78
+target5.y=90

+ 221
- 0
Shapes/Canvas.java Parādīt failu

@@ -0,0 +1,221 @@
1
+import javax.swing.*;
2
+import java.awt.*;
3
+import java.util.List;
4
+import java.util.*;
5
+
6
+/**
7
+ * Canvas is a class to allow for simple graphical drawing on a canvas.
8
+ * This is a modification of the general purpose Canvas, specially made for
9
+ * the BlueJ "shapes" example. 
10
+ *
11
+ * @author: Bruce Quig
12
+ * @author: Michael Kölling (mik)
13
+ *
14
+ * @version: 1.6 (shapes)
15
+ */
16
+public class Canvas
17
+{
18
+    // Note: The implementation of this class (specifically the handling of
19
+    // shape identity and colors) is slightly more complex than necessary. This
20
+    // is done on purpose to keep the interface and instance fields of the
21
+    // shape objects in this project clean and simple for educational purposes.
22
+
23
+    private static Canvas canvasSingleton;
24
+
25
+    /**
26
+     * Factory method to get the canvas singleton object.
27
+     */
28
+    public static Canvas getCanvas()
29
+    {
30
+        if(canvasSingleton == null) {
31
+            canvasSingleton = new Canvas("BlueJ Shapes Demo", 300, 300, 
32
+                    Color.white);
33
+        }
34
+        canvasSingleton.setVisible(true);
35
+        return canvasSingleton;
36
+    }
37
+
38
+    //  ----- instance part -----
39
+
40
+    private JFrame frame;
41
+    private CanvasPane canvas;
42
+    private Graphics2D graphic;
43
+    private Color backgroundColour;
44
+    private Image canvasImage;
45
+    private List<Object> objects;
46
+    private HashMap<Object, ShapeDescription> shapes;
47
+    
48
+    /**
49
+     * Create a Canvas.
50
+     * @param title  title to appear in Canvas Frame
51
+     * @param width  the desired width for the canvas
52
+     * @param height  the desired height for the canvas
53
+     * @param bgClour  the desired background colour of the canvas
54
+     */
55
+    private Canvas(String title, int width, int height, Color bgColour)
56
+    {
57
+        frame = new JFrame();
58
+        canvas = new CanvasPane();
59
+        frame.setContentPane(canvas);
60
+        frame.setTitle(title);
61
+        canvas.setPreferredSize(new Dimension(width, height));
62
+        backgroundColour = bgColour;
63
+        frame.pack();
64
+        objects = new ArrayList<Object>();
65
+        shapes = new HashMap<Object, ShapeDescription>();
66
+    }
67
+
68
+    /**
69
+     * Set the canvas visibility and brings canvas to the front of screen
70
+     * when made visible. This method can also be used to bring an already
71
+     * visible canvas to the front of other windows.
72
+     * @param visible  boolean value representing the desired visibility of
73
+     * the canvas (true or false) 
74
+     */
75
+    public void setVisible(boolean visible)
76
+    {
77
+        if(graphic == null) {
78
+            // first time: instantiate the offscreen image and fill it with
79
+            // the background colour
80
+            Dimension size = canvas.getSize();
81
+            canvasImage = canvas.createImage(size.width, size.height);
82
+            graphic = (Graphics2D)canvasImage.getGraphics();
83
+            graphic.setColor(backgroundColour);
84
+            graphic.fillRect(0, 0, size.width, size.height);
85
+            graphic.setColor(Color.black);
86
+        }
87
+        frame.setVisible(visible);
88
+    }
89
+
90
+    /**
91
+     * Draw a given shape onto the canvas.
92
+     * @param  referenceObject  an object to define identity for this shape
93
+     * @param  color            the color of the shape
94
+     * @param  shape            the shape object to be drawn on the canvas
95
+     */
96
+     // Note: this is a slightly backwards way of maintaining the shape
97
+     // objects. It is carefully designed to keep the visible shape interfaces
98
+     // in this project clean and simple for educational purposes.
99
+    public void draw(Object referenceObject, String color, Shape shape)
100
+    {
101
+        objects.remove(referenceObject);   // just in case it was already there
102
+        objects.add(referenceObject);      // add at the end
103
+        shapes.put(referenceObject, new ShapeDescription(shape, color));
104
+        redraw();
105
+    }
106
+ 
107
+    /**
108
+     * Erase a given shape's from the screen.
109
+     * @param  referenceObject  the shape object to be erased 
110
+     */
111
+    public void erase(Object referenceObject)
112
+    {
113
+        objects.remove(referenceObject);   // just in case it was already there
114
+        shapes.remove(referenceObject);
115
+        redraw();
116
+    }
117
+
118
+    /**
119
+     * Set the foreground colour of the Canvas.
120
+     * @param  newColour   the new colour for the foreground of the Canvas 
121
+     */
122
+    public void setForegroundColor(String colorString)
123
+    {
124
+        if(colorString.equals("red"))
125
+            graphic.setColor(Color.red);
126
+        else if(colorString.equals("black"))
127
+            graphic.setColor(Color.black);
128
+        else if(colorString.equals("blue"))
129
+            graphic.setColor(Color.blue);
130
+        else if(colorString.equals("yellow"))
131
+            graphic.setColor(Color.yellow);
132
+        else if(colorString.equals("green"))
133
+            graphic.setColor(Color.green);
134
+        else if(colorString.equals("magenta"))
135
+            graphic.setColor(Color.magenta);
136
+        else if(colorString.equals("white"))
137
+            graphic.setColor(Color.white);
138
+        else
139
+            graphic.setColor(Color.black);
140
+    }
141
+
142
+    /**
143
+     * Wait for a specified number of milliseconds before finishing.
144
+     * This provides an easy way to specify a small delay which can be
145
+     * used when producing animations.
146
+     * @param  milliseconds  the number 
147
+     */
148
+    public void wait(int milliseconds)
149
+    {
150
+        try
151
+        {
152
+            Thread.sleep(milliseconds);
153
+        } 
154
+        catch (Exception e)
155
+        {
156
+            // ignoring exception at the moment
157
+        }
158
+    }
159
+
160
+    /**
161
+     * Redraw ell shapes currently on the Canvas.
162
+     */
163
+    private void redraw()
164
+    {
165
+        erase();
166
+        for(Iterator i=objects.iterator(); i.hasNext(); ) {
167
+            ((ShapeDescription)shapes.get(i.next())).draw(graphic);
168
+        }
169
+        canvas.repaint();
170
+    }
171
+       
172
+    /**
173
+     * Erase the whole canvas. (Does not repaint.)
174
+     */
175
+    private void erase()
176
+    {
177
+        Color original = graphic.getColor();
178
+        graphic.setColor(backgroundColour);
179
+        Dimension size = canvas.getSize();
180
+        graphic.fill(new Rectangle(0, 0, size.width, size.height));
181
+        graphic.setColor(original);
182
+    }
183
+
184
+
185
+    /************************************************************************
186
+     * Inner class CanvasPane - the actual canvas component contained in the
187
+     * Canvas frame. This is essentially a JPanel with added capability to
188
+     * refresh the image drawn on it.
189
+     */
190
+    private class CanvasPane extends JPanel
191
+    {
192
+        public void paint(Graphics g)
193
+        {
194
+            g.drawImage(canvasImage, 0, 0, null);
195
+        }
196
+    }
197
+    
198
+    /************************************************************************
199
+     * Inner class CanvasPane - the actual canvas component contained in the
200
+     * Canvas frame. This is essentially a JPanel with added capability to
201
+     * refresh the image drawn on it.
202
+     */
203
+    private class ShapeDescription
204
+    {
205
+        private Shape shape;
206
+        private String colorString;
207
+
208
+        public ShapeDescription(Shape shape, String color)
209
+        {
210
+            this.shape = shape;
211
+            colorString = color;
212
+        }
213
+
214
+        public void draw(Graphics2D graphic)
215
+        {
216
+            setForegroundColor(colorString);
217
+            graphic.fill(shape);
218
+        }
219
+    }
220
+
221
+}

+ 192
- 0
Shapes/Circle.java Parādīt failu

@@ -0,0 +1,192 @@
1
+import java.awt.*;
2
+import java.awt.geom.*;
3
+
4
+/**
5
+ * A circle that can be manipulated and that draws itself on a canvas.
6
+ * 
7
+ * @author  Michael Kölling and David J. Barnes
8
+ * @version 1.0  (15 July 2000)
9
+ */
10
+
11
+public class Circle
12
+{
13
+    private int diameter;
14
+    private int xPosition;
15
+    private int yPosition;
16
+    private String color;
17
+    private boolean isVisible;
18
+
19
+    /**
20
+     * Create a new circle at default position with default color.
21
+     */
22
+    public Circle()
23
+    {
24
+        diameter = 30;
25
+        xPosition = 20;
26
+        yPosition = 60;
27
+        color = "blue";
28
+        isVisible = false;
29
+    }
30
+
31
+    /**
32
+     * Make this circle visible. If it was already visible, do nothing.
33
+     */
34
+    public void makeVisible()
35
+    {
36
+        isVisible = true;
37
+        draw();
38
+    }
39
+
40
+    /**
41
+     * Make this circle invisible. If it was already invisible, do nothing.
42
+     */
43
+    public void makeInvisible()
44
+    {
45
+        erase();
46
+        isVisible = false;
47
+    }
48
+
49
+    /**
50
+     * Move the circle a few pixels to the right.
51
+     */
52
+    public void moveRight()
53
+    {
54
+        moveHorizontal(20);
55
+    }
56
+
57
+    /**
58
+     * Move the circle a few pixels to the left.
59
+     */
60
+    public void moveLeft()
61
+    {
62
+        moveHorizontal(-20);
63
+    }
64
+
65
+    /**
66
+     * Move the circle a few pixels up.
67
+     */
68
+    public void moveUp()
69
+    {
70
+        moveVertical(-20);
71
+    }
72
+
73
+    /**
74
+     * Move the circle a few pixels down.
75
+     */
76
+    public void moveDown()
77
+    {
78
+        moveVertical(20);
79
+    }
80
+
81
+    /**
82
+     * Move the circle horizontally by 'distance' pixels.
83
+     */
84
+    public void moveHorizontal(int distance)
85
+    {
86
+        erase();
87
+        xPosition += distance;
88
+        draw();
89
+    }
90
+
91
+    /**
92
+     * Move the circle vertically by 'distance' pixels.
93
+     */
94
+    public void moveVertical(int distance)
95
+    {
96
+        erase();
97
+        yPosition += distance;
98
+        draw();
99
+    }
100
+
101
+    /**
102
+     * Slowly move the circle horizontally by 'distance' pixels.
103
+     */
104
+    public void slowMoveHorizontal(int distance)
105
+    {
106
+        int delta;
107
+
108
+        if(distance < 0) 
109
+        {
110
+            delta = -1;
111
+            distance = -distance;
112
+        }
113
+        else 
114
+        {
115
+            delta = 1;
116
+        }
117
+
118
+        for(int i = 0; i < distance; i++)
119
+        {
120
+            xPosition += delta;
121
+            draw();
122
+        }
123
+    }
124
+
125
+    /**
126
+     * Slowly move the circle vertically by 'distance' pixels.
127
+     */
128
+    public void slowMoveVertical(int distance)
129
+    {
130
+        int delta;
131
+
132
+        if(distance < 0) 
133
+        {
134
+            delta = -1;
135
+            distance = -distance;
136
+        }
137
+        else 
138
+        {
139
+            delta = 1;
140
+        }
141
+
142
+        for(int i = 0; i < distance; i++)
143
+        {
144
+            yPosition += delta;
145
+            draw();
146
+        }
147
+    }
148
+
149
+    /**
150
+     * Change the size to the new size (in pixels). Size must be >= 0.
151
+     */
152
+    public void changeSize(int newDiameter)
153
+    {
154
+        erase();
155
+        diameter = newDiameter;
156
+        draw();
157
+    }
158
+
159
+    /**
160
+     * Change the color. Valid colors are "red", "yellow", "blue", "green",
161
+     * "magenta" and "black".
162
+     */
163
+    public void changeColor(String newColor)
164
+    {
165
+        color = newColor;
166
+        draw();
167
+    }
168
+
169
+    /*
170
+     * Draw the circle with current specifications on screen.
171
+     */
172
+    private void draw()
173
+    {
174
+        if(isVisible) {
175
+            Canvas canvas = Canvas.getCanvas();
176
+            canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition, 
177
+                    diameter, diameter));
178
+            canvas.wait(10);
179
+        }
180
+    }
181
+
182
+    /*
183
+     * Erase the circle on screen.
184
+     */
185
+    private void erase()
186
+    {
187
+        if(isVisible) {
188
+            Canvas canvas = Canvas.getCanvas();
189
+            canvas.erase(this);
190
+        }
191
+    }
192
+}

README.TXT → Shapes/README.TXT Parādīt failu


+ 11
- 0
Shapes/README.md Parādīt failu

@@ -0,0 +1,11 @@
1
+# FirstLab-Shapes
2
+
3
+This is the first BlueJ lab. 
4
+
5
+Be sure to 
6
+
7
+* FORK it to your own account.
8
+* CLONE your version of the repo into your dev directory.
9
+* Open the BlueJ project file from Finder.
10
+
11
+Object of lb is to follow the chapter reading the doc folder. (Draw a House!)

+ 191
- 0
Shapes/Square.java Parādīt failu

@@ -0,0 +1,191 @@
1
+import java.awt.*;
2
+
3
+/**
4
+ * A square that can be manipulated and that draws itself on a canvas.
5
+ * 
6
+ * @author  Michael Kölling and David J. Barnes
7
+ * @version 1.0  (15 July 2000)
8
+ */
9
+
10
+public class Square
11
+{
12
+    private int size;
13
+    private int xPosition;
14
+    private int yPosition;
15
+    private String color;
16
+    private boolean isVisible;
17
+
18
+    /**
19
+     * Create a new square at default position with default color.
20
+     */
21
+    public Square()
22
+    {
23
+        size = 30;
24
+        xPosition = 60;
25
+        yPosition = 50;
26
+        color = "red";
27
+        isVisible = false;
28
+    }
29
+
30
+    /**
31
+     * Make this square visible. If it was already visible, do nothing.
32
+     */
33
+    public void makeVisible()
34
+    {
35
+        isVisible = true;
36
+        draw();
37
+    }
38
+
39
+    /**
40
+     * Make this square invisible. If it was already invisible, do nothing.
41
+     */
42
+    public void makeInvisible()
43
+    {
44
+        erase();
45
+        isVisible = false;
46
+    }
47
+
48
+    /**
49
+     * Move the square a few pixels to the right.
50
+     */
51
+    public void moveRight()
52
+    {
53
+        moveHorizontal(20);
54
+    }
55
+
56
+    /**
57
+     * Move the square a few pixels to the left.
58
+     */
59
+    public void moveLeft()
60
+    {
61
+        moveHorizontal(-20);
62
+    }
63
+
64
+    /**
65
+     * Move the square a few pixels up.
66
+     */
67
+    public void moveUp()
68
+    {
69
+        moveVertical(-20);
70
+    }
71
+
72
+    /**
73
+     * Move the square a few pixels down.
74
+     */
75
+    public void moveDown()
76
+    {
77
+        moveVertical(20);
78
+    }
79
+
80
+    /**
81
+     * Move the square horizontally by 'distance' pixels.
82
+     */
83
+    public void moveHorizontal(int distance)
84
+    {
85
+        erase();
86
+        xPosition += distance;
87
+        draw();
88
+    }
89
+
90
+    /**
91
+     * Move the square vertically by 'distance' pixels.
92
+     */
93
+    public void moveVertical(int distance)
94
+    {
95
+        erase();
96
+        yPosition += distance;
97
+        draw();
98
+    }
99
+
100
+    /**
101
+     * Slowly move the square horizontally by 'distance' pixels.
102
+     */
103
+    public void slowMoveHorizontal(int distance)
104
+    {
105
+        int delta;
106
+
107
+        if(distance < 0) 
108
+        {
109
+            delta = -1;
110
+            distance = -distance;
111
+        }
112
+        else 
113
+        {
114
+            delta = 1;
115
+        }
116
+
117
+        for(int i = 0; i < distance; i++)
118
+        {
119
+            xPosition += delta;
120
+            draw();
121
+        }
122
+    }
123
+
124
+    /**
125
+     * Slowly move the square vertically by 'distance' pixels.
126
+     */
127
+    public void slowMoveVertical(int distance)
128
+    {
129
+        int delta;
130
+
131
+        if(distance < 0) 
132
+        {
133
+            delta = -1;
134
+            distance = -distance;
135
+        }
136
+        else 
137
+        {
138
+            delta = 1;
139
+        }
140
+
141
+        for(int i = 0; i < distance; i++)
142
+        {
143
+            yPosition += delta;
144
+            draw();
145
+        }
146
+    }
147
+
148
+    /**
149
+     * Change the size to the new size (in pixels). Size must be >= 0.
150
+     */
151
+    public void changeSize(int newSize)
152
+    {
153
+        erase();
154
+        size = newSize;
155
+        draw();
156
+    }
157
+
158
+    /**
159
+     * Change the color. Valid colors are "red", "yellow", "blue", "green",
160
+     * "magenta" and "black".
161
+     */
162
+    public void changeColor(String newColor)
163
+    {
164
+        color = newColor;
165
+        draw();
166
+    }
167
+
168
+    /*
169
+     * Draw the square with current specifications on screen.
170
+     */
171
+    private void draw()
172
+    {
173
+        if(isVisible) {
174
+            Canvas canvas = Canvas.getCanvas();
175
+            canvas.draw(this, color,
176
+                    new Rectangle(xPosition, yPosition, size, size));
177
+            canvas.wait(10);
178
+        }
179
+    }
180
+
181
+    /*
182
+     * Erase the square on screen.
183
+     */
184
+    private void erase()
185
+    {
186
+        if(isVisible) {
187
+            Canvas canvas = Canvas.getCanvas();
188
+            canvas.erase(this);
189
+        }
190
+    }
191
+}

+ 195
- 0
Shapes/Triangle.java Parādīt failu

@@ -0,0 +1,195 @@
1
+import java.awt.*;
2
+
3
+/**
4
+ * A triangle that can be manipulated and that draws itself on a canvas.
5
+ * 
6
+ * @author  Michael Kölling and David J. Barnes
7
+ * @version 1.0  (15 July 2000)
8
+ */
9
+
10
+public class Triangle
11
+{
12
+    private int height;
13
+    private int width;
14
+    private int xPosition;
15
+    private int yPosition;
16
+    private String color;
17
+    private boolean isVisible;
18
+
19
+    /**
20
+     * Create a new triangle at default position with default color.
21
+     */
22
+    public Triangle()
23
+    {
24
+        height = 30;
25
+        width = 40;
26
+        xPosition = 50;
27
+        yPosition = 15;
28
+        color = "green";
29
+        isVisible = false;
30
+    }
31
+
32
+    /**
33
+     * Make this triangle visible. If it was already visible, do nothing.
34
+     */
35
+    public void makeVisible()
36
+    {
37
+        isVisible = true;
38
+        draw();
39
+    }
40
+
41
+    /**
42
+     * Make this triangle invisible. If it was already invisible, do nothing.
43
+     */
44
+    public void makeInvisible()
45
+    {
46
+        erase();
47
+        isVisible = false;
48
+    }
49
+
50
+    /**
51
+     * Move the triangle a few pixels to the right.
52
+     */
53
+    public void moveRight()
54
+    {
55
+        moveHorizontal(20);
56
+    }
57
+
58
+    /**
59
+     * Move the triangle a few pixels to the left.
60
+     */
61
+    public void moveLeft()
62
+    {
63
+        moveHorizontal(-20);
64
+    }
65
+
66
+    /**
67
+     * Move the triangle a few pixels up.
68
+     */
69
+    public void moveUp()
70
+    {
71
+        moveVertical(-20);
72
+    }
73
+
74
+    /**
75
+     * Move the triangle a few pixels down.
76
+     */
77
+    public void moveDown()
78
+    {
79
+        moveVertical(20);
80
+    }
81
+
82
+    /**
83
+     * Move the triangle horizontally by 'distance' pixels.
84
+     */
85
+    public void moveHorizontal(int distance)
86
+    {
87
+        erase();
88
+        xPosition += distance;
89
+        draw();
90
+    }
91
+
92
+    /**
93
+     * Move the triangle vertically by 'distance' pixels.
94
+     */
95
+    public void moveVertical(int distance)
96
+    {
97
+        erase();
98
+        yPosition += distance;
99
+        draw();
100
+    }
101
+
102
+    /**
103
+     * Slowly move the triangle horizontally by 'distance' pixels.
104
+     */
105
+    public void slowMoveHorizontal(int distance)
106
+    {
107
+        int delta;
108
+
109
+        if(distance < 0) 
110
+        {
111
+            delta = -1;
112
+            distance = -distance;
113
+        }
114
+        else 
115
+        {
116
+            delta = 1;
117
+        }
118
+
119
+        for(int i = 0; i < distance; i++)
120
+        {
121
+            xPosition += delta;
122
+            draw();
123
+        }
124
+    }
125
+
126
+    /**
127
+     * Slowly move the triangle vertically by 'distance' pixels.
128
+     */
129
+    public void slowMoveVertical(int distance)
130
+    {
131
+        int delta;
132
+
133
+        if(distance < 0) 
134
+        {
135
+            delta = -1;
136
+            distance = -distance;
137
+        }
138
+        else 
139
+        {
140
+            delta = 1;
141
+        }
142
+
143
+        for(int i = 0; i < distance; i++)
144
+        {
145
+            yPosition += delta;
146
+            draw();
147
+        }
148
+    }
149
+
150
+    /**
151
+     * Change the size to the new size (in pixels). Size must be >= 0.
152
+     */
153
+    public void changeSize(int newHeight, int newWidth)
154
+    {
155
+        erase();
156
+        height = newHeight;
157
+        width = newWidth;
158
+        draw();
159
+    }
160
+
161
+    /**
162
+     * Change the color. Valid colors are "red", "yellow", "blue", "green",
163
+     * "magenta" and "black".
164
+     */
165
+    public void changeColor(String newColor)
166
+    {
167
+        color = newColor;
168
+        draw();
169
+    }
170
+
171
+    /*
172
+     * Draw the triangle with current specifications on screen.
173
+     */
174
+    private void draw()
175
+    {
176
+        if(isVisible) {
177
+            Canvas canvas = Canvas.getCanvas();
178
+            int[] xpoints = { xPosition, xPosition + (width/2), xPosition - (width/2) };
179
+            int[] ypoints = { yPosition, yPosition + height, yPosition + height };
180
+            canvas.draw(this, color, new Polygon(xpoints, ypoints, 3));
181
+            canvas.wait(10);
182
+        }
183
+    }
184
+
185
+    /*
186
+     * Erase the triangle on screen.
187
+     */
188
+    private void erase()
189
+    {
190
+        if(isVisible) {
191
+            Canvas canvas = Canvas.getCanvas();
192
+            canvas.erase(this);
193
+        }
194
+    }
195
+}

doc/BlueJ-objects-first-ch1.pdf → Shapes/doc/BlueJ-objects-first-ch1.pdf Parādīt failu


package.bluej → Shapes/package.bluej Parādīt failu