Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6707949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:42:01+00:00 2026-05-26T07:42:01+00:00

This program should first open an image, and then allow it to be manipulated

  • 0

This program should first open an image, and then allow it to be manipulated via grayscale, scale, and rotate methods (not functional at the moment; disregard it). However, I’m not sure how I can call upon the updated image every time a method is performed. For example, if I grayscale an image, it goes to grayscale. But if I then scale it to a different size, the resulting image is a scaled version of the original image, not a scaled grayscale image. I tried putting in “image2 = image;” to no avail. How can I fix this?
Thanks.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.image.*;
import javax.imageio.ImageIO;

public class Picture{
    JFileChooser fileChooser = new JFileChooser();    
    final JFrame frame = new JFrame("Edit Image");    
    Container content;
    static BufferedImage image;
    BufferedImage image2;
    JLabel imageLabel;

    public Picture() {
        //asks for image file as input
        fileChooser.setDialogTitle("Choose an image file to begin:");
        fileChooser.showOpenDialog(frame);
        File selectedFile = fileChooser.getSelectedFile();
        if (fileChooser.getSelectedFile() != null) {
            try {
                //reads File as image
                image = ImageIO.read(selectedFile);
            } 
            catch (IOException e) {
                System.out.println("Invalid image file: " + selectedFile);
                System.exit(0);
            }
        }
        else {
            System.out.println("No File Selected!");
        }
    }

    public int width() {
        //returns width of present image
        int width = image.getWidth();
        return width;
    }

    public int height() {
        //returns height of present image
        int height = image.getHeight();
        return height;
    }

    public void getImage() {
        this.image2 = image;
    }

    public void saveImage() {
        //saves current image as JPEG
        fileChooser.setDialogTitle("Save this image?");
        fileChooser.showSaveDialog(frame);
        try {
            //writes new file
            ImageIO.write(this.image, "JPG", fileChooser.getSelectedFile());
        }
        catch (IOException f) {
            System.out.println("Saving failed! Could not save image.");
        }
    }

