User:Oskar Sigvardsson/TestPanel.java

From Wikipedia, the free encyclopedia
//This program opens a window, shows an image, and creates an animation that makes it redder by the second
package testwindow;

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class TestPanel extends JPanel implements Runnable {

    boolean first = true;
    int[] pixels;
    int w, h;
    MemoryImageSource source;
    Image image, originalImage;
    String filename;
    
    public TestPanel(String filename) {
        this.filename = filename;
        Thread t = new Thread(this);
        t.start();
    }
    
    public void run() {
        for(int i = 0; i<255; i++) {
            try {
                //Updates it so that it's approx. 24 fps
                Thread.sleep(1000 / 24);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            
            //If the image hasn't been loaded yet, do nothing
            if(pixels != null)
                //Updates the raster
                processPixels();
            
            if(source != null)
                source.newPixels();
            
        }
    }
    
    private void loadPixels() {
        //If the source-image hasn't been loaded yet, try to load it
        if(originalImage == null)
            originalImage = Toolkit.getDefaultToolkit().getImage(filename);
        
        w = originalImage.getWidth(this);
        h = originalImage.getHeight(this);
        
        //Same thing, if the source-image still hasn't been loaded, do nothing, and try to load it next cycle
        if(w == -1 || h == -1) {
            first = true;
            return;
        }
        
        //Initilize the pixel-array
        pixels = new int[w*h];
        //Initilize PixelGrabber
        PixelGrabber pg = new PixelGrabber(originalImage, 0, 0, w, h, pixels, 0, w);
        try {
            //Grab the pixels
            pg.grabPixels();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        //Create the MemoryImageSource and the image
        source = new MemoryImageSource(w, h, pixels, 0, w);
        source.setAnimated(true);
        image = Toolkit.getDefaultToolkit().createImage(source);
    }
    
    private void processPixels() {
        for(int i = 0; i<pixels.length; i++) {
            //Extract the red component
            int r = (pixels[i] >> 16) & 0xff;
            //Increase red component
            r++;
            if(r>255)
                r =255;
            
            //Put the increased red component back
            pixels[i] = (255 <<24) | (pixels[i] & 0xffff) | ((r & 0xFF) << 16);
        }
    }
    
    public void paint(Graphics g) {
        //If this is run before the image have been initialized, load it
        if(first) {
            first = false;
            loadPixels();
        }
        g.drawImage(image, 0, 0, this);
    }
    
    public static void main(String[] args) {
        //Create a window for the panel
        JFrame jf = new JFrame("Test");
        jf.setSize(400, 400);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //Replace with any image on your harddrive to see results
        TestPanel panel = new TestPanel("Some image file");
        
        jf.add(panel);
        jf.setVisible(true);
    }
}