JAVA: Image Filter Starter Code

//There are four classes here


  import javax.swing.JFrame;

public class ImageStarter extends JFrame {

    public ImageStarter()
    {
        add(new ImageFilter());
        setTitle("Image Filter");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(800,500);
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(false);
    }

    public static void main(String[] args) {
        new ImageStarter();
    }
}
 


  


 
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.Timer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.imageio.*;
import java.awt.image.*;
import java.io.*;
import java.io.DataInput;
 import java.io.FileNotFoundException;
import java.io.IOException;
 import java.io.RandomAccessFile;
import javax.imageio.stream.FileImageInputStream; 
import javax.imageio.stream.FileImageOutputStream; 




public class ImageFilter  extends JPanel implements Runnable, MouseListener
{
boolean ingame = true;
private Dimension d;
int BOARD_WIDTH=800;
int BOARD_HEIGHT=500;
BufferedImage img;
boolean start = true;
boolean imgReady = false;
 private Thread animator;
 int count = 0;
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
String tempS = "z";
BufferedImage img2;
boolean iImgReady = false;
ExamineImage ei = new ExamineImage();
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
           int x = 100;
        int y = 200;
         
 
 
    public ImageFilter()
    {
          addKeyListener(new TAdapter());
         addMouseListener(this);
        setFocusable(true);
        d = new Dimension(BOARD_WIDTH, BOARD_HEIGHT);
        setBackground(Color.black);
       
     
           
            if (animator == null || !ingame) {
               animator = new Thread(this);
               animator.start();
            }
                    
  
        setDoubleBuffered(true);
    }
    
     public void selectFile() {
 
        //Handle open button action.
       
       
            ChooseFile.starter();
           
        }
    
  
    
    
    public void imageSelect(){
    if(ChooseFile.sourceFolder.equals("z")){
        //don't do anything
    }else{
        setImages();
    }
    
}

   public void setImages(){
      
       
        try {
           
              img =   ImageIO.read(new FileImageInputStream(new File(ChooseFile.sourceFolder)));
              //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
               tempS = ChooseFile.sourceFolder;
              ei.rgb(ChooseFile.sourceFolder);
              //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
               
                imgReady=true;
            } catch (IOException e) {
            System.out.println("Image could not be read");
            // System.exit(1);
            }
            
       
    }
    
    
    
     public void image2Ready(){ //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
       //ChooseFile.theFile
       if(!(ExamineImage.iImgName.equals("z"))){
          System.out.println("Image Inverted");
        try {
            //    img = ImageIO.read(this.getClass().getResource(ChooseFile.sourceFolder));
              img2 =   ImageIO.read(new FileImageInputStream(new File(ExamineImage.iImgName)));
               iImgReady=true;
            } catch (IOException e) {
            System.out.println("Image could not be read");
            // System.exit(1);
            }
        } 
       
    }//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    
    public void paint(Graphics g)
{
super.paint(g);

   g.setColor(Color.white);
   g.fillRect(0, 0, d.width, d.height);
   
        
   
    if (ingame) {
       //button for selecting file
      g.setColor(Color.gray);
      g.fillRect(50, d.height-75, 100, 25);
        Font small = new Font("Helvetica", Font.BOLD, 14);
        FontMetrics metr = this.getFontMetrics(small);
        g.setColor(Color.white);
        g.setFont(small);
        g.drawString("Select File", 55, d.height-60);
        //select file
        
       
        
      if(imgReady==true){
        // System.out.println("TRUE");&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
        g.drawImage(img,0,0,400,400 ,null);
        if( tempS != ChooseFile.sourceFolder){
            setImages();
            iImgReady = false;
        }
        
         //button for inverting image
         g.setColor(Color.gray);
         g.fillRect(250, d.height-75, 100, 25);
         g.setColor(Color.white);
        g.drawString("Invert Image", 255, d.height-60);
        //invert image&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
        
        if(iImgReady == true){//invert image&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
        
                  g.drawImage(img2,420,0,400,400 ,null);
              
            }else{
                // ei.image(ei.rr,ei.gg,ei.bb,ei.lumm);
                 image2Ready(); 
           
        }//invert image&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
        
    }else{
        
        imageSelect();
    }
      
        
   
    }
      Toolkit.getDefaultToolkit().sync();
       g.dispose();
}



private class TAdapter extends KeyAdapter {

    
    
public void keyReleased(KeyEvent e) {
// player.keyReleased(e);
 int key = e.getKeyCode();
 
      if(key==39){
         
        }
        
         if(key==37){
          
        }
        
    
     
}

public void keyPressed(KeyEvent e) {
System.out.println( e.getKeyCode());
   // message = "Key Pressed: " + e.getKeyCode();
    int key = e.getKeyCode();
    
        if(key==39){
          
        }
        
        
        if(key==37){
         
        }
        
        
          if(key==38){
            
            }
        
      // if(key

}

}



public void mousePressed(MouseEvent e) {
    int mx = e.getX();
     int my = e.getY();
    // g.fillRect(50, d.height-75, 100, 25);
    if(mx > 50 && mx < 150 && my > d.height-75 && my < d.height-75 + 25){
        selectFile();
    }
   
    //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
      if(mx > 250 && mx < 350 && my > d.height-75 && my < d.height-75 + 25){
        System.out.println("Invert");
        ei.image(ei.rr,ei.gg,ei.bb,ei.lumm);
                 image2Ready(); 
    }
   //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
         //  ei.lum(sourceFolder);

}

public void mouseReleased(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void mouseClicked(MouseEvent e) {

}

public void run() {

long beforeTime, timeDiff, sleep;

beforeTime = System.currentTimeMillis();
 int animationDelay = 10;
 long time = 
            System.currentTimeMillis();
    while (true) {//infinite loop
     // spriteManager.update();
      repaint();
      try {
         time += animationDelay;
          Thread.sleep(Math.max(0,time - 
          System.currentTimeMillis()));
      }catch (InterruptedException e) {
        System.out.println(e);
      }//end catch
    }//end while loop

    


}//end of run

  

}//end of class

 



  


