1. 程式人生 > >通過選單控制元件開啟一個本地的圖片(java)

通過選單控制元件開啟一個本地的圖片(java)

package product;

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;


import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

public class product extends JFrame {

JPanel jp1, jp2;
private int currentPixArray[] = null;// 儲存當前操作的畫素矩陣
private String fileString = null;// 影象的路徑
private JLabel imageLabel = null;// 用於顯示影象的標籤
private BufferedImage newImage;// 載入的影象
private int h, w;// 影象的高和寬
private LinkedList<int[]> imageStack = new LinkedList<int[]>();// 儲存歷史操作影象矩陣
private LinkedList<int[]> tempImageStack = new LinkedList<int[]>();// 儲存歷史操作影象矩陣


// 建構函式
public product(String title) {
super(title);
jp1 = new JPanel();
jp2 = new JPanel();
JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jp1, jp2);
add(jsp);
setTitle("product");
setBounds(300, 200, 1500, 1000);
setDefaultCloseOperation(EXIT_ON_CLOSE);
jsp.setDividerLocation(0.5);// 在1/2處進行拆分


// 建立選單
//選單1
JMenuBar jb = new JMenuBar();
JMenu fileMenu = new JMenu("檔案");
jb.add(fileMenu);


JMenuItem openImageMenuItem = new JMenuItem("開啟影象");
fileMenu.add(openImageMenuItem);
openImageMenuItem.addActionListener(new OpenListener());


JMenuItem exitMenu = new JMenuItem("退出");
fileMenu.add(exitMenu);
exitMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});


this.setJMenuBar(jb);//將選單組載入到Frame窗體中

        //載入圖片
imageLabel = new JLabel("");//影象標籤初始化
// jp1.add(imageLabel,new Integer(Integer.MIN_VALUE));
jp1.add(imageLabel);//將標籤載入到jp1中顯示


this.setVisible(true);//將整個Frame窗體顯示出來
}


private class OpenListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser jc = new JFileChooser();
int returnValue = jc.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = jc.getSelectedFile();
if (selectedFile != null) {
fileString = selectedFile.getAbsolutePath();
try {
newImage = ImageIO.read(new File(fileString));
w = newImage.getWidth();
h = newImage.getHeight();
currentPixArray = getPixArray(newImage, w, h);// 儲存影象的數子矩陣
imageStack.clear();
tempImageStack.clear();
imageStack.addLast(currentPixArray);
imageLabel.setIcon(new ImageIcon(newImage));


} catch (IOException ex) {
System.out.println(ex);
}


}
}
product.this.repaint();
}
}


////////////////// 獲取影象畫素矩陣/////////
private int[] getPixArray(Image im, int w, int h) {
int[] pix = new int[w * h];
PixelGrabber pg = null;
try {
pg = new PixelGrabber(im, 0, 0, w, h, pix, 0, w);
if (pg.grabPixels() != true)
try {
throw new java.awt.AWTException("pg error" + pg.status());
} catch (Exception eq) {
eq.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();


}
return pix;
}


public void showphoto_left(int[] srcPixArray) {
Icon icon;// 宣告圖示
JLabel l;// 宣告標籤
// main = new JPanel();
l = new JLabel();
Image pic = createImage(new MemoryImageSource(w, h, srcPixArray, 0, w));
icon = new ImageIcon(pic);// 將圖示用圖片檔案例項化
// 在此直接建立物件,注意目錄之間的分隔符是雙斜線
l.setIcon(icon);
l.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
jp1.add(l, new Integer(Integer.MIN_VALUE));// 將標籤新增到main這個面板容器中
// jp1.add(main);//將main面板新增到jp1面板中
// pack();//(調整視窗的大小,使其適應元件的大小和佈局)
}


public void showphoto_right(int[] srcPixArray) {
Icon icon;// 宣告圖示
JLabel l;// 宣告標籤
// main = new JPanel();
l = new JLabel();
Image pic = createImage(new MemoryImageSource(w, h, srcPixArray, 0, w));
icon = new ImageIcon(pic);// 將圖示用圖片檔案例項化
// 在此直接建立物件,注意目錄之間的分隔符是雙斜線
l.setIcon(icon);
l.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
jp2.add(l, new Integer(Integer.MIN_VALUE));// 將標籤新增到main這個面板容器中
// jp1.add(main);//將main面板新增到jp1面板中
// pack();//(調整視窗的大小,使其適應元件的大小和佈局)
}


public static void main(String[] args) {
new product("ShowImage");
}


}