JAVA實驗報告-簡單繪圖程序_第1頁
JAVA實驗報告-簡單繪圖程序_第2頁
JAVA實驗報告-簡單繪圖程序_第3頁
JAVA實驗報告-簡單繪圖程序_第4頁
JAVA實驗報告-簡單繪圖程序_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、實驗三繪制圖形一、實驗目的學會JButton的使用,能進行JButton的基本操作,能使用JButton構造一個圖形界面框架通過實驗熟悉swing圖形用戶界面程序設計熟悉java中的事件處理模型,每個事件動作代表的意義進一步提高編程能力,學會獨立思考,學會上網查找資料決問題。二、實驗內容設計一個程序,該程序能夠繪制基本的圖形(包括圓形、矩形、直線等);要求有一個簡單易操作的用戶界面(包括菜單、按鈕等組件)。編寫的程序要求能夠實現(xiàn)保存和讀取的功能。三、實驗代碼Main.javaimportjava.awt.BorderLayout;importjava.awt.event.ActionListe

2、ner;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JToolBar;importimportwinpaint.WinToolBar;.windowsPaintpublicclassMain/*paramargs*/publicstaticvoidmain(Stringargs)/TODOAuto-generatedmethodstubJPanelpaintWin=newwindowsPaint(畫圖”);paintWin.setBounds(0,30

3、,1000,1000);paintWin.setLayout(null);paintWin.setvisible(true);JToolBartool=newWinToolBar(windowsPaint)paintWin);tool.setBounds(0,0,1000,30);tool.setVisible(true);JFrameframe=newJFrame();frame.setLayout(null);frame.setVisible(true);frame.setBounds(0,0,1000,1000);frame.add(tool);frame.add(paintwin);f

4、rame.add(paintwin);test,javaimportjava.awt.GradientPaint;importjava.lang.reflect.Constructor;importbase.GrapicsElem;importbase.Rect;publicclasstestfractor/*paramargsthrowsClassNotFoundException*/publicstaticvoidmain(Stringargs)throwsClassNotFoundException/TODOAuto-generatedmethodstubGrapicsElemGe=ne

