更改在JFrame中显示的ImageIcon [英] Change ImageIcon being displayed in JFrame

查看:89
本文介绍了更改在JFrame中显示的ImageIcon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有此代码.我希望它在调用main方法时首先显示用户输入的图像文件,然后对其进行更改.我的第一个功能是grayscale().我在JMenuBar中提供了一个灰度按钮,以便单击该按钮时,它将创建当前图像的灰度版本.它可以工作,但是应用该方法后,我无法在JFrame中显示新图像.我试图打电话给show();在方法中,但这只是再次打开原始图像.我知道灰度功能正在发挥作用,只是它之后不显示结果图像.创建后如何显示image2?感谢您的帮助.

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

推荐答案

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

这篇关于更改在JFrame中显示的ImageIcon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