-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShape.java
More file actions
148 lines (130 loc) · 4.23 KB
/
Copy pathShape.java
File metadata and controls
148 lines (130 loc) · 4.23 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
package media;
import java.util.ArrayList;
import java.util.List;
import javafx.scene.Group;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Polygon;
public class Shape {
private Color fill = null;
private Color line = null;
private Group group;//to be returned
private Canvas canvas;
private GraphicsContext gc;
private List<Double> pointsX = new ArrayList<Double>(); //points for the polygons
private List<Double> pointsY = new ArrayList<Double>();
private boolean oval = false;
private int oheight = 0;//oval height
private int owidth = 0;
private int ocx = 0;//oval reference coordinates
private int ocy = 0;
private float width;//frame size
private float height;
private int startTime = 0;
private int endTime;
private int slideNumber;
public Shape(Color lineColour, Color fillColour,int w, int h, int lw, int startTime, int endTime, int slideNumber) { // constructor for a solid colour shape
group = new Group();
fill = fillColour;
line = lineColour;
width = w;
height = h;
canvas = new Canvas(width,height);
gc = canvas.getGraphicsContext2D();
gc.setFill(fill);
gc.setStroke(line);
gc.setLineWidth(lw);
this.startTime = startTime;
this.endTime = endTime;
this.slideNumber = slideNumber;
}
public Shape(int w, int h, int lw, Color c1, Color c2, float c1x, float c1y, float c2x, float c2y, Boolean Cyclical, int startTime, int endTime, int slideNumber) { //constructor for a shape with a colour gradient
group = new Group();
width = w;
height = h;
canvas = new Canvas(width,height);
gc = canvas.getGraphicsContext2D();
this.startTime = startTime;
this.endTime = endTime;
this.slideNumber = slideNumber;
if (Cyclical == true) {//sets up a cyclical gradient pattern
Stop[] stops = new Stop[] {new Stop(0,c1), new Stop(1,c2)};
LinearGradient lg = new LinearGradient(c1x,c1y,c2x,c2y,false,CycleMethod.REFLECT,stops);
gc.setFill(lg);
gc.setStroke(Color.TRANSPARENT);
}
else {//standard linear gradient
Stop[] stops = new Stop[] {new Stop(0,c1), new Stop(1,c2)};
LinearGradient lg = new LinearGradient(c1x,c1y,c2x,c2y,false,CycleMethod.NO_CYCLE,stops);
gc.setFill(lg);
gc.setStroke(Color.TRANSPARENT);
}
}
public void addPoint(double x, double y) {//adds point to the polygon
pointsX.add(x);
pointsY.add(y);
}
public void drawOval (int width, int height, int cx, int cy) {// draws an oval, cx,cy correspond to distance from the top left corner to enclosing box
oval = true; // changes shape from polygon to oval
oheight = height;
owidth = width;
ocx = cx;
ocy = cy;
}
public void create() {
if (oval == true) {
gc.fillOval(ocx,ocy,owidth,oheight);
gc.strokeOval(ocx,ocy,owidth,oheight);
group.getChildren().add(canvas);
}
else {
double[] pointX = new double[pointsX.size()];// used to create an array of points from an arraylist
double[] pointY = new double[pointsY.size()];
//adding arraylist points to an array
for (int i = 0; i<pointsX.size(); i++) {
pointX[i] = pointsX.get(i);
}
for (int i = 0; i<pointsY.size(); i++) {
pointY[i] = pointsY.get(i);
}
gc.strokePolygon(pointX,pointY,pointsX.size());//creates a polygon outline
gc.fillPolygon(pointX,pointY,pointsX.size());//creates the polygon solid
group.getChildren().add(canvas);//adds canvas to the group
}
}
public Group get() {
return group;
}
public void destroy() {//removes the canvas from the group
group.getChildren().remove(canvas);
}
public int getSlideNumber() {
return(slideNumber);
}
public int getStartTime() {
return(startTime);
}
public int getEndTime() {
return(endTime);
}
// Object serialization for test
public String serialization() {
String data="";
String fillS="null";
String lineS="null";
if(fill!=null) {
fillS=fill.toString();
}
if(line!=null) {
lineS=line.toString();
}
data=data+fillS+","+lineS+","+pointsX.toString()+","
+pointsY.toString()+","+oval+","+oheight+","+owidth+","+ocx+","+ocy
+","+width+","+height+","+startTime+","+endTime+","+slideNumber;
return data;
}
}