import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;     
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;

public class RedLeaves extends Applet 
                        implements ActionListener {
  int           WIDTH, HEIGHT;                  
  Image         imageInput,imageOutput;         
  int[]         pixelInput, pixelOutput;        
  int selection;
  
  Button button1 = new Button("元の画像");
  Button button2 = new Button("紅葉");  

  Panel panel   = new Panel();

  
  
  public void init(){
    
    panel.add(button1);
    panel.add(button2);
    panel.setLayout(new GridLayout(1,2));
    add(panel);

   selection = 0; 

   button1.addActionListener(this);
   button2.addActionListener(this);


    
    loadImage("GREEN1.jpg");

      int index = 0;
      int alpha;
      int Red;
      int Green;
      int Blue;
      float hsb[] = new float[3];
       for (int y = 0; y < HEIGHT; y++) {
       for (int x = 0; x < WIDTH; x++) {
            alpha = (pixelInput[y * WIDTH + x] & 0xff000000);
            Red = (pixelInput[y * WIDTH + x] & 0x00ff0000)>>16;
            Green = (pixelInput[y * WIDTH + x] & 0x0000ff00)>>8;
            Blue = (pixelInput[y * WIDTH + x] & 0x000000ff);

        Color.RGBtoHSB (Red,Green,Blue,hsb);
         if( (hsb[0] <= 0.4) && (hsb[0] >= 0.1) ){
             if (hsb[0]>=(1.0-0.88)){
                hsb[0]= hsb[0]-(float)1.0;}
           pixelOutput[index++] =(alpha | Color.HSBtoRGB(hsb[0]+(float)0.88,hsb[1]+(float)0.2,hsb[2]));}
         else
           {pixelOutput[index++] = (alpha | Color.HSBtoRGB(hsb[0],hsb[1],hsb[2]));}

         }
       }
    imageOutput = createImage(new MemoryImageSource(WIDTH, HEIGHT,
                                                    pixelOutput,0,WIDTH));


    repaint();
  }
  

   
  public void paint(Graphics g){
   switch (selection) {
     case 0:  g.drawImage(imageInput, 40,25,this); 
      break;
     case 1:  g.drawImage(imageOutput,40,25,this); 
      break;
     }
   }
 
  
  
  public void loadImage(String s){
    MediaTracker        mt;

    imageInput = getImage(getDocumentBase(), s);

   
    mt = new MediaTracker(this);
    mt.addImage(imageInput,0);                 )
  
  try {                                        
      mt.waitForID(0);                          
    } catch (InterruptedException e){
      return;
    }
 
    HEIGHT = imageInput.getHeight(this);        
    WIDTH  = imageInput.getWidth(this);

    pixelInput  = new int[WIDTH * HEIGHT];      
    pixelOutput = new int[WIDTH * HEIGHT];

  PixelGrabber pg = new PixelGrabber(imageInput,0,0,WIDTH,HEIGHT,pixelInput,0,WIDTH);
      

    try {                               
      pg.grabPixels();
    } catch (InterruptedException e) {
      System.err.println("interrupted waiting for pixels!");
      return;
    }
  }



 public void actionPerformed(ActionEvent e){
      if(e.getActionCommand().equals("元の画像")){
          selection = 0;}
         else if(e.getActionCommand().equals("紅葉")){
          selection = 1;}
       repaint();
      }
}