|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

03-28-2008, 12:10 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 19
|
|
|
Alpha Fade Flicker
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
|
|

04-02-2008, 02:22 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 19
|
|
|
Anyone have any clues?? I'm used to Java automatically handling the Image Buffering, I use the swing toolkit. I've not as yet had to double buffer anything. Is this one of the cases where it would be needed?
|
|

04-02-2008, 05:04 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,189
|
|
// <applet code="FadeTest" width="300" height="300"></applet>
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 FadeTest extends JApplet
{
String file = "opened.wav";
Image source;
MediaTracker mt;
public void init()
{
mt = new MediaTracker(this);
source = getImage(getCodeBase(), "rico.jpg");
//"images/blackBear.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();
ImageScreen2 thescreen = new ImageScreen2(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 ImageScreen2 extends JPanel implements Runnable
{
Thread thread;
boolean filtering = false;
Image source;
int rule = AlphaComposite.SRC_OVER;
float alpha = 0;
float alphaInc = 0.01f;
public ImageScreen2(Image sourceImage)
{
source = sourceImage;
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(100);
}
catch (InterruptedException ex)
{
System.out.println("interrupted");
filtering = false;
}
alpha += alphaInc;
if(alpha < 0.01f || alpha > 0.99f)
alphaInc *= -1;
repaint();
}
}
protected void paintComponent(Graphics g)
{
// g.setColor(Color.white);
// g.fillRect(0,0,300,300);
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
Composite orig = g2.getComposite();
AlphaComposite ac = AlphaComposite.getInstance(rule, alpha);
g2.setComposite(ac);
g2.drawImage(source,0,0,this);
g2.setComposite(orig);
}
}
|
|

04-02-2008, 03:02 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 19
|
|
|
Dude you are a Java God! Thanks again for the help.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|