//******************************************************************************
// -----------------------------------------------------------
// tipsImg4.java ( Thread et Double Buffer )
// -----------------------------------------------------------
// Comments : very simple drawimage applet
// with MediaTracker and double buffer
// -----------------------------------------------------------
// Author : R. BERTHOU
// E-Mail : rbl@berthou.com
// URL : http://www.javaside.com
// -----------------------------------------------------------
// 1.00 * R.BERTHOU * 16/04/2000 * samples tips
//******************************************************************************
// Importations
import java.awt.Graphics ;
import java.awt.Image ;
import java.awt.MediaTracker;
import java.awt.Color;
import java.awt.Image;
import java.awt.Event;
public class tipsImg4 extends java.applet.Applet
implements Runnable
{
// Variables
Image img ; // ...
Thread th ;
long delay = 5 ;
int xPos = 0 ;
int yPos = 0 ;
int aW = -1 ;
int aH = -1 ;
int iW = -1 ;
int iH = -1 ;
int cX = 1 ;
int cY = 1 ;
// true => double buffer active
boolean b = true ;
// for double buffering to prevent flicker
Image offScreenImage;
Graphics offScreen;
// Initialisation de l'applet
public void init() {
MediaTracker trk = new MediaTracker(this) ;
// Lecture de l'image
img = getImage(getCodeBase(), "./photo.gif") ;
// Ajout de l'image dans le MediaTracker
trk.addImage(img, 0) ;
try {
trk.waitForAll() ;
} catch (InterruptedException e) { }
}
// control the starting of the applet
public void start() {
// create thread
th = new Thread(this);
// start the thread
th.start();
} // end of start
// control the stopping of the applet
public void stop() {
th.stop();
} // end of stop
// control the running of the applet
public void run() {
// set the priority of the thread to low
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
// loop continuously
while(true) {
// values for working out the constant delay
long thisTick, waitTick;
// calculate the tick to wait for
waitTick = System.currentTimeMillis() + delay;
// update the screen
xPos += cX ;
yPos += cY ;
if (cX == 1) {
if ( (xPos+iW) > aW) cX = -1 ;
}
else
if ( xPos < 0) cX = 1 ;
if (cY == 1) {
if ( (yPos+iH) > aH) cY = -1 ;
}
else
if ( yPos < 0) cY = 1 ;
repaint ();
thisTick = System.currentTimeMillis();
if ( thisTick<waitTick ) {
try {
Thread.currentThread().sleep( (int)(waitTick-thisTick));
} catch (InterruptedException e) {}
}
} // end of loop
} // end of run
// if you click on the applet you active or remove double buffer
public boolean mouseDown(Event e, int x, int y){
b = !b ;
return true ;
}
// Called when the applet needs to be painted
// calls the flicker free updating system
public void paint (Graphics g) {
update(g);
} // end of paint
// Draw the applet without flicker
public synchronized void update(Graphics g) {
// get the size of the applet
int xW = size().width ;
int xH = size().height ;
if ((xW != aW) || (xH != aH) || (offScreen == null)) {
// initialise the double buffering screen
try {
offScreenImage = createImage (xW, xH);
offScreen = offScreenImage.getGraphics ();
} catch (Exception e) {
offScreen = null;
}
aW = xW ;
aH = xH ;
// get Image size
iW = img.getWidth(this) ;
iH = img.getHeight(this) ;
}
if ((offScreen!=null) && b) {
paintApplet(offScreen);
g.drawImage(offScreenImage,0,0,this);
} else
paintApplet(g);
} // end of update
// Draw applet
public void paintApplet(Graphics g) {
g.setColor(Color.white ) ;
g.fillRect(0, 0, aW, aH) ;
g.drawImage(img, xPos, yPos, this) ;
g.setColor(Color.red ) ;
g.drawString("double buffer : " + b, 10, yPos) ;
}
}
Generated by srcColor