Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Applets Code Examples

Fade an Image

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
Fade an Image 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; } } }