Gestion des
transformations
sous Java 2D
Utilisation des transformations géométriques pour la programmation d'une animation.
Transformations.class
Fichiers source
import java.awt.*; import java.awt.geom.*; import java.applet.*; public class Transformations extends Applet { public double a = 0.0 ; public double sx = 0.5 ; public double sy = 1.0 ; public void init() { setBackground(Color.white); ThreadTransformations tt = new ThreadTransformations(this); tt.start(); } public void paint(Graphics g) { Graphics2D g2 =(Graphics2D) g; AffineTransform at = new AffineTransform(); at.translate(125.0,125.0); at.rotate(a); at.scale(sx,sy); g2.setTransform(at); Rectangle r = new Rectangle(-75,-75,150,150); g2.draw(r); } public String getAppletInfo() { return "Transformations geometriques sous Java 2D."; } }
public class ThreadTransformations extends Thread { private Transformations t ; private boolean bRun ; private double dx = 0.01 ; private double dy = -0.013 ; public ThreadTransformations(Transformations trans) { super(); t = trans; bRun = false; } public void run() { bRun = true; while (bRun) { try { sleep(15); } catch(InterruptedException ie) {} ; t.a += 0.01; if ( ( t.sx <= 0.3 ) | ( t.sx >= 1.2 ) ) dx = -dx ; t.sx += dx; if ( ( t.sy <= 0.3 ) | ( t.sy >= 1.2 ) ) dy = -dy ; t.sy += dy; t.repaint(); } } }