Curve Introduction
Each vertex of the besell curve has two control points, which are used to control the curve radians on both sides of the vertex. It is a mathematical curve applied to two-dimensional graphics applications. A curve has four points: the starting point, the ending point (also called the anchor), and two separated intermediate points. Sliding two intermediate points will change the shape of the besell curve. In the late 1960s S, Pierre béier used mathematical methods to depict the bersel curve for Renault's automobile manufacturing industry.
Curve drawing
The following two java files are used to implement the given control vertices, draw the corresponding besuppliers curve, and drag the control points to automatically update the besuppliers curve;
Bezierframe. Java file content:
package test1;import java.awt.*;import javax.swing.*;public class BezierFrame extends JFrame{public static void main(String[] args){EventQueue.invokeLater(new Runnable(){public void run(){JFrame frame = new JFrame();frame.setTitle("BezierTest");frame.setSize(600,600);BezierPanel bezier = new BezierPanel();bezier.setPreferredSize(new Dimension(580, 580));frame.add(bezier, BorderLayout.CENTER);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);}});}}
Bezierpanel. Java file content:
Package test1; import javax. swing. *; import Java. AWT. *; import Java. AWT. geom. *; import Java. AWT. event. *; import Java. util. arraylist; import Java. util. random; Class bezierpanel extends jcomponent {Private Static int size = 10; private int current; Private Static random generator = new random (); Private point2d [] points; Public bezierpanel () {// initpoints (4); point2d p1 = new point2d. double (100,200); point2d P2 = new point2d. double (150,100); point2d P3 = new point2d. double (200,100); point2d P4 = new point2d. double (250,200); points = new point2d [] {P1, P2, P3, P4}; addmouselistener (New mouseadapter () {public void mousepressed (mouseevent event) {point2d P = event. getpoint (); For (INT I = 0; I <points. length; I ++) {Double X = points [I]. getx ()-size/2; Double Y = points [I]. gety ()-size/2; rectangle2d r = new rectangle2d. double (X, Y, size, size); If (R. contains (p) {current = I; return ;}} public void mousereleased (mouseevent event) {current =-1 ;}}); addmousemotionlistener (New mousemotionadapter () {public void mousedragged (mouseevent event) {If (current =-1) return; points [current] = event. getpoint (); repaint () ;}}); current =-1 ;}// returns the public point2d cubicbezr (double T, point2d [] P) {point2d [] temp = new point2d [p. length]; for (int K = 0; k <p. length; k ++) temp [k] = P [k]; for (INT I = 0; I <3; I ++) {for (Int J = 0; j <4-i-1; j ++) {Double X = (1-T) * temp [J]. getx () + T * temp [J + 1]. getx (); Double Y = (1-T) * temp [J]. gety () + T * temp [J + 1]. gety (); temp [J] = new point2d. double (x, y) ;}} return temp [0];} // draw the three-time bezr Curve Public void drawbezr (Graphics g, point2d [] P) {for (double T = 0; t <1; t + = 0.002) {point2d p1 = cubicbezr (T, P); point2d P2 = cubicbezr (t + 0.001, P ); g. drawline (INT) p1.getx (), (INT) p1.gety (), (INT) p2.getx (), (INT) p2.gety () ;}// override the jcomponent method, used to initialize and draw public void paintcomponent (Graphics g) {If (points = NULL) return; // draw four control points graphics2d g2 = (graphics2d) g; for (INT I = 0; I <points. length; I ++) {Double X = points [I]. getx ()-size/2; Double Y = points [I]. gety ()-size/2; g2.fill (New rectangle2d. double (X, Y, size, size);} // draw the curve drawbezr (G, points );}}
The above two java files come from resources: http://download.csdn.net/detail/amuguelove/4331693