Dessin off line
et double buffer
DessinOffLine.class
Fichiers source
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.applet.*;
import java.net.*;
public class DessinOffLine extends Applet implements MouseListener {
public double a = 0.0 ;
private Image i = null;
private boolean db = false;
public void init() {
setBackground(Color.white);
try {
URL url = this.getDocumentBase();
String adresse = "http://"+url.getHost()+"/IG/Java2D/Images/R1.gif" ;
i = getImage(new URL(adresse)) ; }
catch (MalformedURLException e) { }
ThreadDessinOffLine tdol = new ThreadDessinOffLine(this);
tdol.start();
addMouseListener(this);
}
public void paint(Graphics g) {
Graphics2D g2 =(Graphics2D) g;
BufferedImage bi = new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics2D g2bi =(Graphics2D) bi.getGraphics();
g2bi.setColor(Color.white);
g2bi.fill(new Rectangle(0,0,getWidth(),getHeight()));
AffineTransform at = new AffineTransform();
at.translate(getWidth()/2,getHeight()/2);
at.rotate(a);
g2bi.setTransform(at);
g2bi.setColor(Color.blue);
if ( i != null ) {
int tx = i.getWidth(this);
int ty = i.getHeight(this);
g2bi.draw(new Rectangle(-tx/2-1,-ty/2-1,tx+2,ty+2));
g2bi.drawImage(i,-tx/2,-ty/2,this);
g2.drawImage(bi,0,0,this); }
}
public void update(Graphics g) {
if ( !db ) {
Graphics2D g2 =(Graphics2D) g;
g2.setColor(Color.white);
g2.fill(new Rectangle(0,0,getWidth(),getHeight())); }
paint(g);
}
public String getAppletInfo() {
return "Dessin offline et double buffer.";
}
public void mouseClicked(MouseEvent e) {
db = !db;
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
public class ThreadDessinOffLine extends Thread {
private DessinOffLine dol ;
private boolean bRun ;
public ThreadDessinOffLine(DessinOffLine dol) {
super();
this.dol = dol;
bRun = false;
}
public void run() {
bRun = true;
while (bRun) {
try {
sleep(150); }
catch(InterruptedException ie) {} ;
dol.a += 0.05;
dol.repaint(); }
}
}