計算表圖形面積課程設(shè)計報告_第1頁
計算表圖形面積課程設(shè)計報告_第2頁
計算表圖形面積課程設(shè)計報告_第3頁
計算表圖形面積課程設(shè)計報告_第4頁
計算表圖形面積課程設(shè)計報告_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

精選優(yōu)質(zhì)文檔-----傾情為你奉上精選優(yōu)質(zhì)文檔-----傾情為你奉上專心---專注---專業(yè)專心---專注---專業(yè)精選優(yōu)質(zhì)文檔-----傾情為你奉上專心---專注---專業(yè)中南林業(yè)科技大學課程設(shè)計報告設(shè)計名稱:計算圖形面積姓名:學號:專業(yè)班級:院(系):計算機與與信息工程設(shè)計時間:設(shè)計地點:中南林業(yè)科技大學指導教師評語:成績:簽名:2013年1月11日設(shè)計目的1.深入理解Java面向?qū)ο蟮脑O(shè)計思想2.提高用Java語言編程的能力3.學會制作軟件開發(fā)文二、課程要求分析2.1本課程及程序功能要求課程名稱:計算圖形面積的制作要求:編寫一個體現(xiàn)MVC結(jié)構(gòu)的GUI程序。首先編寫一個圖形接口以及實現(xiàn)該接口的圓類、梯形類、三角形類等,然后再編寫一個窗口。要求:1.能夠選擇具體的圖形;2.使用若干文本框和一個文本區(qū)為該圖形對象中的數(shù)據(jù)提供視圖,其中文本框用來顯示和更新圖形對象的基本要素(如梯形的上底、下底和高),文本區(qū)對象用來顯示圖形的面積。窗口中有一個按鈕,用戶單擊該按鈕后,程序用文本框中的數(shù)據(jù)作為圖形對象的基本要素,并將計算出的圖形面積顯示在文本區(qū)中。2.2課程介紹計算圖形面積使用的事JavaSE中的GUI和線程基本技術(shù),可以實現(xiàn)三角形、圓、梯形的面積計算。具體操作如下:點擊單選按鈕選擇要計算面積的圖形形狀。輸入所選擇圖形對象的基本要素(如梯形的上底、下底、高)。點擊按鈕“確定”,可以在文本區(qū)顯示計算得出的結(jié)果。點擊右上角的關(guān)閉鍵可以退出本程序。三、程序流程圖a、b、c構(gòu)成三角形?輸入邊長a、b、c輸入半徑r輸入上下底a1、a2、高hflag=3flag=2flag=1圓If(e.getSource().equals(radio2))三角形If(e.getSource().equals(radio1))選擇圖形開始a、b、c構(gòu)成三角形?輸入邊長a、b、c輸入半徑r輸入上下底a1、a2、高hflag=3flag=2flag=1圓If(e.getSource().equals(radio2))三角形If(e.getSource().equals(radio1))選擇圖形開始梯形梯形If(e.getSource().equals(radio3))area=(a1+a2)*h/2area=3.14*r*rarea=(a1+a2)*h/2area=3.14*r*rNP=(sideA+sideB+sideC)/2area=Math.sqrt(p*(p-sideA)*(p-side)*(p-sideC))無法計算面積NP=(sideA+sideB+sideC)/2area=Math.sqrt(p*(p-sideA)*(p-side)*(p-sideC))無法計算面積YIf(e.getSource().equals(button)) If(e.getSource().equals(button))flag=3flag=1flag=3flag=1flag=2flag=2調(diào)用lader并插入計算結(jié)果到文本區(qū)調(diào)用circle并插入計算結(jié)果到文本區(qū)調(diào)用triangle并插入計算結(jié)果到文本區(qū)調(diào)用lader并插入計算結(jié)果到文本區(qū)調(diào)用circle并插入計算結(jié)果到文本區(qū)調(diào)用triangle并插入計算結(jié)果到文本區(qū)結(jié)束四、程序設(shè)計介紹4.1本程序所用知識點(1)GUI概念圖形用戶界面(GraphicalUserInterface,簡稱GUI)是指采用圖形方式顯示的計算機操作用戶接口。與早期計算機使用的命令行界面相比,圖形界面對于用戶來說在視覺上更易于接受。運行Java程序有兩種方式,一是命令行輸出,另一種就是圖形用戶界面輸出(2)MVC結(jié)構(gòu)模型-視圖-控制器(Model-View-Controller),簡稱MVC。MVC是一種先進的設(shè)計結(jié)構(gòu),其目的是以會話框形式提供方便的GUI支持。(3)布局管理FlowLayout(流式布局管理器)這個簡單的“Flow”的組件安裝在窗體中,從左到右,直到頂部的空格全部再移去一行,并繼續(xù)循環(huán)這些組件。(4)創(chuàng)建一個窗體窗口中可以存放各種組件,所以窗體是容器,可以被直接顯示、繪制在操作系統(tǒng)所控制的平臺上。(5)事件處理事件處理機制事件:用戶對組件的一個操作事件源:發(fā)生事件的組件監(jiān)聽器:我們需要處理某個事件的時候,就需要在發(fā)生事件的組件上添加監(jiān)聽器。事件處理器:監(jiān)聽器中的方法,添加監(jiān)聽器后,組件發(fā)生了對應(yīng)的事件就會執(zhí)行指定的方法(6)所用組件JLabel創(chuàng)建標簽;JTextField創(chuàng)建文本框;JRadioButton創(chuàng)建單選按鈕;JTextArea創(chuàng)建文本區(qū);JButton創(chuàng)建可單擊按鈕程序代碼設(shè)計Main.javapublicclassMain{ publicstaticvoidmain(String[]args){ NewFramenf=newNewFrame(); nf.setVisible(true); }}NewFrame.javaimportjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.ButtonGroup;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JRadioButton;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;importjavax.swing.JTextField;publicclassNewFrameextendsJFrameimplementsActionListener{ //創(chuàng)建對象 intflag; Triangletriangle=newTriangle();//創(chuàng)建對象triangle并分配內(nèi)存空間 Circlecircle=newCircle(5);//創(chuàng)建對象circle并分配內(nèi)存空間 Laderlader=newLader(10,20,10);//創(chuàng)建對象lader并分配內(nèi)存空間 ButtonGroupgroup;//創(chuàng)建按鈕群 JLabellabel,a,b,c,r,a1,a2,h,result;//創(chuàng)建標簽 JTextFieldt1,t2,t3,t4,t5,t6,t7;// 創(chuàng)建文本框 JRadioButtonradio1,radio2,radio3;//創(chuàng)建單選按鈕 JTextAreaarea;//創(chuàng)建文本區(qū) JButtonbutton;//創(chuàng)建可單擊按鈕 NewFrame(){ //設(shè)置窗體風格,包括大小,界面,標題等等 this.setSize(400,500);//設(shè)置窗口大小 this.setTitle("計算圖形面積");//設(shè)置窗口標題 this.setLayout(null);//設(shè)置布局為空 this.setResizable(false);//不可改變窗體大小 this.setLocationRelativeTo(null);//窗口居中顯示 this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);//關(guān)閉窗口時退出 //為對象分配內(nèi)存 group=newButtonGroup(); radio1=newJRadioButton("三角形"); radio2=newJRadioButton("圓"); radio3=newJRadioButton("梯形"); label=newJLabel("選擇形狀"); a=newJLabel("邊a:"); b=newJLabel("邊b:"); c=newJLabel("邊c:"); r=newJLabel("半徑:"); a1=newJLabel("上底:"); a2=newJLabel("下底:"); h=newJLabel("高:"); t1=newJTextField(5); t2=newJTextField(5); t3=newJTextField(5); t4=newJTextField(5); t5=newJTextField(5); t6=newJTextField(5); t7=newJTextField(5); area=newJTextArea(400,50); result=newJLabel("輸出結(jié)果"); button=newJButton("確定"); ////設(shè)置單選按鈕大小并添加到窗體 radio1.setBounds(40,30,80,50); this.add(radio1); group.add(radio1); radio2.setBounds(40,80,80,50); this.add(radio2); group.add(radio2); radio3.setBounds(40,130,80,50); this.add(radio3); group.add(radio3); //設(shè)置標簽的大小并添加到窗體上 label.setBounds(40,10,100,20); this.add(label); a.setBounds(120,50,40,10); this.add(a); b.setBounds(210,50,40,10); this.add(b); c.setBounds(300,50,40,10); this.add(c); r.setBounds(120,100,40,10); this.add(r); a1.setBounds(120,145,50,20); this.add(a1); a2.setBounds(210,145,50,20); this.add(a2); h.setBounds(300,145,50,20); this.add(h); result.setBounds(40,250,60,20); this.add(result); //設(shè)置文本框、文本區(qū)的大小并添加到窗體上 t1.setBounds(150,45,50,20); this.add(t1); t2.setBounds(240,45,50,20); this.add(t2); t3.setBounds(330,45,50,20); this.add(t3); t4.setBounds(160,95,50,20); this.add(t4); t5.setBounds(150,145,50,20); this.add(t5); t6.setBounds(240,145,50,20); this.add(t6); t7.setBounds(330,145,50,20); this.add(t7); area.setBounds(100,250,200,100); this.add(newJScrollPane(area));//為area綁定一個滾動條并添加到窗體 this.add(area); button.setBounds(150,400,100,30); this.add(button); //在按鈕上添加命令 radio1.addActionListener(this); radio2.addActionListener(this); radio3.addActionListener(this); button.addActionListener(this); } //以下是按鈕按下的事件處理 publicvoidactionPerformed(ActionEvente){ if(e.getSource().equals(radio1))//如果按下“三角形”按鈕 { flag=1; } if(e.getSource().equals(radio2))//如果按下“圓”按鈕 { flag=2; } if(e.getSource().equals(radio3))//如果按下“梯形”按鈕 { flag=3; } if(e.getSource().equals(button))//如果按下“確定”按鈕 { if(flag==1)//按下“三角形”按鈕時調(diào)用triangle { try{doublea=Double.parseDouble(t1.getText()); doubleb=Double.parseDouble(t2.getText()); doublec=Double.parseDouble(t3.getText()); triangle.setA(a); triangle.setB(b); triangle.setC(c);//輸入三邊長到文本框中 area.setText(""); if(triangle.getArea()!=0) { area.append("三角形的面積是:"); area.append("\n"); area.append(""+triangle.getArea());//輸入結(jié)果到文本區(qū) } else { area.append("無法計算面積"); } } catch(Exceptionex){ area.append("\n"+"\n"); } } if(flag==2)//按下“圓”按鈕時調(diào)用circle { try{doubler=Double.parseDouble(t4.getText().trim()); circle.setRadius(r);//輸入半徑到文本框中 area.setText(""); area.append("圓的面積是:"); area.append("\n"); area.append(""+circle.getArea());//插入結(jié)果到文本區(qū) } catch(Exceptionex){ area.append("\n"+"\n"); } } if(flag==3)//按下“梯形時”按鈕時調(diào)用lader { try{doublea1=Double.parseDouble(t5.getText().trim()); lader.setAbove(a1); doublea2=Double.parseDouble(t6.getText().trim()); lader.setBottom(a2); doubleh=Double.parseDouble(t7.getText().trim()); lader.setHeight(h);//輸入上底、下底、高到文本框中 area.setText(""); area.append("梯形的面積是:"); area.append("\n"); area.append(""+lader.getArea());//輸入結(jié)果到文本區(qū) } catch(Exceptionex){ area.append("\n"+"\n"); } } } } }Picture.javainterfacePicture{//創(chuàng)建Picture接口 doublegetArea();}Triangle.javapublicclassTriangleimplementsPicture{//實現(xiàn)Picture接口 doublesideA,sideB,sideC,area; booleanisTriangle; publicdoublegetArea(){ if(isTriangle){ doublep=(sideA+sideB+sideC)/2.0; area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC)); } else{ System.out.print("無法計算面積"); } returnarea; }//判斷a、b、c是否構(gòu)成三角形 publicvoidsetA(doublea){ sideA=a; if(sideA+sideB>sideC&&sideA+sideC>sideB&&sideB+sideC>sideA){ isTriangle=true; } else{ isTriangle=false; } } publicvoidsetB(doubleb){ sideB=b; if(sideA+sideB>sideC&&sideA+sideC>sideB&&sideB+sideC>sideA){ isTriangle=true; } else{ isTriangle=false; } } publicvoidsetC(doublec){ sideC=c; if(sideA+sideB>sideC&&sideA+sideC>sideB&&sideB+sideC>sideA){ isTriangle=true; } else{ isTriangle=false; } }}Circle.javapublicclassCircleimplementsPicture{//實現(xiàn)Picture接口 doubleradius,area; Circle(doubler){ radius=r; } publicdoublegetArea(){ returnarea=3.14*radius*radius; } voidsetR

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論