5、wRect();System.out.println(Ge.getClass().getName();Classdemo1=null;Classdemo2=null;Classdemo3=null;try/一般盡量采用這種形式demo1=Class.forName(base.Rect);Classcon=demo1.getSuperclass();System.out.println(con.getName();demo2=Class.forName(base.GrapicsElem);Classsuns=demo2.getInterfaces();for(inti=0;isuns.lengt

6、h;i+)System.out.println(sunsi.getName();System.out.println(con.length);/GrapicsElemgg=(GrapicsElem)con0.newInstance();System.out.println(con_0);System.out.println(con1);System.out.println(con2);catch(Exceptione)e.printStackTrace();demo2=newRect().getClass();demo3=Rect.class;+demo1.getName();+demo2.g

7、etName();+demo3.getName();System.out.println(類名稱System.out.println(類名稱System.out.println(類名稱GrapicsElem.javapackagebase;importjava.awt.Color;importjava.awt.Graphics;importjava.awt.Point;publicabstractclassGrapicsElemPointstart;Pointend;privateColorcolor;publicColorgetColor()returncolor;publicvoidset

8、Color(Colorcolor)this.color=color;publicvoidsetStart(Pointstart)this.start=start;publicvoidsetEnd(Pointend)abstractvoidabstractvoidabstractvoidabstractvoidjava.util.ArrayList;classLineextendsGrapicsElemthis.end=end;publicpublicpublicpublicLine.javapackagebase;importjava.awt.Graphics;importjava.awt.P

9、oint;importpublicdoMousePress(Pointp);doMouseDrag(Pointp);doMouseRelease(Pointp);draw(Graphicsg);ArrayListtrack;publicLine()track=newArrayList();Overridepublicvoiddraw(Graphicsg)for(inti=0;itrack.size()-1;i+)Pointcur=track.get(i);Pointnext=track.get(i+1);g.drawLine(cur.x,cur.y,next.x,next.y);private

10、voidAddPoint(Pointp)track.add(p);OverridepublicvoiddoMousePress(Pointp)/TODOAuto-generatedmethodstubAddPoint(p);OverridepublicvoiddoMouseDrag(Pointp)/TODOAuto-generatedmethodstubAddPoint(p);OverridepublicvoiddoMouseRelease(Pointp)/TODOAuto-generatedmethodstubAddPoint(p);Rect.javapackagebase;importja

11、va.awt.Graphics;importjava.awt.Point;publicclassRectextendsGrapicsElemintx,y,width,height;Overridepublicvoiddraw(Graphicsg)/TODOAuto-generatedmethodstubg.drawRect(getX(),getY(),getWidth(),getHeight();OverridepublicvoiddoMousePress(Pointp)super.setStart(p);super.setEnd(p);OverridepublicvoiddoMouseDra

12、g(Pointp)super.setEnd(p);OverridepublicvoiddoMouseRelease(Pointp)super.setEnd(p);publicintgetX()returnMath.min(start.x,end.x);publicintgetY()returnMath.min(start.y,end.y);publicintgetWidth()returnMath.abs(start.x-end.x);publicintgetHeight()returnMath.abs(start.y-end.y);Circle.javapackagebase;importj

13、ava.awt.Graphics;publicclassCircleextendsRectOverridepublicvoiddraw(Graphicsg)/TODOAuto-generatedmethodstubg.drawOval(getX(),getY(),getWidth(),getHeight();windowsPaint.javapackagewinpaint;importjavaawt.BasicStroke;importjavaawt.Button;importjavaawt.Color;importjavaawt.Font;importjavaawt.Graphics;imp

14、ortjavaawtGraphics2D;importjavaawt.Image;importjavaawt.Point;importjavaawt.eventActionEvent;importjavaawt.eventActionListener;importjavaawt.event.MouseEvent;importjavaawt.event.MouseListener;importjavaawt.event.MouseMotionListener;importjavaioFile;importjavaioIOException;importjavanioBuffer;importja

15、vautil.ArrayList;importjavautil.BitSet;importjavautil.List;importjavaawtgeom*;importjavaawtimageBufferedImage;importjavaximageioImagelO;importjavax.swingImageIcon;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JToolBar;importbas

16、e.Circle;importbase.GrapicsElem;importbase.Line;importbaseRect;importjavaawt.BorderLayout;publicclasswindowsPaintextendsJPanelimplementsMouseListener,MouseMotionListenerStringcommand=矩形;ListLg=newArrayList();GrapicsElemcur;JLabellabel;publicwindowsPaint(Stringtitle)init();privatevoidinit()addMouseLi

17、stener(this);addMouseMotionListener(this);/*可以使用反射去掉判斷語句、Classg=ClassforName(baseRect);gnewInstance();return*/privateGrapicsElemnewInstance()command=WinToolBar.Command;if(commandequals(矩形)returnnewRect();elseif(commandequals(圓形)returnnewCircle();elseif(commandequals(線)returnnewLine();returnnull;/Ove

18、rridepublicvoidpaint(Graphicsg)/TODOAuto-generatedmethodstubsuperpaint(g);Graphics2Dg2d=(Graphics2D)g;g2dsetColor(curgetColor();/System.outprintln(curgetColor();g2dsetStroke(newBasicStroke(100f);for(inti=0;iLgsize();i+)GrapicsElemge=Lgget(i);gedraw(g);/Override/publicvoidrepaint()/TODOAuto-generated

19、methodstub/TODOAuto-generatedmethodstub/superrepaint();Threadthread=newThread(newRunnable()Overridepublicvoidrun()/TODOAuto-generatedmethodstubtrySaveToFile();catch(IOExceptione)/TODOAuto-generatedcatchblockeprintStackTrace(););threadstart();Imageiconbg=newImageIcon(tmpjpg);if(bg!=null)if(label!=nul

20、l)this.remove(label);label=newJLabel(bg);labelsetBounds(0,0,bggetIconWidth(),bggetIconHeight();/thisadd(label);/superrepaint();/privatevoidSaveToFile()throwsException/TODOAuto-generatedmethodstubBufferedlmagebimg=newBufferedImage(thisgetWidth(),thisgetHeight(),BufferedlmageTYPE_3BYTE_BGR);Graphics2D

21、g2d=bimgcreateGraphics();thispaint(g2d);ImageIO.write(bimg,jpg,newFile(tmpjpg);OverridepublicvoidmousePressed(MouseEvente)Pointp=newPoint(egetX(),egetY();cur=newInstance();cur.doMousePress(p);Lgadd(cur);OverridepublicvoidmouseDragged(MouseEvente)Pointp=newPoint(egetX(),egetY();cur.doMouseDrag(p);rep

22、aint();OverridepublicvoidmouseReleased(MouseEvente)Pointp=newPoint(egetX(),egetY();cur.doMouseRelease(p);repaint();OverridepublicvoidmouseMoved(MouseEvente)/TODOAuto-generatedmethodstubOverridepublicvoidmouseClicked(MouseEvente)/TODOAuto-generatedmethodstubOverridepublicvoidmouseEntered(MouseEvente)

23、/TODOAuto-generatedmethodstubOverridepublicvoidmouseExited(MouseEvente)/TODOAuto-generatedmethodstubWinToolBar.javapackagewinpaint;importjavaawt.Color;importjavaawt.eventActionEvent;importjavaawt.eventActionListener;importjavaawt.event.ItemEvent;importjavaawt.event.ItemListener;importjavax.swing.JBu

24、tton;importjavax.swing.JComboBox;importjavax.swing.JSeparator;importjavax.swing.JToolBar;publicclassWinToolBarextendsJToolBarimplementsActionListenerpublicstaticStringCommand=矩形;JButtonbtn1,btn2,btn3;/windowsPaintwinp;publicWinToolBar(windowsPaintwinP)btnl=newJButton(矩形);btn2=newJButton(圓形);btn3=newJButton(線);thisadd

溫馨提示

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

評論

0/150

提交評論