    public void show() {
        //set frame title, set it visible, etc
        content = frame.getContentPane();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        //add the image to the frame
        ImageIcon icon = new ImageIcon(image);
        imageLabel = new JLabel(icon);
        frame.setContentPane(imageLabel);

        //add a menubar on the frame with a single option: saving the image
        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);
        JMenu progName = new JMenu("Edit Image");
        progName.setBackground(Color.RED);
        menuBar.add(progName);
        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);
        JMenu editMenu = new JMenu("Edit");
        menuBar.add(editMenu);

        ImageIcon exitIcon = new ImageIcon("app-exit.png");
        JMenuItem exitAction = new JMenuItem("Exit", exitIcon);
        progName.add(exitAction);
        exitAction.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ImageIcon saveIcon = new ImageIcon("save-icon.png");
                    int askSave = JOptionPane.showConfirmDialog(null,"Save current image?", "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, saveIcon);
                    if (askSave == JOptionPane.YES_OPTION) {
                        //opens save image method, then exits
                        saveImage();
                        System.exit(0);
                    }
                    else {
                        //exits without saving
                        System.exit(0);
                    }
                }
            });

        ImageIcon newIcon = new ImageIcon("new-image.png");
        JMenuItem newAction = new JMenuItem("Open Image", newIcon);
        fileMenu.add(newAction);
        newAction.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ImageIcon saveIcon = new ImageIcon("save-icon.png");
                    int askSave = JOptionPane.showConfirmDialog(null,"Save current image?", "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, saveIcon);
                    if (askSave == JOptionPane.YES_OPTION) {
                        //opens save image method, then asks asks for new image file
                        saveImage();
                        Picture p = new Picture();
                        imageLabel.setIcon(new ImageIcon(image));
                        //resizes canvas to fit new image
                        frame.setSize(width(), height());
                    }
                    else {
                        //asks for new image file since user did not want to save original
                        Picture p = new Picture();
                        imageLabel.setIcon(new ImageIcon(image));
                        //resizes canvas to fit new image
                        frame.setSize(width(), height());
                    }
                }
            });

        ImageIcon saveIcon = new ImageIcon("save-image.png");
        JMenuItem saveAction = new JMenuItem("Save Image As...", saveIcon);
        fileMenu.add(saveAction);
        saveAction.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //opens save image method
                    saveImage();
                }
            });
        ImageIcon gsIcon = new ImageIcon("grayscale-image.png");
        JMenuItem grayScale = new JMenuItem("Grayscale", gsIcon);
        editMenu.add(grayScale);
        grayScale.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //grabs height and width of image, then grayscales it
                    grayscale(width(), height());
                }
            });

        ImageIcon scaleIcon = new ImageIcon("scale-image.png");
        JMenuItem scaleImg = new JMenuItem("Scale Image", scaleIcon);
        editMenu.add(scaleImg);
        scaleImg.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //asks for height and width to create new image
                    ImageIcon widthIcon = new ImageIcon("LR-arrows.png");
                    String scaleWidth = (String)JOptionPane.showInputDialog(null,"What should the new width be?", "", JOptionPane.QUESTION_MESSAGE, widthIcon, null, null);
                    ImageIcon heightIcon = new ImageIcon("UD-arrows.png");
                    String scaleHeight = (String)JOptionPane.showInputDialog(null,"What should the new height be?", "", JOptionPane.QUESTION_MESSAGE, widthIcon, null, null);
                    //turns user input strings into doubles
                    double x = Double.parseDouble(scaleWidth);
                    double y = Double.parseDouble(scaleHeight);
                    //casts doubles as ints
                    int newWidth = (int)x;
                    int newHeight = (int)y;
                    //resizes frame to fit new image dimensions
                    frame.setSize(newWidth, newHeight);
                    //calls scale method to resize image using given dimensions
                    scale(newWidth, newHeight);
                }
            });
        ImageIcon rotateIcon = new ImageIcon("rotate-image.png");
        JMenuItem rotateImg = new JMenuItem("Rotate Image", rotateIcon);
        editMenu.add(rotateImg);
        rotateImg.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                }
            });

        //paint the frame
        frame.pack();
        frame.repaint();
        frame.setVisible(true);
    }

    // convert to grayscale
    public void grayscale(int width, int height) {
        // create a grayscale image with original dimensions
        image2 = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

        // convert colored image to grayscale
        ColorConvertOp grayScale = new ColorConvertOp(image.getColorModel().getColorSpace(),image2.getColorModel().getColorSpace(),null);
        grayScale.filter(image,image2);
        imageLabel.setIcon(new ImageIcon(image2));
        getImage();
    }

    //scales image by a given factor
    public void scale(int width, int height){
        //uses user-input dimensions to create new image
        image2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image2.createGraphics();
        //gets new dimensions and resizes image
        g.drawImage(image, 0, 0, image2.getWidth(), image2.getHeight(), 0, 0, width(), height(), null);  
        imageLabel.setIcon(new ImageIcon(image2));
        getImage();
    }

    //rotates the image
    public void rotate(int width, int height, int theta) {

    }

    public static void main(String[] args) {
        Picture p = new Picture();
        p.show();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:42:02+00:00Added an answer on May 26, 2026 at 7:42 am

    Picture

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.image.*;
    import javax.imageio.ImageIO;
    
    public class Picture{
        JFileChooser fileChooser = new JFileChooser();
        final JFrame frame = new JFrame("Edit Image");
        Container content;
        static BufferedImage image;
        BufferedImage image2;
        JLabel imageLabel;
    
        public Picture() {
            //asks for image file as input
            fileChooser.setDialogTitle("Choose an image file to begin:");
            fileChooser.showOpenDialog(frame);
            File selectedFile = fileChooser.getSelectedFile();
            if (fileChooser.getSelectedFile() != null) {
                try {
                    //reads File as image
                    image = ImageIO.read(selectedFile);
                }
                catch (IOException e) {
                    System.out.println("Invalid image file: " + selectedFile);
                    System.exit(0);
                }
            }
            else {
                System.out.println("No File Selected!");
            }
        }
    
        public int width() {
            //returns width of present image
            int width = image.getWidth();
            return width;
        }
    
        public int height() {
            //returns height of present image
            int height = image.getHeight();
            return height;
        }
    /*
        public void getImage() {
            this.image2 = image;
        }
    */
        public void saveImage() {
            //saves current image as JPEG
            fileChooser.setDialogTitle("Save this image?");
            fileChooser.showSaveDialog(frame);
            try {
                //writes new file
                ImageIO.write(this.image, "JPG", fileChooser.getSelectedFile());
            }
            catch (IOException f) {
                System.out.println("Saving failed! Could not save image.");
            }
        }
    
        public void show() {
            //set frame title, set it visible, etc
            content = frame.getContentPane();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(false);
    
            //add the image to the frame
            ImageIcon icon = new ImageIcon(image);
            imageLabel = new JLabel(icon);
            frame.setContentPane(imageLabel);
    
            //add a menubar on the frame with a single option: saving the image
            JMenuBar menuBar = new JMenuBar();
            frame.setJMenuBar(menuBar);
            JMenu progName = new JMenu("Edit Image");
            progName.setBackground(Color.RED);
            menuBar.add(progName);
            JMenu fileMenu = new JMenu("File");
            menuBar.add(fileMenu);
            JMenu editMenu = new JMenu("Edit");
            menuBar.add(editMenu);
    
            ImageIcon exitIcon = new ImageIcon("app-exit.png");
            JMenuItem exitAction = new JMenuItem("Exit", exitIcon);
            progName.add(exitAction);
            exitAction.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        ImageIcon saveIcon = new ImageIcon("save-icon.png");
                        int askSave = JOptionPane.showConfirmDialog(null,"Save current image?", "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, saveIcon);
                        if (askSave == JOptionPane.YES_OPTION) {
                            //opens save image method, then exits
                            saveImage();
                            System.exit(0);
                        }
                        else {
                            //exits without saving
                            System.exit(0);
                        }
                    }
                });
    
            ImageIcon newIcon = new ImageIcon("new-image.png");
            JMenuItem newAction = new JMenuItem("Open Image", newIcon);
            fileMenu.add(newAction);
            newAction.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        ImageIcon saveIcon = new ImageIcon("save-icon.png");
                        int askSave = JOptionPane.showConfirmDialog(null,"Save current image?", "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, saveIcon);
                        if (askSave == JOptionPane.YES_OPTION) {
                            //opens save image method, then asks asks for new image file
                            saveImage();
                            Picture p = new Picture();
                            imageLabel.setIcon(new ImageIcon(image));
                            //resizes canvas to fit new image
                            frame.setSize(width(), height());
                        }
                        else {
                            //asks for new image file since user did not want to save original
                            Picture p = new Picture();
                            imageLabel.setIcon(new ImageIcon(image));
                            //resizes canvas to fit new image
                            frame.setSize(width(), height());
                        }
                    }
                });
    
            ImageIcon saveIcon = new ImageIcon("save-image.png");
            JMenuItem saveAction = new JMenuItem("Save Image As...", saveIcon);
            fileMenu.add(saveAction);
            saveAction.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        //opens save image method
                        saveImage();
                    }
                });
            ImageIcon gsIcon = new ImageIcon("grayscale-image.png");
            JMenuItem grayScale = new JMenuItem("Grayscale", gsIcon);
            editMenu.add(grayScale);
            grayScale.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        //grabs height and width of image, then grayscales it
                        grayscale(width(), height());
                    }
                });
    
            ImageIcon scaleIcon = new ImageIcon("scale-image.png");
            JMenuItem scaleImg = new JMenuItem("Scale Image", scaleIcon);
            editMenu.add(scaleImg);
            scaleImg.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        //asks for height and width to create new image
                        ImageIcon widthIcon = new ImageIcon("LR-arrows.png");
                        String scaleWidth = (String)JOptionPane.showInputDialog(null,"What should the new width be?", "", JOptionPane.QUESTION_MESSAGE, widthIcon, null, null);
                        ImageIcon heightIcon = new ImageIcon("UD-arrows.png");
                        String scaleHeight = (String)JOptionPane.showInputDialog(null,"What should the new height be?", "", JOptionPane.QUESTION_MESSAGE, widthIcon, null, null);
                        //turns user input strings into doubles
                        double x = Double.parseDouble(scaleWidth);
                        double y = Double.parseDouble(scaleHeight);
                        //casts doubles as ints
                        int newWidth = (int)x;
                        int newHeight = (int)y;
                        //resizes frame to fit new image dimensions
                        frame.setSize(newWidth, newHeight);
                        //calls scale method to resize image using given dimensions
                        scale(newWidth, newHeight);
                    }
                });
            ImageIcon rotateIcon = new ImageIcon("rotate-image.png");
            JMenuItem rotateImg = new JMenuItem("Rotate Image", rotateIcon);
            editMenu.add(rotateImg);
            rotateImg.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
    
                    }
                });
    
            //paint the frame
            frame.pack();
            frame.repaint();
            frame.setVisible(true);
        }
    
        // convert to grayscale
        public void grayscale(int width, int height) {
            // create a grayscale image with original dimensions
            image2 = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    
            // convert colored image to grayscale
            ColorConvertOp grayScale = new ColorConvertOp(image.getColorModel().getColorSpace(),image2.getColorModel().getColorSpace(),null);
            grayScale.filter(image,image2);
            imageLabel.setIcon(new ImageIcon(image2));
            //getImage();
            image = image2;
        }
    
        //scales image by a given factor
        public void scale(int width, int height){
            //uses user-input dimensions to create new image
            image2 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics g = image2.createGraphics();
            //gets new dimensions and resizes image
            g.drawImage(image, 0, 0, image2.getWidth(), image2.getHeight(), 0, 0, width(), height(), null);
            imageLabel.setIcon(new ImageIcon(image2));
            //getImage();
            image = image2;
        }
    
        //rotates the image
        public void rotate(int width, int height, int theta) {
    
        }
    
        public static void main(String[] args) {
            Picture p = new Picture();
            p.show();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this program that should execute a piece of code base on the
I am working on a bug in this program where it should be able
Consider this problem: I have a program which should fetch (let's say) 100 records
Edited Question: This should be clear. using System; namespace UpdateDateTimeFields { class Program {
How can I do this in a C# program? I'm pretty sure it should
Dear all,Now i have this question in my java program,I think it should be
This program I use has it's own variables to set when you run it,
This program stores pairs in a map, counting the number of times a word
This program reads emails (really just a .txt file structured like an email) and
This program I'm doing is about a social network, which means there are users

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.