Photo Editor
Photo Editor
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* @author 1BestCsharp
*/
public class PhotoEditorApp extends JFrame {
public PhotoEditorApp(){
setLocationRelativeTo(null);
createMenuBar();
add(buttonPanel, BorderLayout.SOUTH);
setVisible(true);
}
// Create "Open" menu item and set an action listener to handle its
click event
JMenuItem openItem = new JMenuItem("open");
openItem.addActionListener(e -> openImage());
// Create "Save" menu item and set an action listener to handle its
click event
JMenuItem saveItem = new JMenuItem("save");
saveItem.addActionListener(e -> saveImage());
// Create a Method to reset the edited image back to the original image
private void resetImage(){
// Display the file chooser dialog and wait for the user to select a
file
int result = fileChooser.showOpenDialog(this);
if(result == JFileChooser.APPROVE_OPTION)
{
// Get the selected file
File selectedFile = fileChooser.getSelectedFile();
try
{
// Check if the selected file is a valid image file
BufferedImage testImage = ImageIO.read(selectedFile);
if(testImage == null)
{
// Show an error message if the selected file is not a
valid image
JOptionPane.showMessageDialog(this, "Invalid Image File
Selected", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
// Show the save dialog to allow the user to choose a location to save
the image
int result = fileChooser.showSaveDialog(this);
// If the user selects a location and clicks the "Save" button
if(result == JFileChooser.APPROVE_OPTION)
{
// Get the selected file and attempt to save the edited image to it
File selectedFile = fileChooser.getSelectedFile();
try
{
// create a method that applies a grayscale filter to the edited image using the
luminosity method
private void applyGrayScaleFilter()
{
// Check if the original image exists
if(originalImage != null)
{
// Loop through all the pixels in the edited image
for(int x = 0; x < editedImage.getWidth(); x++)
{
for(int y = 0; y < editedImage.getHeight(); y++)
{
// Get the RGB value of the corresponding pixel in the original
image
int rgb = originalImage.getRGB(x, y);
// Calculate the grayscale value using the luminosity method
int gray = (int) (0.3 * ((rgb >> 16) & 0xFF) + 0.59 * ((rgb >> 8)
& 0xFF) + 0.11 * (rgb & 0xFF) );
// create a that method applies a simple blur filter to the edited image using a
3x3 kernel.
private void applyBlurFilter()
{
// Loop through all the pixels in the edited image (excluding the
borders)
for(int x = 1; x < editedImage.getWidth() - 1; x++)
{
for(int y = 1; y < editedImage.getHeight() - 1; y++)
{
// Initialize RGB values to 0 for each pixel
int r = 0, g = 0, b = 0;
// Compose the new RGB value using the blurred color components
int newRgb = (r << 16) | (g << 8) | b;
// Update the edited image with the new RGB value
editedImage.setRGB(x, y, newRgb);
}
}
// Update the label displaying the edited image to reflect the changes
updateImageLabel();
// create a method that applies the invert colors filter to the editedImage.
private void applyInvertColorsFilter()
{
// Check if the original image exists
if(originalImage != null)
{
// Loop through all the pixels in the edited image
for(int x = 0; x < editedImage.getWidth(); x++)
{
for(int y = 0; y < editedImage.getHeight(); y++)
{
// Get the RGB color value at the current pixel in the
originalImage.
int rgb = originalImage.getRGB(x, y);
// Extract the red component, invert it, and store it in r.
int r = 255 - ((rgb >> 16) & 0xFF);
// Extract the green component, invert it, and store it in g.
int g = 255 - ((rgb >> 8) & 0xFF);
// Extract the blue component, invert it, and store it in b.
int b = 255 - (rgb & 0xFF);
// Combine the inverted RGB components and set the pixel value
in the editedImage.
editedImage.setRGB(x, y, (r << 16) | (g << 8) | b);
}
}
}
}
// Update the label displaying the editedImage
updateImageLabel();
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new PhotoEditorApp();