Java 更改JFrame中显示的图像图标

Java 更改JFrame中显示的图像图标,java,swing,image-processing,grayscale,imageicon,Java,Swing,Image Processing,Grayscale,Imageicon,我这里有这个密码。我希望它在调用main方法时首先显示一个图像文件作为用户输入,然后对其应用更改。我的第一个函数是grayscale()。我在JMenuBar中提供了一个灰度按钮,当单击时,它将创建当前图像的灰度版本。它可以工作,但是在应用该方法后,我无法在JFrame中显示新图像。我试着打电话给show();在方法中,但这只是再次打开原始图像。我知道灰度函数正在完成它的工作,它只是不显示生成的图像。如何使其在创建后显示image2?谢谢你的帮助 import java.awt.*; impor

我这里有这个密码。我希望它在调用main方法时首先显示一个图像文件作为用户输入,然后对其应用更改。我的第一个函数是grayscale()。我在JMenuBar中提供了一个灰度按钮,当单击时,它将创建当前图像的灰度版本。它可以工作,但是在应用该方法后,我无法在JFrame中显示新图像。我试着打电话给show();在方法中,但这只是再次打开原始图像。我知道灰度函数正在完成它的工作,它只是不显示生成的图像。如何使其在创建后显示image2?谢谢你的帮助

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();
    }
}

作为比较,这里有一个有效的例子。@trashgod我认为该来源看起来很熟悉&甚至在注意到其中没有任何其他问题显示为“已访问”之前检查了用户配置文件。我忘记的是,我正在32位和64位操作系统之间切换,并且正在使用新的浏览器p为了更快地提供帮助,请发布一条消息。另外,请在代码中生成图像,就像我在回答中所做的那样。顺便说一句,我删除了之前的一条评论,当时我认为我已经向你们提到了SSCCE的(伟大的,美妙的)概念。我真傻。
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();
    }
}