import java.awt.image.*;
import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.*;
import java.io.*;

/**
 * <b>TP Flux octets Exo 1. </b> :
 *
 * @version 1
 * @author Katia AUXILIEN
 */
public class FluxImage extends JComponent {
  private BufferedImage img = new BufferedImage(768,1024,BufferedImage.TYPE_3BYTE_BGR); 

  /**
   * ..
   *
   * @param args ..
   */
 
    public FluxImage() {
      super();
      int rgb,r,g,b;
      try{
        BufferedInputStream fis = new BufferedInputStream(new FileInputStream("image.bin"));
        for(int y=0;y<1024;y++){
          for (int x=0;x<768;x++) {
            r=fis.read()<<16;
            g=fis.read()<<8;
            b=fis.read();
            rgb = r | g | b;
            //System.out.println(x+","+y+":"+x*y);
            this.img.setRGB(x,y,rgb);
          }
        }
        fis.close();
      }
      catch(IOException e){
        System.out.println("Message d'erreur");
      }

    }


  @Override
  protected void paintComponent(Graphics pinceau) {
    Graphics secondPinceau = pinceau.create();
    if(this.isOpaque()){
      secondPinceau.setColor(this.getBackground());
      secondPinceau.fillRect(0,0,this.getWidth(),this.getHeight());
    }
    secondPinceau.drawImage(this.img,0,0,this);
  }

}

/* PROF :
private int width,height;
  private String path;
  private Image image;

  public FluxImage(int width,int height, String path) throws IOException {
    this.width=width;
    this.height=height;
    this.path = path;
    this.image = readFromFile(width,height,path);
    setPreferredSize(new Dimension(width,height));

    this.repaint();

  }

  private Image readFromFile(int width, int height, String path) throws IOException {
    FileInputStream input = new FileInputStream(path);
    BufferedImage img = new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);

    for(int i = 0; i < width ; i++){
      for (int j = 0;j < width ;j++) {
        int r = input.read();
        int g = input.read();
        int b = input.read();
        //on pourrait ajouter une condition de vérification des valeurs rgb
        int rgb = (r <<16)|(g << 8)|(b); //bit à bit on fait | pas || !!!
        img.setRGB(j,i,rgb);
      }
    }
    return img;
  }

  @Override
  protected void paintComponent(Graphics pinceau) {
    Graphics secondPinceau = pinceau.create();
    if (this.isOpaque()) {
      secondPinceau.setColor(this.getBackground());
      secondPinceau.fillRect(0, 0, this.getWidth(), this.getHeight());
    }
    secondPinceau.drawImage(this.image, this.height,this.width, this);
  }

}*/