|
|
Lesson 04 |
|
Double buffering |
|
|
|
|
|
[ Double buffering ]
In this tutorial you will learn how to use double buffering with Image and Graphics.
Well, it's ok now, but it should be better to work with an array of pixels .
If you want to use MemoryImageSource, go to lesson 5.
[ Code ]
// Lesson04
// Double buffering using Image & Graphics
// This code is better viewed with tab size=4
import java.applet.Applet;
import java.awt.*;
import java.net.*;
public class Lesson04 extends Applet implements Runnable
{
Color txtColor; // Text color
Image image; // Image
URL link; // Link
Thread thread; // Thread
int x,y; // Text position
boolean threadRunning; // thread running ?
boolean threadSuspended; // thread suspended ?
int width,height; // applet width,height
Image backImage; // back Image
Graphics backGraphics; // back Graphics
public void init()
{
width=Integer.parseInt(getParameter("width"));
height=Integer.parseInt(getParameter("height"));
txtColor = Color.white;
x = 10;
y = 20;
// Load Image
image=getImage(getDocumentBase(),getParameter("image"));
MediaTracker mediatracker = new MediaTracker(this);
mediatracker.addImage(image, 0);
try { mediatracker.waitForID(0); }
catch(InterruptedException ex) {}
mediatracker=null;
// Init link
try { link=new URL(getParameter("link")); }
catch (MalformedURLException e) { System.err.println(e); }
// Init double buffering
backImage = createImage(width, height);
backGraphics = backImage.getGraphics();
threadSuspended=false;
}
public void start() {
if(thread == null)
thread = new Thread(this);
threadRunning=true;
thread.start();
}
public void run()
{
while (threadRunning)
{
try
{
Thread.sleep(20); // sleep a while
synchronized(this)
{
while (threadSuspended) { wait(); } // thread suspended ?
}
}
catch(InterruptedException ex) {} // catch exception
x = (x+1)%200;
repaint(); // repaint will call update
}
}
public void stop() {
if(thread != null) {
threadRunning=false;
//thread.stop(); // this method has been deprecated
thread = null;
}
}
public void destroy()
{
}
public void paint(Graphics g)
{
backGraphics.drawImage(image, 0, 0, this);
backGraphics.setColor(txtColor);
backGraphics.drawString("Lesson04",x-50,y);
g.drawImage(backImage, 0, 0, this);
}
public void update(Graphics g)
{
paint(g);
}
public synchronized boolean mouseEnter(Event event, int i, int j) {
threadSuspended=true; // Suspend thread
showStatus(link.toString()); // Show link in status bar
setCursor(new Cursor(Cursor.HAND_CURSOR)); // Change mouse cursor
return true;
}
public synchronized boolean mouseExit(Event event, int i, int j) {
threadSuspended=false; //
notify(); // notify
setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); // Change mouse cursor
return true;
}
public boolean mouseDown(Event event, int i, int j) {
getAppletContext().showDocument(link,"_blank"); // Open link
return true;
}
}
|
[ HTML TAG ]
<applet code="Lesson04.class" codebase = "lesson04" width="150" height="150">
<param name="image" value="lesson04/flare.jpg">
<param name="link" value="http://java.sun.com">
</applet>
|
[ Source ]
- Lesson.java
|
|
|
|
|
|
|