User:Oskar Sigvardsson/TestPanel.java
//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);
}
}
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.