This java tip is real cool! It takes an image and fades it. You can use this code to create a slide show, as one picture fades out the other fades in.
Once again this is an applet so the HTML and the images used with this code is at the bottom of this tip.
Also one thing I forgot to tell you on the last tip. Due to the formatting of text on this message board, I have put a space in front of each HTML tag, so you have to remove it in order to save that file as an HTML file.
============================================
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.net.*;
public class FadeImage extends Applet {
Image img, faded;
int level, sign;
MediaTracker tracker;
AlphaFilter f;
FilteredImageSource fis;
public void init() {
level = 0;
sign = 15;
tracker = new MediaTracker(this);
try {
img = getImage(new URL(getDocumentBase(), "gumby.gif"));
tracker.addImage(img,0);
tracker.waitForID(0);
}
catch (Exception e) {
e.printStackTrace();
}
f = new AlphaFilter();
f.setLevel(level);
fis = new FilteredImageSource(img.getSource(), f) ;
FadeThread ft = new FadeThread();
ft.delayedFading(this, 20);
ft.start();
}
public void paint(Graphics g) {
if (faded != null) {
g.drawImage(faded,0,0,this);
}
}
public void fadeIt() {
Graphics g = this.getGraphics();
level += sign;
if (level < 0) {
level=0;
sign = sign * -1;
}
if (level > 255) {
level=255;
sign = sign * -1;
try {
Thread.sleep(1000);
}
catch (Exception e) {}
}
f.setLevel(level);
if (faded != null) faded.flush();
faded = this.createImage(fis);
tracker.addImage(faded,0);
try {
tracker.waitForID(0);
}
catch (Exception ex) {
ex.printStackTrace();
}
repaint();
}
class FadeThread extends Thread {
FadeImage fadeApplet;
int delay;
public void delayedFading(FadeImage f, int delay) {
this.fadeApplet = f;
this.delay = delay;
}
public void run() {
while (true) {
try {
sleep(delay);
fadeApplet.fadeIt();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
class AlphaFilter extends RGBImageFilter {
private int level;
public AlphaFilter() {
canFilterIndexColorModel = true;
}
public void setLevel(int lev) {
level = lev;
}
public int filterRGB(int x, int y, int rgb) {
int a = level * 0x01000000;
return (rgb & 0x00ffffff) | a;
}
}
}
=============== HTML File ==================
<
HTML>
< TITLE>< /TITLE>
< BODY>
<
APPLET CODE="FadeImage.class">
< /APPLET>
<
/BODY>< /HTML>
| File Attachments: |
|
GUMBY.GIF (8.3 KB)
|