Code source de l'applet "BEZIER"
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class bezier extends Applet implements ItemListener,MouseListener,MouseMotionListener
{ private static final long serialVersionUID = 1420972181089579L;
Font font = new Font("SansSerif",1,12);
Font small = new Font("Serif",0,9);
Image ima; Graphics h;
int la,ha;
int Xs,Ys;
FlowLayout fl= new FlowLayout(1,50,8);
Checkbox cb3;
CheckboxGroup bg1 = new CheckboxGroup();
Checkbox cb1,cb2;
Object cren=RenderingHints.VALUE_ANTIALIAS_OFF;
boolean anticren=false;
protected Point2D [] pt=new Point2D[4];{
pt[0]=new Point2D.Double(50,100);
pt[1]=new Point2D.Double(200,220);
pt[2]=new Point2D.Double(420,200);
pt[3]=new Point2D.Double(550,80);}
Line2D tg1=new Line2D.Double();
Line2D tg2=new Line2D.Double();
CubicCurve2D cub=new CubicCurve2D.Double();
QuadCurve2D quad=new QuadCurve2D.Double();
BasicStroke epais2=new BasicStroke(2.f,0,0);
BasicStroke normal= new BasicStroke();
Cursor hand=new Cursor(Cursor.HAND_CURSOR);
Cursor curs=new Cursor(Cursor.DEFAULT_CURSOR);
Rectangle rect=new Rectangle();
boolean ok,ok1,ok2,cubique=true;
public void init()
{ resize(600,300);
setBackground(Color.lightGray);
setFont(font);
la=getWidth(); ha=getHeight();
ima=createImage(la,ha);
h=ima.getGraphics();
setLayout(fl);
addMouseListener(this);
addMouseMotionListener(this);
add(cb1 = new Checkbox("Cubique",bg1,true));
cb1.addItemListener(this);
add(cb2 = new Checkbox("Parabole",bg1,false));
cb2.addItemListener(this);
add(cb3 = new Checkbox("Anticrénelage",false));
cb3.addItemListener(this);}
public void itemStateChanged(ItemEvent evt)
{ if (evt.getSource()==cb3){
anticren=!anticren;
if (anticren)cren=RenderingHints.VALUE_ANTIALIAS_ON;
else cren=RenderingHints.VALUE_ANTIALIAS_OFF;}
if (evt.getSource()==cb1)cubique=true;
if (evt.getSource()==cb2)cubique=false;
repaint();}
public void cadre3D(int x, int y, int w, int z)
{ Color blanc=new Color(230,230,230),gris =new Color(130,130,130);
h.setColor(gris); h.drawLine(x,y,w,y);
h.drawLine(x,y+1,w-1,y+1); h.drawLine(x,y,x,z);
h.drawLine(x+1,y,x+1,z-1); h.setColor(blanc);
h.drawLine(x,z,w,z); h.drawLine(w,y+1,w,z);
h.drawLine(x+1,z-1,w,z-1); h.drawLine(w-1,y+2,w-1,z);}
public void update(Graphics g)
{ paint(g);}
public void paint(Graphics g)
{ h.setColor(Color.lightGray); h.fillRect(0,0,la,ha);
h.setColor(Color.black); h.drawRect(0,0,la-1,ha-1);
h.setFont(small); h.drawString("jjR 01-2012",25,ha-25);
h.setFont(font);
Graphics2D g2=(Graphics2D)h;
g2.setColor(Color.black);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,cren);
tg1.setLine(pt[0],pt[1]);
if (cubique)tg2.setLine(pt[2],pt[3]); else tg2.setLine(pt[1],pt[3]);
g2.draw(tg1);
g2.fill(new Rectangle2D.Double(pt[1].getX()-2,pt[1].getY()-2,5,5));
g2.draw(tg2);
if (cubique)g2.fill(new Rectangle2D.Double(pt[2].getX()-2,pt[2].getY()-2,5,5));
g2.setStroke(epais2);
g2.setColor(Color.red);
if (cubique) {
cub.setCurve(pt[0],pt[1],pt[2],pt[3]);
g2.draw(cub);}
else{
quad.setCurve(pt[0],pt[1],pt[3]);
g2.draw(quad);}
g2.setStroke(normal);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
cadre3D(20,40,la-20,ha-20);
g.drawImage(ima,0,0,this);
showStatus("J. J. Rousseau 01-2012");}
public void mousePressed(MouseEvent evt)
{ Xs=evt.getX(); Ys=evt.getY();
rect=new Rectangle((int)pt[1].getX()-2,(int)pt[1].getY()-2,6,15);
if (rect.contains(Xs, Ys))ok1=true;
rect=new Rectangle((int)pt[2].getX()-2,(int)pt[2].getY()-2,6,15);
if (rect.contains(Xs, Ys))ok2=true;}
public void mouseReleased(MouseEvent evt)
{ Xs=evt.getX(); Ys=evt.getY();
ok1=false; ok2=false;}
public void mouseExited(MouseEvent evt)
{}
public void mouseClicked(MouseEvent evt)
{}
public void mouseEntered(MouseEvent evt)
{}
public void mouseDragged(MouseEvent evt)
{ Xs=evt.getX(); Ys=evt.getY();
if (Xs<20)Xs=20;
if (Xs>580)Xs=580;
if (Ys<40)Ys=40;
if (Ys>280)Ys=280;
if (ok1){
pt[1].setLocation(Xs,Ys);}
if (ok2){
pt[2].setLocation(Xs,Ys);}
repaint();}
public void mouseMoved(MouseEvent evt)
{ ok=false;
Xs=evt.getX(); Ys=evt.getY();
rect=new Rectangle((int)pt[1].getX()-2,(int)pt[1].getY()-2,6,15);
if (rect.contains(Xs, Ys))ok=true;
rect=new Rectangle((int)pt[2].getX()-2,(int)pt[2].getY()-2,6,15);
if (rect.contains(Xs, Ys))ok=true;
if (ok)setCursor(hand); else setCursor(curs);}
public void destroy()
{ h.dispose(); ima.flush();}
}