Coders Packet

Simple Photo Editor using Swing

By Srinjoy Ghosh

A simple photo editor desktop app that is able to open an image file, apply filters on it, and save a copy of the filtered image. Only two filters are available, i.e., grayscale and sepia

This Java project is a simple photo editor that can take an image, and make a copy of it with either of the two filters "Sepia" and "Grayscale".

The "ui" is the main class and contains the main() method. It is responsible for creating the user interface of the application. The user interface is simple and contains a JMenuBar to select options and a JLabel to display the image. The JLabel has the color blue applied to it. Instances of the classes for the "File" and "Filters" menus on the menu bar are created by passing this ui instance as a paramter. The class for the "File" menu is named "imageManager" and the class for the filter menu is named FilterMenuClass.

                                                                     

   

public class ui implements ActionListener {

    //initializing components for the entire window
    JFrame window;      //the main window
    JLabel label;       //the label to display the image to work on
    JMenuBar menuBar;   //menu bar to select options

    //menus on the menu bar
    JMenu fileMenu, filterMenu;

    //menus on the file menu
    JMenuItem openItem, saveItem, clearItem, resetItem;

    //menus on the filter menu
    JMenuItem sepiaItem, grayItem;

    //creating objects of necessary classes
    imageManager imgM=new imageManager(this);
    FilterMenuClass fm=new FilterMenuClass(this);

 

                                                                 

The File menu contains 4 options, starting with "Open". This menu item has an action listener added to it, and on click calls the openImage() method of the imageManager class. This method launches a JFileChooser, to select an image file. Upon selecting the file, a copy of the image is made named "Name-of-the-original-file(Out).jpg". It is on this image file, the filters are added. The image is set as an icon on the JLabel instance of the UI and the the JFrame, which serves as the main window, adjusts it's size to fit with the size of the image.

         

         

 

//select image function
    public void openImage() {
        JFileChooser fileChooser=new JFileChooser();
        fileChooser.setAcceptAllFileFilterUsed(false);
        fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Image files", ImageIO.getReaderFileSuffixes()));

        fileChooser.showOpenDialog(null);
        
        if (fileChooser.getSelectedFile()!=null) {
            input=fileChooser.getSelectedFile();
            pathname=input.getAbsolutePath();

            try {
                image=ImageIO.read(input);
                output=new File(pathname+"(Out).jpg");
                ImageIO.write(image, "jpg", output);
                image=ImageIO.read(output);
                inviteImage();
    
            } catch (Exception e) {
                //TODO: handle exception
                System.out.println("File Error");
            }
        }

            
        else {
            System.out.println("No File Selected");
        }

    }

    //function to bring the image to the window
    public void inviteImage() {
        ui.window.setSize(image.getWidth(),image.getHeight());
        ui.label.setIcon(new ImageIcon(image));
    }

 

 

The "Save" option calls the saveImage() function of the imageManager class which writes the image obtained from the JLabel's icon to the new image file created by the openImage() function.

//function to save the changes made to image
    public void saveImage() {
        try {
            image=(BufferedImage) ((ImageIcon) ui.label.getIcon()).getImage();
            ImageIO.write(image, "jpg", output);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("No file");;
        }
    }

 

 

The "Clear" function clears the JLabel of the ui and removes the file path from the file name and address variables of the imageManager object.

//function to clear the label
    public void clearWindow() {
        ui.label.setIcon(null);
        input=null;
        output=null;
        pathname="";
    }

 

 

 

The "Reset" function brings the original file on the JLabel's icon, releasing it from any of the filters added.

//function to reset all filters
    public void reset() {
        try {
            ui.label.setIcon(new ImageIcon(ImageIO.read(input)));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

 

 

The "Filters" menu has two options, one for sepia and one for grayscale. The JMenuItem marked "Sepia" calls the sepiafilter() method of the FilterMenuCLass object. This method takes the image on the JLabel icon and passes it as parameter to the makesepia() method of a sepiamaker object. "sepiamaker" is a class built specifically to make the addtion of a sepia filter possible.

//sepia filter method of the FilterMenuClass object
    public void sepiafilter() {
        image=(BufferedImage) ((ImageIcon) ui.label.getIcon()).getImage();
        s=new sepiamaker(image);
        image=s.makesepia();
        ui.label.setIcon(new ImageIcon(image));
    }


//makeSepia() method of the sepiamaker class
    public BufferedImage makesepia() {
        for ( int i = 0; i < image.getWidth(); i++) {
            for ( int j = 0; j < image.getHeight(); j++) {

                Color c=new Color(image.getRGB(i, j));
                int red=c.getRed();
                int green=c.getGreen();
                int blue=c.getBlue();
                red=green=blue=(red+green+blue)/3;

                red+=depth*2;
                green+=depth*0.62;
                blue-=intensity;

                red=Math.min(red, 255);
                green=Math.min(green, 255);
                blue=Math.max(blue, 0);

                image.setRGB(i, j, new Color(red,green,blue).getRGB());
            }
        }
        return image;
    }

 

 

 

The "Grayscale" menu object calls the grayscalefilter method of the FilterMenuClass which in turn passes the image on the ui's JLabel icon to the makegrayscale() method of a grayscaler object. Similar to sepiamaker, grayscaler is also a class built to add the grayscale filter to images.

   

//grayscale filter method of the FilterMenuCLass class
    public void grayscalefilter() {
        image=(BufferedImage) ((ImageIcon) ui.label.getIcon()).getImage();
        g=new grayscaler(image);
        image=g.makegrayscale();
        ui.label.setIcon(new ImageIcon(image));
    }



//makegrayscale method of the grayscaler class
    public BufferedImage makegrayscale() {
        for ( int i = 0; i < image.getWidth(); i++) {
            for ( int j = 0; j < image.getHeight(); j++) {
                Color c=new Color(image.getRGB(i, j));
                int red=(int) (c.getRed()*0.2126);
                int green=(int) (c.getGreen()*0.7152);
                int blue=(int) (c.getBlue()*0.0722);

                //creating a new color with the new red, green and blue values
                Color newColor=new Color(red+green+blue,red+green+blue,red+green+blue);

                //coloring the image pixel with the new color
                image.setRGB(i, j, newColor.getRGB());
            }
        }
        return image;
    }

 

Download project

Reviews Report

Submitted by Srinjoy Ghosh (srinjoyghosh731)

Download packets of source code on Coders Packet