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

  • SEARCH
  • Home
  • 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 6681865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:38:19+00:00 2026-05-26T04:38:19+00:00

I have here this code. I want it to first display an image file

  • 0

I have here this code. I want it to first display an image file as input by the user when calling the main method, then apply changes to it. My first function is grayscale(). I have provided a grayscale button in a JMenuBar such that when clicked, it will create a grayscale version of the current image. It works, however I can’t get the new image to display in the JFrame after the method is applied. I tried calling show(); within the method, but that just opens the original image again. I know the grayscale function is doing its job, it just doesn’t display the resulting Image after. How can I get it to show image2 after it is created? Thanks for the help.

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 ImageEdit{
    Container content;
    static BufferedImage image;
    BufferedImage  image2;

    public ImageEdit(String filename) {
        File f = new File(filename);
        //assume file is the image file
        try {
            image = ImageIO.read(f);
        } 
        catch (IOException e) {
            System.out.println("Invalid image file: " + filename);
            System.exit(0);
        }
    }

    public void show() {
        final int width = image.getWidth();
        final int height = image.getHeight();

        JFrame frame = new JFrame("Edit Picture");

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

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

        //add a menubar on the frame with a single option: saving the image
        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);
        JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);
        JMenuItem saveAction = new JMenuItem("Save");
        fileMenu.add(saveAction);
        JMenuItem grayScale = new JMenuItem("Grayscale");
        fileMenu.add(grayScale);
        grayScale.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    grayscale(width, height);
                }
            });

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

    public void grayscale(int width, int height) {
        // create a grayscale image the same size
        image2 = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

        // convert the original colored image to grayscale
        ColorConvertOp grayScale = new ColorConvertOp(
            image.getColorModel().getColorSpace(),
        image2.getColorModel().getColorSpace(),null);
        grayScale.filter(image,image2);
        show();
    }

    public static void main(String[] args) {
        ImageEdit p = new ImageEdit(args[0]);
        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-26T04:38:20+00:00Added an answer on May 26, 2026 at 4:38 am
    import java.awt.*;
    import java.awt.image.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ImageEdit{
        Container content;
        BufferedImage image;
        BufferedImage  image2;
        JLabel imageLabel;
    
        public ImageEdit(BufferedImage image) {
            this.image = image;
        }
    
        public void show() {
            final int width = image.getWidth();
            final int height = image.getHeight();
    
            JFrame frame = new JFrame("Edit Picture");
    
            //set frame title, set it visible, etc
            content = frame.getContentPane();
            //frame.setResizable(false);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //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 fileMenu = new JMenu("File");
            menuBar.add(fileMenu);
            JMenuItem saveAction = new JMenuItem("Save");
            fileMenu.add(saveAction);
            JMenuItem grayScale = new JMenuItem("Grayscale");
            fileMenu.add(grayScale);
            grayScale.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        grayscale(width, height);
                    }
                });
    
            //paint the frame
            frame.pack();
            frame.setVisible(true);
        }
    
        public void grayscale(int width, int height) {
            // create a grayscale image the same size
            image2 = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    
            // convert the original colored image to grayscale
            ColorConvertOp grayScale = new ColorConvertOp(
                image.getColorModel().getColorSpace(),
            image2.getColorModel().getColorSpace(),null);
            grayScale.filter(image,image2);
            imageLabel.setIcon(new ImageIcon(image2));
            //show();
        }
    
        public static void main(String[] args) {
            int size = 120;
            int pad = 10;
            BufferedImage bi = new BufferedImage(
                size,
                size,
                BufferedImage.TYPE_INT_RGB);
            Graphics g = bi.createGraphics();
            g.setColor(Color.WHITE);
            g.fillRect(0,0,size,size);
            g.setColor(Color.YELLOW);
            g.fillOval(pad,pad,size-(2*pad),size-(2*pad));
            g.dispose();
    
            ImageEdit p = new ImageEdit(bi);
            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 html code that i want to edit with jQuery. Here is
I have this code here: var infiltrationResult; while(thisOption) { var trNode = document.createElement('tr'); var
I have this code here, which is intended to allow any type of arguments:
I have this code here, {foreach from=$cart.cartItems item=item name=cart} <div id=cart2Produkt> <p>{if $item.Product.ID} <a
What is the issue with this code? Here we have two files: classA.h and
I have this code try { //AN EXCEPTION IS GENERATED HERE!!! } catch {
Hey I have this code right here: http://pastie.org/534470 And on line 109 I get
Quick question, What have I done wrong here. The purpose of this code is
Here's my problem - I have some code like this: <mx:Canvas width=300 height=300> <mx:Button
I have this class called SiteAsyncDownload.cs Here's the code: public class SiteAsyncDownloader { WebClient

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.