import java.awt.*;
import java.awt.event.*;
import java.util.*;
class CvBezier extends Canvas
{ Point2D[] P = new Point2D[4];
int np=0,centerX,centerY;
float rWidth=10.0F,rHeight=7.5F,eps=rWidth/100F, pixelSize;
CvBezier() {
super();
setBackground(Color.white);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
float x=fx(evt.getX()),y=fy(evt.getY());
if(np==4) np=0;
P[np++] = new Point2D(x, y);
repaint();
}
});
}
void initgr()
{ Dimension d = getSize();
int maxX = d.width - 1,maxY = d.height - 1;
pixelSize = Math.max(rWidth/maxX,rHeight/maxY);
centerX = maxX/2; centerY = maxY/2;
}
int iX(float x){return Math.round(centerX + x/pixelSize);}
int iY(float y){return Math.round(centerY - y/pixelSize);}
float fx(int X){return (X - centerX) * pixelSize;}
float fy(int Y){return (centerY - Y) * pixelSize;}
Point2D middle(Point2D A, Point2D B)
{ return new Point2D((A.x + B.x)/2, (A.y + B.y)/2);
}
void bezier(Graphics g, Point2D P0, Point2D P1,Point2D P2, Point2D P3)
{ int x0=iX(P0.x), y0=iY(P0.y),x3=iX(P3.x),y3=iY(P3.y);
if (Math.abs(x0 - x3)<= 1 && Math.abs(y0 - y3) <= 1)
g.drawLine(x0,y0,x3,y3);
else
{ Point2D A=middle(P0,P1), B=middle(P3,P2),C=middle(P1,P2),A1 = middle(A,C),B1 = middle(B,C)
,C1=middle(A1,B1);
bezier(g,C1,B1,B,P3);
bezier(g,P0,A,A1,C1);
}
}
public void paint(Graphics g)
{ initgr();
int left = iX(-rWidth/2), right = iX(rWidth/2),bottom = iY(-rHeight/2),top = iY(rHeight/2);
g.drawRect(left,top,right -left,bottom-top);
for (int i=0; i0)
g.drawLine(iX(P[i-1].x),iY(P[i-1].y),iX(P[i].x),iY(P[i].y));
}
if (np==4) bezier(g,P[0],P[1],P[2],P[3]);
}
}