1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Synthese extends JFrame{
private Image image;
private Image ImageR;
private Image ImageV;
private Image ImageB;
private Image ir;
private Image ib;
private Image iv;
private Container pane;
Synthese(){
this.setTitle("Synthese additive");
this.setSize(300,300);
this.setLocationRelativeTo(this);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pane = this.getContentPane();
pane.setLayout(new FlowLayout());
}
void dessinSynthese(URL URLImage,String nomImage){
/** Accès au toolkit : */
java.awt.Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
/** lecture de l'image : */
Image image = toolkit.getImage(URLImage);
Image ImageR = toolkit.createImage(URLImage);
Image ImageV = toolkit.createImage(URLImage);
Image ImageB = toolkit.createImage(URLImage);
System.out.println(ImageR.getHeight(null));
String nomImR = nomImage + "R";
String nomImV = nomImage + "V";
String nomImB = nomImage + "B";
//transformation de l'image en BufferedImage
BufferedImage ir = toBufferedImage(ImageR);
BufferedImage ib = toBufferedImage(ImageV);
BufferedImage iv = toBufferedImage(ImageB);
filtreR(ir);
filtreB(ib);
filtreV(iv);
JButton b = new JButton(new ImageIcon(ir)) ;
b.setSize(300,300);
JButton b2 = new JButton(new ImageIcon(ib)) ;
b2.setSize(300,300);
pane.add(b);
pane.add(b2);
this.setVisible(true);
}
BufferedImage toBufferedImage(Image imageinit) {
/** On test si l'image n'est pas déja une instance de BufferedImage */
if( imageinit instanceof BufferedImage ) {
/** cool, rien à faire */
return( (BufferedImage)imageinit );
} else {
/** On s'assure que l'image est complètement chargée */
image = new ImageIcon(imageinit).getImage();
ir = new ImageIcon(imageinit).getImage();
iv = new ImageIcon(imageinit).getImage();
ib = new ImageIcon(imageinit).getImage();
/** On crée la nouvelle image */
BufferedImage bufferedImage = new BufferedImage(
imageinit.getWidth(null),
imageinit.getHeight(null),
BufferedImage.TYPE_INT_RGB );
Graphics g = bufferedImage.createGraphics();
g.drawImage(image,0,0,null);
g.drawImage(ir,0,0,null);
g.drawImage(iv,0,0,null);
g.drawImage(ib,0,0,null);
g.dispose();
//filtreR(bufferedImage);
//System.out.println("taille : ");
return(bufferedImage);
}
}
void filtreR(BufferedImage imR){
int w = image.getWidth(null);
int h = image.getHeight(null);
for(int y = 0;y<h;y++){
for(int x = 0;x<w;x++){
int pixel = imR.getRGB( x, y );
imR.setRGB( x, y, pixel&0xFFFF0000 );
}
}
repaint();
}
void filtreB(BufferedImage imB){
int w = image.getWidth(null);
int h = image.getHeight(null);
for(int y = 0;y<h;y++){
for(int x = 0;x<w;x++){
int pixel = imB.getRGB( x, y );
imB.setRGB( x, y, pixel&0xFF0000F );
}
}
repaint();
}
void filtreV(BufferedImage imV){
int w = image.getWidth(null);
int h = image.getHeight(null);
for(int y = 0;y<h;y++){
for(int x = 0;x<w;x++){
int pixel = imV.getRGB( x, y );
imV.setRGB( x, y, pixel&0xFF00FF00 );
}
}
// repaint();
}
public void paintComponent(Graphics g){
Rectangle rect = this.getBounds();
if(ir != null) {
g.drawImage(ir, 0,0,ir.getWidth(null),ir.getHeight(null), null);
System.out.println("OK");
}
if(ib != null) {
g.drawImage(ib, 0,0,ib.getWidth(null),ib.getHeight(null), null);
}
else{System.out.println("image nulle ");}
// g.drawString("Name : " + nomIm,5,image.getHeight(null) + 20);
//g.drawString("width : " + image.getWidth(null) ,5,image.getHeight(null) + 40);
// g.drawString("height : " + image.getHeight(null),5,image.getHeight(null) + 60);
}
public static void main(String[] args) {
Synthese synthese = new Synthese();
}
} |
Partager