更改 JFrame 中显示的 ImageIcon
我这里有这段代码。我希望它在调用 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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)