//Bezier Curve Algorithm //User chooses four points of the graphic canvas (by clicking mouse button) that will describe cubic bezier curve. //Finally the curve will be drawn. User can also move with created points and so change shape of the curve. import java.awt.*; import java.applet.Applet; public class BezierCurve extends Applet { Point[] points; //control points int number = 0; //control points counter Image buffer; //image buffer Graphics gfx; //graphics double p; //parameter int moving = 5; //flag to notify if user is moving a point double x1,x2,y1,y2; //temporary coordinates public void init() { points = new Point[4]; buffer = createImage(size().width,size().height); //create graphic buffer gfx = buffer.getGraphics(); gfx.setColor(Color.white); gfx.fillRect(0, 0, size().width, size().height); setBackground(Color.white); } public void bernstein() { for(int i=0; i<=100;i++) { //Berstein polynomials p=(double)i/100.0d; x2=(points[0].x+p*(-points[0].x*3+p*(3*points[0].x- points[0].x*p)))+p*(3*points[1].x+p*(-6*points[1].x+ points[1].x*3*p))+p*p*(points[2].x*3-points[2].x*3*p)+ points[3].x*p*p*p; y2=(points[0].y+p*(-points[0].y*3+p*(3*points[0].y- points[0].y*p)))+p*(3*points[1].y+p*(-6*points[1].y+ points[1].y*3*p))+p*p*(points[2].y*3-points[2].y*3*p)+ points[3].y*p*p*p; gfx.drawLine((int)x1,(int)y1,(int)x2,(int)y2); //bezier curve x1 = x2; y1 = y2; } } public void paint(Graphics g) { //paint gfx.setColor(Color.white); gfx.clearRect(0,0,size().width,size().height); //repaint background if(number == 4) { x1 = points[0].x; y1 = points[0].y; for(int i=0; i1 && i<(number-1) && i!=1) { gfx.setColor(Color.lightGray); gfx.drawLine(points[i].x,points[i].y,points[i+1].x,points[i+1].y); //control handles } } gfx.setColor(Color.black); bernstein(); } for(int i=0; isize().width) x=size().width-5; if (x<0) x=5; if (y>size().height) y=size().height-5; if (y<0) y=5; points[moving].move(x,y); repaint(); } return true; } public boolean mouseUp(Event evt, int x, int y) { moving = 5; repaint(); return true; } public void update(Graphics g) { //update paint(g); } }