/***************************************** 影像處理基礎 *************************************************/ import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; import javax.imageio.*; import java.awt.image.*; public class ui44a extends JFrame implements ActionListener { Container c; JLabel lab; JButton bot,bot1; Image img=null; ImageIcon icon=null; java.awt.image.BufferedImage bi; String fn1; int index,more,R,G,B,i,cpt; int flag=0; public ui44a() //建構元 { super("影像處理基礎"); c=getContentPane(); setSize(640,480); bot=new JButton("讀入影像"); bot1=new JButton("處理"); icon=new ImageIcon(); lab=new JLabel(icon); c.setLayout(new FlowLayout(FlowLayout.CENTER));//設定為流水版面設定 //注意加入元件的順序 c.add(bot1); c.add(bot); c.add(lab); bot.addActionListener(this);//設定自己為bot0按鈕的傾聽者 setVisible(true); } public void paint(Graphics g) //真正的畫圖設定 { super.paint(g);//畫出元件 } public void actionPerformed(ActionEvent e) //按鈕事件的處理方法 { if (e.getSource()==bot) { JFileChooser jfc = new JFileChooser("./"); jfc.setFileSelectionMode(JFileChooser.FILES_ONLY); int rst = jfc.showOpenDialog(this); if(rst==JFileChooser.APPROVE_OPTION) fn1 = jfc.getSelectedFile().toString(); try { bi=ImageIO.read(new File(fn1)); icon.setImage((Image)bi); } catch (Exception ee) { System.out.println(ee.toString()); } repaint(); } else if (e.getSource()==bot1) { } } /***主程式***/ public static void main(String args[]) //程式起點 { ui44a app=new ui44a(); //畫圖 app.addWindowListener(new WindowAdapter(){ //匿名內部類別 public void windowClosing(WindowEvent e) { System.exit(0); } }); //處理視窗關閉要求 } }