I'm tryin' to make an applet that displays a slowly fading image against a white background. It works ish, it's just a bit pants! The background is all grey and it flickers like a bugger. I'm used to using the Toolkit which automatically handles all the Image stuff for me.
I've added an HTML file to run the main class, Unfortunately the images were 2 big to upload here.
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import static java.lang.Thread.*;
import java.applet.*;
import java.awt.event.*;
import java.net.*;
public class index extends JApplet
{
String file = "opened.wav";
Image source;
MediaTracker mt;
public void init()
{
mt = new MediaTracker(this);
source = getImage(getCodeBase(), "rico.jpg");
mt.addImage(source,1);
try
{
mt.waitForAll();
}
catch(Exception e)
{
System.out.println("read error: " + e.getMessage());
}
setBounds (0,0,300,325);
Container c = getContentPane();
Image3screen thescreen = new Image3screen(source);
c.setLayout(new BorderLayout());
c.add(thescreen, BorderLayout.CENTER);
addMouseListener
(
new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
AudioClip ac = getAudioClip(getCodeBase(), file);
ac.play();
try
{
AppletContext a = getAppletContext();
URL u = new URL("http://jamesfrize.43host.info/sonodrome.html");
a.showDocument(u,"_self");
}
catch (Exception e)
{
System.out.println("read error: " + e.getMessage());
}
}
}
);
}
}
class Image3screen extends JPanel implements Runnable
{
int alphaLevel = 0;
int alphaInc = 5;
AlphaFilter af;
Thread thread;
boolean filtering = false;
Image source, result;
public Image3screen(Image sourceImage)
{
source = sourceImage;
result = source;
alphaLevel = alphaInc;
start();
}
private void start()
{
if(!filtering)
{
filtering = true;
thread = new Thread(this);
thread.setPriority(NORM_PRIORITY);
thread.start();
}
}
public void run()
{
while(filtering)
{
try
{
sleep(50);
}
catch (InterruptedException ex)
{
System.out.println("interrupted");
filtering = false;
}
alphaLevel += alphaInc;
if(alphaLevel < 5 || alphaLevel > 250)
alphaInc *= -1;
filterImage();
repaint();
}
}
private void filterImage()
{
result = createImage(new FilteredImageSource(source.getSource(),
new AlphaFilter(alphaLevel)));
}
public void paintComponent(Graphics g)
{
g.setColor(Color.white);
g.fillRect(0,0,300,300);
g.drawImage(result,0,0,this);
}
}
class AlphaFilter extends RGBImageFilter
{
int alphaLevel;
public AlphaFilter(int alpha)
{
alphaLevel = alpha;
canFilterIndexColorModel = true;
}
public int filterRGB(int x, int y, int rgb)
{
int alpha = (rgb >> 24) & 0xff;
alpha = (alpha * alphaLevel) / 255;
return ((rgb & 0x00ffffff) | (alpha << 24));
}
}
Cheers, :{p