-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationViewer.java
More file actions
184 lines (177 loc) · 7.74 KB
/
Copy pathAnimationViewer.java
File metadata and controls
184 lines (177 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* ==========================================================================================
* AnimationViewer.java : Moves shapes around on the screen according to different paths.
* It is the main drawing area where shapes are added and manipulated.
* NAME: JANHAV KHANNA
* YOUR UPI: JKHA639
* DATE: 20/05/21
* DESCRIPTION: This is the animationviewer class which handles the drawing, movement, addition of shapes and painting of each shape that is being added to the program.
* ==========================================================================================
*/
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class AnimationViewer extends JComponent implements Runnable {
private Thread animationThread = null; // the thread for animation
private static int DELAY = 20; // the current animation speed
private ArrayList<Shape> shapes = new ArrayList<Shape>(); //create the ArrayList to store shapes
private Painter painter = new GraphicsPainter();
private ShapeType currentShapeType=Shape.DEFAULT_SHAPETYPE; // the current shape type,
private PathType currentPathType=Shape.DEFAULT_PATHTYPE; // the current path type
private Color currentBorderColor=Shape.DEFAULT_BORDER_COLOR, currentFillColor=Shape.DEFAULT_FILL_COLOR; // the current fill colour of a shape
private int marginWidth=Shape.DEFAULT_MARGIN_WIDTH, marginHeight=Shape.DEFAULT_MARGIN_HEIGHT, currentWidth=Shape.DEFAULT_WIDTH, currentHeight=Shape.DEFAULT_HEIGHT;
private String currentText=Shape.DEFAULT_TEXT;
/** Constructor of the AnimationPanel */
public AnimationViewer(boolean isGraphicsVersion) {
if (isGraphicsVersion) {
start();
addMouseListener(new MyMouseAdapter());
}
}
/** create a new shape
* @param x the x-coordinate of the mouse position
* @param y the y-coordinate of the mouse position */
protected void createNewShape(int x, int y) {
switch (currentShapeType) {
case RECTANGLE: {
shapes.add( new RectangleShape(x, y,currentWidth,currentHeight,marginWidth,marginHeight,currentBorderColor,currentFillColor,currentPathType,currentText) );
break;
} case XRECTANGLE: {
shapes.add( new XRectangleShape(x, y,currentWidth,currentHeight,marginWidth,marginHeight,currentBorderColor,currentFillColor,currentPathType,currentText) );
break;
} case OVAL: {
shapes.add(new OvalShape(x,y,currentWidth,currentHeight,marginWidth,marginHeight,currentBorderColor,currentFillColor,currentPathType,currentText));
break;
} case SQUARE: {
shapes.add(new SquareShape(x,y,Math.min(currentWidth,currentHeight),marginWidth,marginHeight,currentBorderColor,currentFillColor,currentPathType,currentText));
break;
} case NESTED: {
shapes.add(new NestedShape(x,y,currentWidth,currentHeight,marginWidth,marginHeight,currentBorderColor,currentFillColor,currentPathType,currentText));
break;
}
}
}
/** move and paint all shapes within the animation area
* @param g the Graphics control */
public void paintComponent(Graphics g) {
painter.setGraphics(g);
super.paintComponent(g);
for (Shape currentShape: shapes) {
currentShape.move();
currentShape.draw(painter);
currentShape.drawHandles(painter);
currentShape.drawCenteredText(painter);
}
}
public String getCurrentText(){
return this.currentText;
}
public void setCurrentText(String text){
this.currentText = text;
for (Shape currentShape: shapes){
if (currentShape.isSelected())
currentShape.setText(text);
}
}
public ShapeType getCurrentShapeType() { return currentShapeType; }
public void setCurrentShapeType(int st) {
currentShapeType = ShapeType.getShapeType(st);
}
public PathType getCurrentPathType() { return currentPathType; }
public void setCurrentPathType(int pt) {
currentPathType = PathType.getPathType(pt);
}
/** get the current width
* @return currentWidth - the width value */
public int getCurrentWidth() { return currentWidth; }
/** set the current width and the width for all currently selected shapes
* @param w the new width value */
public void setCurrentWidth(int w) {
currentWidth = w;
for (Shape currentShape: shapes)
if ( currentShape.isSelected())
currentShape.setWidth(currentWidth);
}
/** get the current height
* @return currentHeight - the height value */
public int getCurrentHeight() { return currentHeight; }
/** set the current height and the height for all currently selected shapes
* @param h the new height value */
public void setCurrentHeight(int h) {
currentHeight = h;
for (Shape currentShape: shapes)
if ( currentShape.isSelected())
currentShape.setHeight(currentHeight);
}
/** get the current border colour
* @return currentBorderColor - the border colour value */
public Color getCurrentBorderColor() { return currentBorderColor; }
/** set the current border colour and the border colour for all currently selected shapes
* @param bc the new border colour value */
public void setCurrentBorderColor(Color bc) {
currentBorderColor = bc;
for (Shape currentShape: shapes)
if ( currentShape.isSelected())
currentShape.setBorderColor(currentBorderColor);
}
/** get the current fill colour
* @return currentFillColor - the fill colour value */
public Color getCurrentFillColor() { return currentFillColor; }
/** set the current fill colour and the border colour for all currently selected shapes
* @param bc the new fill colour value */
public void setCurrentFillColor(Color fc) {
currentFillColor = fc;
//complete this
for (Shape currentShape: shapes)
if ( currentShape.isSelected())
currentShape.setFillColor(currentFillColor);
}
class MyMouseAdapter extends MouseAdapter {
public void mouseClicked( MouseEvent e ) {
boolean found = false;
for (Shape currentShape: shapes)
if ( currentShape.contains( e.getPoint()) ) { // if the mousepoint is within a shape, then set the shape to be selected/deselected
currentShape.setSelected( ! currentShape.isSelected() );
found = true;
}
if (!found) createNewShape(e.getX(), e.getY());
}
}
// you don't need to make any changes after this line ______________
/** update the painting area
* @param g the graphics control */
public void update(Graphics g){ paint(g); }
/** reset the margin size of all shapes from our ArrayList */
public void resetMarginSize() {
marginWidth = getWidth();
marginHeight = getHeight() ;
for (Shape currentShape: shapes)
currentShape.setMarginSize(marginWidth,marginHeight );
}
/** When the "start" button is pressed, start the thread */
public void start() {
animationThread = new Thread(this);
animationThread.start();
}
/** When the "stop" button is pressed, stop the thread */
public void stop() {
if (animationThread != null) {
animationThread = null;
}
}
/** run the animation */
public void run() {
Thread myThread = Thread.currentThread();
while(animationThread==myThread) {
repaint();
pause(DELAY);
}
}
/** Sleep for the specified amount of time */
private void pause(int milliseconds) {
try {
Thread.sleep((long)milliseconds);
} catch(InterruptedException ie) {}
}
}