Code source de l'applet "AFFINES"
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
public class affines extends Applet implements Runnable
{ private static final long serialVersionUID = 1420972181089580L;
static final double PI= Math.PI;
Thread runner = null;
int anim=15;
Font font = new Font("SansSerif",1,14);
Font small = new Font("Serif",0,9);
Image ima,logo;
BufferedImage bufima = new BufferedImage(560,240,BufferedImage.TYPE_INT_RGB);
Graphics h;
int la,ha,lima,hima;
double alpha=0;
float dec=50,sca=1,dx=100;
FlowLayout fl= new FlowLayout(1,25,5);
Label lb=new Label("Transformations affines");
boolean plus=true,monte;
String s="Essai de Texte";
Color col=new Color(0,0,255,80);
public void init()
{ resize(600,300);
setBackground(Color.lightGray);
setFont(font);
la=getWidth(); ha=getHeight();
ima=createImage(la,ha);
h=ima.getGraphics();
MediaTracker tracker = new MediaTracker(this); //recherche de l'image
logo = getImage(getDocumentBase(),"logo_java.gif");
tracker.addImage(logo,0);
try {tracker.waitForAll();}
catch (InterruptedException e) { }
lima=logo.getWidth(this);
hima=logo.getHeight(this);
setLayout(fl);
add(lb);}
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)
{ Graphics2D g2=(Graphics2D)h;
g2.setColor(Color.lightGray);
g2.fillRect(0,0,la,ha);
Graphics2D g2bufima =(Graphics2D) bufima.getGraphics();
g2bufima.setColor(Color.cyan);
g2bufima.setFont(font);
g2bufima.fill(new Rectangle(0,0,560,240));
g2bufima.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2bufima.setColor(Color.black);
g2bufima.drawString(s,450,230);
AffineTransform afftran = new AffineTransform(); //T1
afftran.translate(dec,150);
afftran.scale(sca,sca);
afftran.rotate(alpha);
g2bufima.setTransform(afftran);
g2bufima.setColor(Color.red);
g2bufima.draw(new Rectangle(-lima/2-1,-hima/2-1,lima+2,hima+2));
g2bufima.drawImage(logo,-lima/2,-hima/2,this);
g2bufima.drawString(s,-50,-35);
alpha+=0.05;
dec+=0.3f;
if (dec>500)dec=50;
if (plus)sca*=1.003; else sca/=1.003;
if (sca>1.2)plus=false;
if (sca<0.8)plus=true;
afftran = new AffineTransform(); //T2
if (dx<50)monte=false;
if (dx>120)monte=true;
if (monte)dx-=0.2f; else dx+=0.2f;
afftran.translate(0,dx);
g2bufima.setTransform(afftran);
g2bufima.setColor(col);
g2bufima.fill(new Ellipse2D.Float(250,0,100,80));
afftran = new AffineTransform(); //T3
afftran.rotate(-PI/2);
g2bufima.setTransform(afftran);
g2bufima.setColor(Color.magenta);
g2bufima.drawString(s,-150,30);
g2.drawImage(bufima,20,40,this); //ecriture de bufima dans g2 (ie h)
h.setColor(Color.black); h.drawRect(0,0,la-1,ha-1);
h.setFont(small); h.drawString("jjR 01-2012",25,ha-25);
h.drawLine(50,50,550,80);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
g2bufima.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
cadre3D(20,40,la-20,ha-20);
g.drawImage(ima,0,0,this); //affichage de h
showStatus("J. J. Rousseau 01-2012");}
public void start()
{ if (runner == null)
{ runner = new Thread(this);
runner.start();}}
public void stop()
{ if (runner != null) runner = null;}
public void run()
{ Thread bidon=Thread.currentThread();
while (runner==bidon){
try{
repaint();
Thread.sleep(anim);}
catch (InterruptedException e){stop();}}}
public void destroy()
{ h.dispose(); ima.flush();}
}