




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、一.課程設計的任務及要求二.需求分析圖形化界面(GUI)編程,編寫一個圖片瀏覽器程序可以支持“.GIF”, “.JPEG”,“.jpeg”,“.TGA”,“.JPG”,“.jpg”等格式,單張打開圖片,可以將同一目錄下的圖片按縮略圖打開按“上一張”“下一張”按鈕可以顯示相應圖片。運行Applet時,圖像不是一氣呵成的,因為方法不是吧圖像完整的裝入內(nèi)存再顯示的。于此相反,方法創(chuàng)建一個線程,該線程與Applet的原有線程并發(fā)執(zhí)行,一邊裝入一邊顯示,從而產(chǎn)生上了不聯(lián)需顯示的現(xiàn)象。為了提高圖像才顯示效果,可以采用雙緩沖技術:首先把圖像裝入內(nèi)存,然后再顯示在屏幕上。三. 設計思路3.1界面設計選擇圖片按
2、鈕:主要用dir函數(shù)實現(xiàn)圖片的遍歷。上一張,下一張:通過做標軸回調(diào)函數(shù)實現(xiàn)。由于本軟件為單機軟件,不需要大量的數(shù)據(jù)讀寫和數(shù)據(jù)交換,實現(xiàn)上、下功能要求只能讀取PictureBox控件當前加載的目錄,讀取當前路徑,創(chuàng)建一維數(shù)組。frame = new Frame(PictureViewer); Panel pb = new Panel(); Button select = new Button(選擇圖片); previous = new Button(上一張); next = new Button(下一張); select.addActionListener(this); previous.add
3、ActionListener(this);3.2.圖像加載:Applet常用來顯示儲存在文件中的圖像,多數(shù)Applet使用的是GIF或JPEG格式的圖像文件。需Applet加載圖像只需首先定義Image對象,然后使用getImage()方法把圖像和文件結合起來即可。image_width = bi.getWidth(this); image_height = bi.getHeight(this); double image_proportion = 1.0 * image_height / image_width; System.out.println(image: w +image_widt
4、h+ ,h +image_height+ ,p1 +image_proportion);if(image_proportion screen_proportion) image_height = screen_height; image_width = (int)(image_height / image_proportion); System.out.println( p1p0 w= +image_width); else image_width = screen_width; image_height = (int)(image_width * image_proportion); Sys
5、tem.out.println( p0p1 h= +image_height); 四.詳細設計4.1.程序設計流程圖開始圖片上一張查找盤符圖片下一張查找文件夾結束查找文件4.2.源程序代碼package C;import java.io.File;import java.io.FilenameFilter;public class MyFilter implements FilenameFilter private String extension; public MyFilter() extension = new String.jpg, .JPG, .gif, .GIF, .png, .P
6、NG, .jpeg, .JPEG; public MyFilter(String extension) this.extension = extension; public boolean accept(File dir,String name) for(String s : extension) if(name.endsWith(s) return true; return false; package C;import java.awt.*;import java.awt.event.*;import java.awt.image.*;public class MyCanvas exten
7、ds Canvas implements ComponentListener /* * */private static final long serialVersionUID = 1L;private BufferedImage bi; private Image im; private int image_width; private int image_height; public void setImage(BufferedImage bi) this.bi = bi; this.zoom(); public void paint(Graphics g) g.drawImage(im,
8、(this.getWidth()-image_width)/2,(this.getHeight()-image_height)/2,this); public void componentResized(ComponentEvent e) if(bi != null) System.out.println(resize!); this.zoom(); this.repaint(); public void componentMoved(ComponentEvent e) public void componentShown(ComponentEvent e) public void compo
9、nentHidden(ComponentEvent e) public void zoom() if(bi = null) return; int screen_width = this.getWidth(); int screen_height = this.getHeight(); double screen_proportion = 1.0 * screen_height / screen_width; System.out.println(screen: w +screen_width+ ,h +screen_height+ ,p0 +screen_proportion); image
10、_width = bi.getWidth(this); image_height = bi.getHeight(this); double image_proportion = 1.0 * image_height / image_width; System.out.println(image: w +image_width+ ,h +image_height+ ,p1 +image_proportion); if(image_proportion screen_proportion) image_height = screen_height; image_width = (int)(imag
11、e_height / image_proportion); System.out.println( p1p0 w= +image_width); else image_width = screen_width; image_height = (int)(image_width * image_proportion); System.out.println( p0p1 h= +image_height); im = bi.getScaledInstance(image_width,image_height,Image.SCALE_SMOOTH); package C;import java.aw
12、t.*;import java.awt.event.*;import java.awt.image.*;import java.io.*;import javax.imageio.*;public class T implements ActionListener private Frame frame; private MyCanvas mc ; private String fpath; private String fname; private File files; private int findex ; private FileDialog fd_load; private MyF
13、ilter filter; private Button previous ; private Button next ; public static void main( String args) throws Exception new T().init(); public void init() frame = new Frame(PictureViewer); Panel pb = new Panel(); Button select = new Button(選擇圖片); previous = new Button(上一張); next = new Button(下一張); sele
14、ct.addActionListener(this); previous.addActionListener(this); next.addActionListener(this); pb.add(select); pb.add(previous); pb.add(next); mc = new MyCanvas(); mc.setBackground(new Color(200,210,230); mc.addComponentListener(mc); frame.add(pb,North); frame.add(mc,Center); frame.setSize(360,360); fr
15、ame.setLocation(400,200); frame.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); frame.setVisible(true); this.validateButton(); filter = new MyFilter(); fd_load = new FileDialog(frame,打開文件,FileDialog.LOAD); fd_load.setFilenameFilter(filter); public vo
16、id actionPerformed(ActionEvent e) String command = e.getActionCommand(); if(command.equals(選擇圖片) fd_load.setVisible(true); fpath = fd_load.getDirectory(); fname = fd_load.getFile(); if(fpath != null) & (fname != null) this.display(new File(fpath + fname); files = new File(fpath).listFiles(filter); t
17、his.setIndex(); else if(command.equals(上一張) findex-; if(findex= files.length) findex = files.length-1; this.display(filesfindex); this.validateButton(); public void display(File f) try BufferedImage bi = ImageIO.read(f); mc.setImage(bi); frame.setTitle(PictureViewer - + f.getName() + ); catch(Except
18、ion e) e.printStackTrace(); mc.repaint(); public void setIndex() File current = new File(fpath + fname); if(files != null) for(int i=0;i 0); next.setEnabled(files!=null) & (findex(files.length-1); 五.運行調(diào)試與分析討論5.1.將同一目錄下的圖片按縮略圖打開 5.2.單張打開圖片5.3.按”上一張”,”下一張”按鈕打開圖片六. 設計體會與小結我通過這次編程實踐學習到了Image和Griphics相關的類的使用。首先我通過網(wǎng)上搜集資料和自己看jdk api對Image、graphics、swing中的類有了實質(zhì)的操練,對它們的理解有了進一步的理解。不象以前只有模糊的記憶,根本不會運用到實際的情況中,僅僅照著課本超代碼。不過由于技術水平限制,寫的代
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五股東協(xié)議補充協(xié)議-股東對公司可持續(xù)發(fā)展戰(zhàn)略的承諾
- 二零二五年度跨境拖車服務及關稅代理合同
- 二零二五年度商業(yè)廣場購物中心房屋租賃與商業(yè)數(shù)據(jù)分析服務合同
- 2025年度閑置校舍租賃合同及校園內(nèi)環(huán)保能源利用合作協(xié)議
- 2025年度美容美發(fā)加盟合同解除書
- Unit 4 Did You Have a Nice Trip?單元基礎知識復習(含答案)
- 2025年度高校學生實習就業(yè)雙選協(xié)議書
- 二零二五年度企業(yè)員工社保權益自愿放棄協(xié)議范本
- 二零二五年度海洋地質(zhì)調(diào)查海域使用權租賃與研究開發(fā)協(xié)議
- 二零二五年度交通事故私了賠償處理協(xié)議
- Unit2大單元整體教學設計-小學英語四年級上冊(Joinin外研劍橋英語)
- 人美版(2024)七年級上冊美術第二單元 色彩魅力第1課《自然的色彩》教學設計
- 2024年水利安全員(B證)考試題庫-下(多選、判斷題)
- 酒店室內(nèi)裝修工程施工組織設計
- 神經(jīng)病學專業(yè)英語詞匯
- 2024年高級纖維檢驗員職業(yè)鑒定理論考試題庫(含答案)
- 心肺復蘇科普課件
- 員工食堂就餐統(tǒng)計表
- 2024至2030年中國醫(yī)療保險行業(yè)市場深度分析及投資戰(zhàn)略規(guī)劃報告
- 小數(shù)除法100道豎式計算題及答案
- 《婦幼保健學》課件-第三章 兒童期保健
評論
0/150
提交評論