-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBezierCurve.java
More file actions
96 lines (85 loc) · 2.57 KB
/
BezierCurve.java
File metadata and controls
96 lines (85 loc) · 2.57 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package task8;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Line2D;
import java.awt.geom.QuadCurve2D;
import java.util.ArrayList;
import javax.swing.JPanel;
/**
*
* @author Maestro
*/
public class BezierCurve extends JPanel {
//(1-t) P0 + t P1
Point p1, p2, p3,p4;
boolean quad=false;
ArrayList<Point> points = new ArrayList<>();
boolean isItCurve=false;
BezierCurve(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}
BezierCurve(Point p1, Point p2, Point p3) {
quad=true;
isItCurve=true;
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
BezierCurve(Point p1, Point p2, Point p3, Point p4) {
isItCurve=true;
quad=false;
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
this.p4=p4;
}
private void calculatePoints() {
for (float t = 0; t <= 1; t += 0.1) {
if (t == 0) {
points.add(p1);
} else if (t == 1) {
points.add(p2);
}
else {
float x = p1.x;
float y = p1.y;
x = ((1 - t) * x) + (t * p2.x);
y = ((1 - t) * y) + (t * p2.y);
points.add(new Point(Math.round(x), Math.round(y)));
}
}
}
@Override
public void paintComponent(Graphics g) {
calculatePoints();
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g2);
BasicStroke bs = new BasicStroke(6.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
g2.setStroke(bs);
g2.setColor(Color.red);
if(isItCurve==false){
for (int i = 1; i < points.size(); i++) {
g2.draw(new Line2D.Double(points.get(i - 1).x, points.get(i - 1).y, points.get(i).x, points.get(i).y));
}
}
else if(isItCurve==true && quad==true){
QuadCurve2D quadcurve = new QuadCurve2D.Float(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
g2.draw(quadcurve);
}
else if(isItCurve==true && quad==false){
CubicCurve2D cc= new CubicCurve2D.Float(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y,p4.x,p4.y);
g2.draw(cc);
}
points.removeAll(points);
}
}