R
Riri
Hello, i'd like to see an image and get its rgb filters in order to watch
the three composants r g b of it.
Here is my code, but i can't even see a picture, the result is an empty
frame... why?
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
import java.net.*;
import java.util.*;
public class Cadre extends JFrame {
private JPanel contentPane;
private BorderLayout borderLayout1 = new BorderLayout();
private JPanel jPanel = new JPanel();
private String filename = "D:/Temp/mandrill";
//Construire le cadre
public Cadre() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Initialiser le composant
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Cadre.class.getResour
ce("[Votre icône]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Visualisation une image");
contentPane.add(jPanel, BorderLayout.WEST);
contentPane.add("Center", new
ImageCanvas(getToolkit().getImage(filename)));
}
//Supplanté, ainsi nous pouvons sortir quand la fenêtre est fermée
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
class ImageCanvas extends Canvas {
Image gradientImage;
ImageCanvas(Image image) {
GradientFilter imgf = new GradientFilter();
ImageProducer ip = image.getSource();
ip = new FilteredImageSource(ip, imgf);
gradientImage = getToolkit().createImage(ip);
repaint();
}
public void paint(Graphics g) {
g.drawImage(gradientImage, 0, 0, this);
}
}
class GradientFilter extends RGBImageFilter {
float[] hsb = new float[3];
int width, height;
public void setDimensions(int w, int h) {
super.setDimensions(w, h);
width = w;
height = h;
}
public void filterRGBPixels(int x, int y, int w, int h,
int pixels[], int offset, int scansize) {
int i = offset;
for (int cy = 0; cy < h; cy++) {
for (int cx = 0; cx < w; cx++) {
Color c = new Color(pixels);
Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), hsb);
hsb[2] += .5f - (float)x / width;
hsb[2] += .5f - (float)y / height;
hsb[2] = Math.max(0.0f, Math.min(1.0f, hsb[2]));
pixels = Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]);
i++;
}
i += scansize - w;
}
consumer.setPixels(x, y, w, h, ColorModel.getRGBdefault(),
pixels, offset, scansize);
}
// Never called.
public int filterRGB(int x, int y, int rgb) {
return 0;
}
}
the three composants r g b of it.
Here is my code, but i can't even see a picture, the result is an empty
frame... why?
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
import java.net.*;
import java.util.*;
public class Cadre extends JFrame {
private JPanel contentPane;
private BorderLayout borderLayout1 = new BorderLayout();
private JPanel jPanel = new JPanel();
private String filename = "D:/Temp/mandrill";
//Construire le cadre
public Cadre() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Initialiser le composant
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Cadre.class.getResour
ce("[Votre icône]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Visualisation une image");
contentPane.add(jPanel, BorderLayout.WEST);
contentPane.add("Center", new
ImageCanvas(getToolkit().getImage(filename)));
}
//Supplanté, ainsi nous pouvons sortir quand la fenêtre est fermée
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
class ImageCanvas extends Canvas {
Image gradientImage;
ImageCanvas(Image image) {
GradientFilter imgf = new GradientFilter();
ImageProducer ip = image.getSource();
ip = new FilteredImageSource(ip, imgf);
gradientImage = getToolkit().createImage(ip);
repaint();
}
public void paint(Graphics g) {
g.drawImage(gradientImage, 0, 0, this);
}
}
class GradientFilter extends RGBImageFilter {
float[] hsb = new float[3];
int width, height;
public void setDimensions(int w, int h) {
super.setDimensions(w, h);
width = w;
height = h;
}
public void filterRGBPixels(int x, int y, int w, int h,
int pixels[], int offset, int scansize) {
int i = offset;
for (int cy = 0; cy < h; cy++) {
for (int cx = 0; cx < w; cx++) {
Color c = new Color(pixels);
Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), hsb);
hsb[2] += .5f - (float)x / width;
hsb[2] += .5f - (float)y / height;
hsb[2] = Math.max(0.0f, Math.min(1.0f, hsb[2]));
pixels = Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]);
i++;
}
i += scansize - w;
}
consumer.setPixels(x, y, w, h, ColorModel.getRGBdefault(),
pixels, offset, scansize);
}
// Never called.
public int filterRGB(int x, int y, int rgb) {
return 0;
}
}