View Single Post
  #2 (permalink)  
Old 03-25-2008, 06:31 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.image.*; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; import static java.lang.Thread.*; public class TestAlpha extends JFrame { private TestAlpha() { setBounds (0,0,300,325); setDefaultCloseOperation(EXIT_ON_CLOSE); Container c = getContentPane(); Image3screen thescreen = new Image3screen(); c.setLayout(new BorderLayout()); c.add(thescreen, BorderLayout.CENTER); } public static void main (String[] args) { TestAlpha img1 = new TestAlpha(); img1.setVisible(true); } } class Image3screen extends JPanel implements Runnable { private Image source, result; private int alphaLevel = 0; int alphaInc = 10; private AlphaFilter af; Thread thread; boolean filtering = false; public Image3screen() { try { source = ImageIO.read(new File(//"rico.jpg")); "taiChi.jpg")); } catch(IOException e) { System.out.println("read error: " + e.getMessage()); } result = source; // repaint(); nothing showing yet/nothing to paint 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(200); } catch (InterruptedException ex) { System.out.println("interrupted"); filtering = false; } alphaLevel += alphaInc; if(alphaLevel < 10 || alphaLevel > 90) alphaInc *= -1; //System.out.println("alphaLevel = " + alphaLevel); filterImage(); } } private void filterImage() { result = createImage(new FilteredImageSource(source.getSource(), new AlphaFilter(alphaLevel))); repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(result,0,0,this); } } class AlphaFilter extends RGBImageFilter { private 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)); } }
Reply With Quote