Java界面程序實現圖片的放大縮小。這個程序簡單地實現了圖片的打開、保存、放大一倍、縮小一倍和固定縮放尺寸,但是並沒有過多的涵蓋對圖片的細節處理,只是簡單地實現了圖片大小的放縮。
思維導圖如下:
效果圖如下:
代碼如下:

package picture; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import javax.swing.*; import java.io.*; import javax.imageio.ImageIO; public class DialogDemo implements ActionListener{ private JFrame frame; private Panel panel, panelButton, panelText; private JLabel labelHight, labelWidth; //固定縮放寬度和長度 private JTextField textHight; private JTextField textWidth; //操作記錄提示框 private JTextArea textArea; //操作按鈕 private JButton buttonReduce, buttonEnlarge, buttonZoom; //菜單欄:打開圖片、保存圖片、關於、退出 private JMenuItem itemSave, itemOpen,itemAbout, itemExit; //打開圖片窗口,保存圖片窗口 private FileDialog dialogOpen; private FileDialog dialogSave; //BufferedImage用於保存圖片 private BufferedImage bufferedImage; //圖片顯示imageCanvas類(繼承Canvas) private imageCanvas canvas; private Image image; private Graphics graphics; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { DialogDemo window = new DialogDemo(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } //圖片顯示imageCanvas類(繼承Canvas),用於圖片重新繪制 class imageCanvas extends Canvas { //重寫Canvas的paint方法 public void paint(Graphics g) { //將image繪制到該組件上 g.drawImage(bufferedImage, 0, 0, null); //f.setVisible(true); } } //構造函數初始化圖像界面 public DialogDemo() { frame = new JFrame(); frame.setBounds(100, 100, 900, 900); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new BorderLayout(0, 0)); canvas = new imageCanvas(); canvas.setPreferredSize(new Dimension(800, 600)); frame.getContentPane().add(canvas, BorderLayout.CENTER); panel = new Panel(); frame.getContentPane().add(panel, BorderLayout.SOUTH); panel.setLayout(new GridLayout(1, 0, 0, 0)); textArea = new JTextArea(); textArea.setEditable(false); textArea.setText("\u63D0\u793A\uFF1A\r\n"); panel.add(textArea); panelButton = new Panel(); panel.add(panelButton); panelButton.setLayout(new GridLayout(3, 1, 0, 0)); buttonReduce = new JButton("\u56FE\u7247\u7F29\u5C0F\u4E00\u500D"); panelButton.add(buttonReduce);buttonReduce.addActionListener(this); buttonEnlarge = new JButton("\u56FE\u7247\u653E\u5927\u4E00\u500D"); panelButton.add(buttonEnlarge);buttonEnlarge.addActionListener(this); panelText = new Panel(); panelButton.add(panelText); panelText.setLayout(new GridLayout(1, 0, 0, 0)); labelHight = new JLabel("\u957F\u5EA6(px)"); panelText.add(labelHight); textHight = new JTextField(); panelText.add(textHight); textHight.setColumns(10); labelWidth = new JLabel("\u5BBD\u5EA6(px)"); panelText.add(labelWidth); textWidth = new JTextField(); panelText.add(textWidth); textWidth.setColumns(10); buttonZoom = new JButton("\u56FA\u5B9A\u7F29\u653E"); panelText.add(buttonZoom); buttonZoom.addActionListener(this); JMenuBar menuBar = new JMenuBar(); frame.setJMenuBar(menuBar); JMenu newMenu = new JMenu("\u6587\u4EF6\r\n"); menuBar.add(newMenu); itemOpen = new JMenuItem("\u6253\u5F00\u56FE\u7247\r\n"); newMenu.add(itemOpen);itemOpen.addActionListener(this); itemSave = new JMenuItem("\u4FDD\u5B58\u56FE\u7247\r\n"); newMenu.add(itemSave);itemSave.addActionListener(this); itemAbout = new JMenuItem("\u5173\u4E8E"); newMenu.add(itemAbout);itemAbout.addActionListener(this); JSeparator separator = new JSeparator(); newMenu.add(separator); itemExit = new JMenuItem("\u9000\u51FA\r\n"); newMenu.add(itemExit);itemExit.addActionListener(this); dialogOpen = new FileDialog(frame, "選擇一張圖片", FileDialog.LOAD); dialogSave = new FileDialog(frame, "選擇保存圖片的路徑", FileDialog.SAVE); } /** * 界面交互,響應事件(調用對應的函數) */ @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == itemSave) { saveImage(); } else if (e.getSource() == itemOpen) { openImage(); } else if (e.getSource() == itemExit) { System.exit(0); } else if (e.getSource() == itemAbout) { JOptionPane.showMessageDialog(null, "圖片縮放程序:PhotoZoomer 1.0", "版本", JOptionPane.INFORMATION_MESSAGE); } else if (e.getSource() == buttonEnlarge) { enlargeImage(); } else if (e.getSource() == buttonReduce) { reduceImage(); } else if (e.getSource() == buttonZoom) { zoomImage(); } } /** * 響應事件封裝成函數 */ //打開圖片 private void openImage() { try { // 創建一個不帶透明色的BufferedImage對象 bufferedImage = new BufferedImage(1920, 890, BufferedImage.TYPE_INT_RGB); bufferedImage.flush(); graphics = bufferedImage.getGraphics(); //打開對話框 dialogOpen.setVisible(true); image = ImageIO.read(new File(dialogOpen.getDirectory() + dialogOpen.getFile())); //判斷圖片是否存在 if (image != null) { graphics.drawImage(image,0,0, null); canvas.repaint(); } //添加提示 textArea.append("打開圖片成功!\n圖片路徑:" + dialogOpen.getDirectory()+"\n"+"圖片名稱:"+dialogOpen.getFile()+"\n"); } catch (IOException e) { e.printStackTrace(); System.out.println("打開圖片發生錯誤!"); } } //保存圖片 private void saveImage() { try { dialogSave.setVisible(true); ImageIO.write(bufferedImage, "jpeg", new File(dialogSave.getDirectory() + dialogSave.getFile())); //添加提示 textArea.append("添加圖片成功!\n保存目錄:"+dialogSave.getDirectory()+"\n"); } catch (IOException e) { e.printStackTrace(); System.out.println("保存圖片發生錯誤!"); } } //固定放縮圖片 private void zoomImage() { int height = Integer.parseInt(textHight.getText()); int width = Integer.parseInt(textWidth.getText()); //判斷輸入是否符合條件 if (height > 0 && width > 0 && height <= 890 && width <= 1920 ) { bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); graphics = bufferedImage.getGraphics(); graphics.drawImage(image, 0, 0, width, height, null); canvas.repaint(); textArea.append("\n圖片縮放為高:"+height+"px,寬:"+width+"px\n"); textHight.setText(""); textWidth.setText(""); } else { textArea.append("\n請輸入正確的圖片寬度和長度!"); textHight.setText(""); textWidth.setText(""); } } //放大圖片一倍 private void enlargeImage() { int height = image.getHeight(null) * 2; int width = image.getWidth(null) * 2; //判斷輸入是否符合條件 if (height > 0 && width > 0 && height <= 890 && width <= 1920 ) { bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); graphics = bufferedImage.getGraphics(); graphics.drawImage(image, 0, 0, width, height, null); canvas.repaint(); textArea.append("\n圖片縮放為高:"+height+"px,寬:"+width+"px\n"); textHight.setText(""); textWidth.setText(""); } else { textArea.append("\n不能再進行放大了!"); textHight.setText(""); textWidth.setText(""); } } //縮小圖片一倍 private void reduceImage() { int height = image.getHeight(null) / 2; int width = image.getWidth(null) / 2; //判斷輸入是否符合條件 if (height > 0 && width > 0 && height <= 890 && width <= 1920 ) { bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); graphics = bufferedImage.getGraphics(); graphics.drawImage(image, 0, 0, width, height, null); canvas.repaint(); textArea.append("\n圖片縮放為高:"+height+"px,寬:"+width+"px\n"); textHight.setText(""); textWidth.setText(""); } else { textArea.append("\n不能再進行縮小了!"); textHight.setText(""); textWidth.setText(""); } } }