本节书摘来自华章计算机《Java数字图像处理:编程技巧与应用实践》一书中的第3章,第3.5节,作者 贾志刚,更多章节内容可以访问云栖社区“华章计算机”公众号查看。
3.5 一个完整的Swing UI Demo
本节将根据前面前四节所讲的Swing UI组件应用知识,实现一个真正的Swing UI演示,以更加贴近实际编程的方式来说明Swing中组件的应用知识。首先来介绍一下要实现的功能:
- 通过文件对话框选择图像文件,刷新JFrame中的内容面板实现图像显示。
- 通过单击[处理]按钮实现对图像的必要处理,然后刷新显示图像。
大致的UI组件布局如图3-1所示。
其中支持BufferedImage对象显示的自定义JPanel类的实现如下:
package com.book.chapter.three;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
public class ImagePanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private BufferedImage sourceImage;
private BufferedImage destImage;
public ImagePanel() {
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.clearRect(0, 0,
this.getWidth(),
this.getHeight());
if (sourceImage != null) {
g2d.drawImage(sourceImage, 0, 0,
sourceImage.getWidth(),
sourceImage.getHeight(), null);
if (destImage != null) {
g2d.drawImage(destImage,
sourceImage.getWidth() + 10, 0,
destImage.getWidth(),
destImage.getHeight(), null);
}
}
}
public void process()
{
// do nothing
}
public BufferedImage getSourceImage() {
return sourceImage;
}
public void setSourceImage(BufferedImage sourceImage) {
this.sourceImage = sourceImage;
}
public BufferedImage getDestImage() {
return destImage;
}
public void setDestImage(BufferedImage destImage) {
this.destImage = destImage;
}
}
SwingUI界面实现与JButton按钮监听处理的类的代码如下:
package com.book.chapter.three;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;
public class MainUI extends JFrame
implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
public static final String IMAGE_CMD = "选择图像...";
public static final String PROCESS_CMD = "处理";
private JButton imgBtn;
private JButton processBtn;
private ImagePanel imagePanel;
// image
private BufferedImage srcImage;
public MainUI()
{
setTitle("JFrame UI - 演示");
imgBtn = new JButton(IMAGE_CMD);
processBtn = new JButton(PROCESS_CMD);
// buttons
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new
FlowLayout(FlowLayout.RIGHT));
btnPanel.add(imgBtn);
btnPanel.add(processBtn);
// filters
imagePanel = new ImagePanel();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(imagePanel,
BorderLayout.CENTER);
getContentPane().add(btnPanel, BorderLayout.SOUTH);
// setup listener
setupActionListener();
}
private void setupActionListener() {
imgBtn.addActionListener(this);
processBtn.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(SwingUtilities.isEventDispatchThread())
{
System.out.println("Event Dispath Thread!!");
}
if(srcImage == null)
{
JOptionPane.showMessageDialog(this,
"请先选择图像源文件");
try {
JFileChooser chooser =
new JFileChooser();
setFileTypeFilter(chooser);
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
if(f != null)
{
srcImage = ImageIO.read(f);
imagePanel.setSourceImage(
srcImage);
imagePanel.repaint();
}
} catch (IOException e1) {
e1.printStackTrace();
}
return;
}
if(IMAGE_CMD.equals(e.getActionCommand()))
{
try {
JFileChooser chooser =
new JFileChooser();
setFileTypeFilter(chooser);
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
if(f != null)
{
srcImage = ImageIO.read(f);
imagePanel.setSourceImage(
srcImage);
imagePanel.repaint();
}
} catch (IOException e1) {
e1.printStackTrace();
}
imagePanel.repaint();
}
else if(PROCESS_CMD.equals(e.getActionCommand()))
{
imagePanel.process();
imagePanel.repaint();
}
}
public void setFileTypeFilter(JFileChooser chooser)
{
FileNameExtensionFilter filter =
new FileNameExtensionFilter(
"JPG & PNG Images", "jpg", "png");
chooser.setFileFilter(filter);
}
public void openView()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(800, 600));
pack();
setVisible(true);
}
public static void main(String[] args) {
MainUI ui = new MainUI();
ui.openView();
}
}