How can I display a BufferedImage in a JFrame?

拜拜、爱过 提交于 2019-11-30 07:24:48

问题


I want to display variations of the same image in the same JFrame, for example display an image in JFrame, then replace it with gray scale of the same image.


回答1:


You will have to repaint the JFrame whenever you update the image.

Here is what a simple google on the topic brings up: (I use those tutorials for all my Java coding)

Java Tutorial: Drawing an Image




回答2:


To build on camickr's solution (for the lazy like me who want quick code to copy/paste) here's a code illustration:

JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JLabel(new ImageIcon(img)));
frame.getContentPane().add(new JLabel(new ImageIcon(img2)));
frame.getContentPane().add(new JLabel(new ImageIcon(img3)));
frame.pack();
frame.setVisible(true);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // if you want the X button to close the app



回答3:


I'm not really sure what you question is but if you have a BufferedImage then you simply create an ImageIcon using the image, then you add the icon to a JLabel and add the label to the GUI like any other component.

If you question is about how to create a gray scale, the I suggest you search the web using those terms as the search keywords, I'm sure you will find examples out there.



来源:https://stackoverflow.com/questions/1626735/how-can-i-display-a-bufferedimage-in-a-jframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!