-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShape.java
More file actions
112 lines (98 loc) · 3.34 KB
/
Copy pathShape.java
File metadata and controls
112 lines (98 loc) · 3.34 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
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 {
Polygon polygon = new Polygon();
Color fill = null;
Color line = null;
int sideNo;
Group group;//to be returned
Canvas canvas;
GraphicsContext gc;
List<Double> pointsx = new ArrayList<Double>(); //points for the polygons
List<Double> pointsy = new ArrayList<Double>();
boolean oval = false;
int oheight = 0;//oval height
int owidth = 0;
int ocx = 0;//oval reference coordinates
int ocy = 0;
float width;//frame size
float height;
public Shape(Color lineColour, Color fillColour,int w, int h, int lw) { // 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);
}
public Shape(int w, int h, int lw, Color c1, Color c2, float c1x, float c1y, float c2x, float c2y, Boolean Cyclical) { //constructor for a shape with a colour gradient
group = new Group();
width = w;
height = h;
canvas = new Canvas(width,height);
gc = canvas.getGraphicsContext2D();
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);
}
}