Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-28-2008, 12:10 AM
Member
 
Join Date: Mar 2008
Posts: 19
jamesfrize is on a distinguished road
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.

Code:
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
Attached Files
File Type: zip index.html.zip (897 Bytes, 3 views)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-02-2008, 02:22 AM
Member
 
Join Date: Mar 2008
Posts: 19
jamesfrize is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-02-2008, 05:04 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Code:
// <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); } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-02-2008, 03:02 PM
Member
 
Join Date: Mar 2008
Posts: 19
jamesfrize is on a distinguished road
Dude you are a Java God! Thanks again for the help.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
flicker angel_eyez New To Java 3 01-14-2008 11:52 PM
Flicker flicker! angel_eyez Java 2D 1 01-13-2008 08:52 AM
MegaBlock 0.0.1 alpha JavaBean Java Announcements 0 07-14-2007 09:25 PM
SableCC 4-alpha.2 JavaBean Java Announcements 0 07-05-2007 01:16 PM
SableCC 4-alpha.1 JavaBean Java Announcements 0 06-23-2007 08:24 PM


All times are GMT +3. The time now is 10:32 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org