   import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import javax.swing.filechooser.*;
import java.io.*;






public class ChooseFile extends JPanel implements ActionListener {
   JButton go;
   static String sourceFolder="z";
   String theFile="";
   JFileChooser chooser;
   String choosertitle;
   
  public ChooseFile() {
    go = new JButton("Choose Folder");
    go.addActionListener(this);
    add(go);
   }

  public void actionPerformed(ActionEvent e) {
 
     System.out.println("Button Clicked");
    chooser = new JFileChooser(); 
    chooser.setCurrentDirectory(new java.io.File("."));
    chooser.setDialogTitle(choosertitle);
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
        "JPG, PNG & GIF Images", "jpg", "gif", "png");
    chooser.setFileFilter(filter);
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    // FILES_AND_DIRECTORIES. The default is FILES_ONLY or DIRECTORIES_ONLY
    // disable the "All files" option.
    //
    //chooser.setAcceptAllFileFilterUsed(false);
    //    
    if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 
      
      String dirr = "" + chooser.getCurrentDirectory();
       File file = chooser.getSelectedFile();
       
      if(dirr.substring(dirr.length()-1,dirr.length()).equals(".")){
           dirr = dirr.substring(0,dirr.length()-1);
           sourceFolder=""+dirr + "" + file.getName();
        }else{
            
            sourceFolder=""+dirr + "/" + file.getName();
        }
      
        
       
          System.out.println("Folder path: " + dirr + " | File Name: " + file.getName());
          System.out.println(sourceFolder);
   
      
          // ExamineImage ei = new ExamineImage();
         //  ei.lum(sourceFolder);
      }
    else {
      System.out.println("No Selection ");
      }
     }
   
  public Dimension getPreferredSize(){
    return new Dimension(200, 200);
    }
    
  //public static void main(String s[]) {
      public static void starter() {
            JFrame frame = new JFrame("");
            ChooseFile panel = new ChooseFile();
            frame.addWindowListener(
              new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                  System.exit(0);
                  }
                }
              );
            frame.getContentPane().add(panel,"Center");
            frame.setSize(panel.getPreferredSize());
            frame.setVisible(true);
    }
}
  


 import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
 import javax.swing.ImageIcon;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class ExamineImage{
    
    
  double[][] lumm;
     int[][] aa ;
                       int[][] rr;
                        int[][] gg ;
                         int[][] bb ;
         //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
             public static String iImgName = "z";
          //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

public void rgb(String ffile) {
    BufferedImage img=null;
   
    File myFile = new File(ffile);
      //  File[] listOfFiles = myFolder.listFiles();
   
try{

              if (myFile.isFile()) {
              
            
               
                img = ImageIO.read(myFile);
                int width=0;
                int height=0;

                int count=0;
                double avg=0;
                            if(img!=null){
                               width  = img.getWidth();
                               height  = img.getHeight();
                            }
                             System.out.println("Width " + width + " " +  height);
                             /* */
                       aa = new int[width][height];
                        rr = new int[width][height];
                         gg = new int[width][height];
                         bb = new int[width][height];
                         lumm = new double[width][height];
                
                for(int x=0; x< width; x++){
                    for (int y=0;y< height;y++){
                                   
                                 
                                     int pixelCol = img.getRGB(x, y);
                                        int a = (pixelCol >>> 24) & 0xff;
                                        int r = (pixelCol >>> 16) & 0xff;
                                        int g = (pixelCol >>> 8) & 0xff;
                                        int b = pixelCol & 0xff;
                                     /* */
                                        aa[x][y] = a;
                                        rr[x][y] = r;
                                        gg[x][y] = g;
                                        bb[x][y] = b;
                                       
                                       
                                    lumm[x][y]=   (0.2126*r) + (0.7152*g) + (0.0722*b);
                                 
                                     
                           }}  
                           
                        
                 
                // image(rr,gg,bb,lumm);
            }

}catch(Exception e){

System.out.println("THERE WAS A PROBLEM " + e);
}

}//end of lumanance method






//this creates a new image

 public void image(int[][] red, int[][] green, int[][] blue, double[][] lum)
    {
       //make some changes to the pixels

    /*
        
        for(int x=0; x< red.length-1; x++){
                    for (int y=0;y < red[0].length-1;y++){
                       //CODING GOES HERE
                        
                    }}
                    */
       //make changes above
       
       
       
       //create new image using new values


      BufferedImage img = new BufferedImage(red.length, red[0].length, BufferedImage.TYPE_INT_RGB);
      
  
      for (int x = 0; x< red.length; x++){
          for (int y = 0; y< red[0].length; y++){
              
                    int r = red[x][y];
                    int g = green[x][y];
                    int b = blue[x][y];
                    int col = (r << 16) | (g << 8) | b;
                    img.setRGB(x, y, col);
          
            
        }}
         
        File f = new File("inverted.jpg");
            
            
            try{
                ImageIO.write(img, "JPEG", f);
                  //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
                    iImgName = "inverted.jpg";
                  //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
            } catch(Exception e){
                
            }
            
           
    }






}//end of class