📄 picturedrawer.java
字号:
jrbtnInvert.addActionListener(this); //其他反色按钮 jrbtnOtherInvert = new JRadioButton( new ImageIcon("PictureDrawer/res/other_invert.png")); jrbtnOtherInvert.setSelectedIcon( new ImageIcon("PictureDrawer/res/other_invert_select.png")); jrbtnOtherInvert.addActionListener(this); //黑色限制值按钮 jrbtnBlackThreshold = new JRadioButton( new ImageIcon("PictureDrawer/res/black_threshold.png")); jrbtnBlackThreshold.setSelectedIcon( new ImageIcon("PictureDrawer/res/black_threshold_selected.png")); jrbtnBlackThreshold.addActionListener(this); //白色限制值按钮 jrbtnWhiteThreshold = new JRadioButton( new ImageIcon("PictureDrawer/res/white_threshold.png")); jrbtnWhiteThreshold.setSelectedIcon( new ImageIcon("PictureDrawer/res/white_threshold_selected.png")); jrbtnWhiteThreshold.addActionListener(this); //亮度调节按钮 jrbtnRescale = new JRadioButton( new ImageIcon("PictureDrawer/res/rescale.png")); jrbtnRescale.setSelectedIcon( new ImageIcon("PictureDrawer/res/rescale_selected.png")); jrbtnRescale.addActionListener(this); //放大缩小按钮 jrbtnZoom = new JRadioButton( new ImageIcon("PictureDrawer/res/zoom.png")); jrbtnZoom.setSelectedIcon( new ImageIcon("PictureDrawer/res/zoom_selected.png")); jrbtnZoom.addActionListener(this); //3x3边缘化按钮 jrbtnEdge33 = new JRadioButton( new ImageIcon("PictureDrawer/res/edge33.png")); jrbtnEdge33.setSelectedIcon( new ImageIcon("PictureDrawer/res/edge33_selected.png")); jrbtnEdge33.addActionListener(this); //5x5边缘化按钮 jrbtnEdge55 = new JRadioButton( new ImageIcon("PictureDrawer/res/edge55.png")); jrbtnEdge55.setSelectedIcon( new ImageIcon("PictureDrawer/res/edge55_selected.png")); jrbtnEdge55.addActionListener(this); //锐化按钮 jrbtnSharpen = new JRadioButton( new ImageIcon("PictureDrawer/res/sharpen.png")); jrbtnSharpen.setSelectedIcon( new ImageIcon("PictureDrawer/res/sharpen_selected.png")); jrbtnSharpen.addActionListener(this); //钝化按钮 jrbtnBlur = new JRadioButton( new ImageIcon("PictureDrawer/res/blur.png")); jrbtnBlur.setSelectedIcon( new ImageIcon("PictureDrawer/res/blur_selected.png")); jrbtnBlur.addActionListener(this); //垂直镜像按钮 jrbtnVerticalMirror = new JRadioButton( new ImageIcon("PictureDrawer/res/vertical_mirror.png")); jrbtnVerticalMirror.setSelectedIcon( new ImageIcon("PictureDrawer/res/vertical_mirror_selected.png")); jrbtnVerticalMirror.addActionListener(this); //水平镜像按钮 jrbtnHorizontalMirror = new JRadioButton( new ImageIcon("PictureDrawer/res/horizontal_mirror.png")); jrbtnHorizontalMirror.setSelectedIcon( new ImageIcon("PictureDrawer/res/horizontal_mirror_selected.png")); jrbtnHorizontalMirror.addActionListener(this); //恢复按钮 jrbtnRetrieve = new JRadioButton( new ImageIcon("PictureDrawer/res/retrieve.png")); jrbtnRetrieve.setSelectedIcon( new ImageIcon("PictureDrawer/res/retrieve_selected.png")); jrbtnRetrieve.addActionListener(this); //添加到ButtonGroup中 ButtonGroup group = new ButtonGroup(); group.add(jrbtnHand); group.add(jrbtnMagnifier); group.add(jrbtnRotate); group.add(jrbtnInvert); group.add(jrbtnOtherInvert); group.add(jrbtnBlackThreshold); group.add(jrbtnWhiteThreshold); group.add(jrbtnRescale); group.add(jrbtnZoom); group.add(jrbtnEdge33); group.add(jrbtnEdge55); group.add(jrbtnSharpen); group.add(jrbtnBlur); group.add(jrbtnVerticalMirror); group.add(jrbtnHorizontalMirror); group.add(jrbtnRetrieve); toolbuttons.add(jrbtnHand); toolbuttons.add(jrbtnMagnifier); toolbuttons.add(jrbtnRotate); toolbuttons.add(jrbtnInvert); toolbuttons.add(jrbtnOtherInvert); toolbuttons.add(jrbtnBlackThreshold); toolbuttons.add(jrbtnWhiteThreshold); toolbuttons.add(jrbtnRescale); toolbuttons.add(jrbtnZoom); toolbuttons.add(jrbtnEdge33); toolbuttons.add(jrbtnEdge55); toolbuttons.add(jrbtnSharpen); toolbuttons.add(jrbtnBlur); toolbuttons.add(jrbtnVerticalMirror); toolbuttons.add(jrbtnHorizontalMirror); toolbuttons.add(jrbtnRetrieve); jToolBar1 = new javax.swing.JToolBar(); jToolBar1.add(jrbtnHand); jToolBar1.add(jrbtnMagnifier); jToolBar1.add(jrbtnRotate); jToolBar1.add(jrbtnInvert); jToolBar1.add(jrbtnOtherInvert); jToolBar1.add(jrbtnBlackThreshold); jToolBar1.add(jrbtnWhiteThreshold); jToolBar1.add(jrbtnRescale); jToolBar1.add(jrbtnZoom); jToolBar1.add(jrbtnEdge33); jToolBar1.add(jrbtnEdge55); jToolBar1.add(jrbtnSharpen); jToolBar1.add(jrbtnBlur); jToolBar1.add(jrbtnVerticalMirror); jToolBar1.add(jrbtnHorizontalMirror); jToolBar1.add(jrbtnRetrieve); getContentPane().add(jToolBar1, java.awt.BorderLayout.NORTH); } /** * 当用户单击 File -> Exit 菜单时,该函数被调用, * 用于关闭应用程序 * <p> * @param evt 从actionPerformed事件传递来的ActionEvent对象实例 */ private void exitMenuItemActionPerformed(ActionEvent evt) { System.exit( 0 ); } /** * 本类用于过虑图像文件。 */ private static class ImageFileFilter extends javax.swing.filechooser.FileFilter { public boolean accept(java.io.File file) { if (file == null) return false; return file.isDirectory() || file.getName().toLowerCase().endsWith(".gif") || file.getName().toLowerCase().endsWith(".jpg") || file.getName().toLowerCase().endsWith(".png"); } public String getDescription() { return "Image files (*.gif, *.jpg, *.png)"; } } /** * 处理主窗口中的action事件 */ public void actionPerformed(ActionEvent e) { //在用户选择File -> Open命令时, //显示一个打开文件对话框,选择要显示的图像文件 if (e.getSource()==openMenuItem) { JFileChooser chooser = new JFileChooser(); chooser.addChoosableFileFilter(new ImageFileFilter()); int option = chooser.showOpenDialog(this); if (option == javax.swing.JFileChooser.APPROVE_OPTION) { java.io.File file = chooser.getSelectedFile(); if (file == null) return; ImageFrame ifr = new ImageFrame(this, file.getAbsolutePath()); desktop.add(ifr, javax.swing.JLayeredPane.DEFAULT_LAYER); ifr.setVisible( true ); ifr.setLocation(0, 0); } return; } else if (e.getSource()==jrbtnHand) { current_tool = TOOL_MOUSE; } else { JInternalFrame jif = desktop.getSelectedFrame(); //如果没有活动窗口或者不是ImageFrame窗口,退出操作 if ((jif==null)||(!(jif instanceof ImageFrame))) { debug("actionPerformed", "No JInternalFrame is selected, return"); return; } //获得当前的活动窗口,所有的操作只针对当前的窗口 ImageFrame ifr = (ImageFrame)jif; //保存图像 if (e.getSource()==saveMenuItem) { ifr.save(""); return; } //图像另存 if (e.getSource()==saveAsMenuItem) { //在用户选择File -> Save As命令时, //显示一个保存文件对话框,选择要保存的图像文件名 JFileChooser chooser = new JFileChooser(); chooser.setDialogType(JFileChooser.SAVE_DIALOG); chooser.addChoosableFileFilter(new ImageFileFilter()); int option = chooser.showOpenDialog(this); if (option == javax.swing.JFileChooser.APPROVE_OPTION) { java.io.File file = chooser.getSelectedFile(); if (file != null) { ifr.save(file.getAbsolutePath()); } } return; } if ((e.getSource()==jrbtnMagnifier) || (e.getSource()==magnifierMenuItem)) { current_tool = TOOL_MAGNIFIER; } else if ((e.getSource()==jrbtnRotate) || (e.getSource()==rotateMenuItem)) { current_tool = TOOL_ROTATE; } else if ((e.getSource()==jrbtnInvert) || (e.getSource()==invertMenuItem)) { current_tool = TOOL_INVERT; } else if ((e.getSource()==jrbtnOtherInvert) || (e.getSource()==otherInvertMenuItem)) { current_tool = TOOL_OTHER_INVERT; } else if ((e.getSource()==jrbtnBlackThreshold) || (e.getSource()==blackThresholdMenuItem)) { current_tool = TOOL_BLACK_THRESHOLD; } else if ((e.getSource()==jrbtnWhiteThreshold) || (e.getSource()==whiteThresholdMenuItem)) { current_tool = TOOL_WHITE_THRESHOLD; } else if ((e.getSource()==jrbtnRescale) || (e.getSource()==rescaleMenuItem)) { current_tool = TOOL_RESCALE; } else if ((e.getSource()==jrbtnZoom) || (e.getSource()==zoomMenuItem)) { current_tool = TOOL_ZOOM; } else if ((e.getSource()==jrbtnEdge33) || (e.getSource()==edge33MenuItem)) { current_tool = TOOL_EDGE33; } else if ((e.getSource()==jrbtnEdge55) || (e.getSource()==edge55MenuItem)) { current_tool = TOOL_EDGE55; } else if ((e.getSource()==jrbtnSharpen) || (e.getSource()==sharpenMenuItem)) { current_tool = TOOL_SHARPEN; } else if ((e.getSource()==jrbtnBlur) || (e.getSource()==blurMenuItem)) { current_tool = TOOL_BLUR; } else if ((e.getSource()==jrbtnVerticalMirror) || (e.getSource()==vMirrorMenuItem)) { current_tool = TOOL_VERTICAL_MIRROR; } else if ((e.getSource()==jrbtnHorizontalMirror) || (e.getSource()==hMirrorMenuItem)) { current_tool = TOOL_HORIZONTAL_MIRROR; } else if ((e.getSource()==jrbtnRetrieve) || (e.getSource()==retrieveMenuItem)) { current_tool = TOOL_RETRIEVE; } ifr.operate(current_tool); } debug("actionPerformed", "current tool is - " + current_tool); } /** * 调试函数,在屏幕上输出调试信息 */ void debug(String method_name, String msg) { System.out.println("[" + method_name + "] : " + msg); } /** * 当ImageFrame的状态改变时(活动的ImageFram改变时), * 修改工具栏上的按钮状态 */ void updateToolButtons(int buttonID) { if (buttonID<=toolbuttons.size()) { JRadioButton jrb = (JRadioButton)toolbuttons.elementAt(buttonID); jrb.setSelected(true); current_tool = buttonID; jToolBar1.updateUI(); } } /** * 开始执行应用程序 * @param args 应用程序的参数 */ public static void main(String args[]) { new PictureDrawer().show(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -