viewing an image

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;
}
}
 
H

hiwa

This my version works. Diffrence is very small.
<code>
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 = "background.jpg";
//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{
//contentPane = (JPanel) this.getContentPane();
Container contentPane = this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Visualisation une image");
contentPane.add(jPanel, BorderLayout.WEST); //what for ??
contentPane.add(new ImageCanvas(getToolkit().getImage(filename)),
BorderLayout.CENTER);
setVisible(true);
}

//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);
}
}

public static void main(String[] args){
Cadre cadre = new Cadre();
}
}

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;
}
}
</code>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top