您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > JAVA实验报告——简单绘图程序
实验三绘制图形一、实验目的学会JButton的使用,能进行JButton的基本操作,能使用JButton构造一个图形界面框架通过实验熟悉swing图形用户界面程序设计熟悉java中的事件处理模型,每个事件动作代表的意义进一步提高编程能力,学会独立思考,学会上网查找资料决问题。二、实验内容设计一个程序,该程序能够绘制基本的图形(包括圆形、矩形、直线等);要求有一个简单易操作的用户界面(包括菜单、按钮等组件)。编写的程序要求能够实现保存和读取的功能。三、实验代码Main.javaimport;import;import;importimportimportimportwinpaint.WinToolBar;importwinpaint.windowsPaint;publicclassMain{/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubJPanelpaintWin=newwindowsPaint(画图);paintWin.setBounds(0,30,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);}}test.javaimportbase.GrapicsElem;importbase.Rect;publicclasstestfractor{/***@paramargs*@throwsClassNotFoundException*/publicstaticvoidmain(String[]args)throwsClassNotFoundException{//TODOAuto-generatedmethodstubGrapicsElemGe=newRect();Class?demo1=null;Class?demo2=null;Class?demo3=null;try{//一般尽量采用这种形式demo1=Class.forName(base.Rect);Class?con=demo1.getSuperclass();demo2=Class.forName(base.GrapicsElem);Class?suns[]=demo2.getInterfaces();for(inti=0;isuns.length;i++){}//GrapicsElemgg=(GrapicsElem)con[0].newInstance();con[0]);con[1]);con[2]);}catch(Exceptione){e.printStackTrace();}demo2=newRect().getClass();demo3=Rect.class;}}GrapicsElem.javapackagebase;importimportimportpublicabstractclassGrapicsElem{Pointstart;Pointend;privateColorcolor;publicColorgetColor(){returncolor;}publicvoidsetColor(Colorcolor){this.color=color;}publicvoidsetStart(Pointstart){this.start=start;}publicvoidsetEnd(Pointend){this.end=end;}publicabstractvoiddoMousePress(Pointp);publicabstractvoiddoMouseDrag(Pointp);publicabstractvoiddoMouseRelease(Pointp);publicabstractvoiddraw(Graphicsg);}Line.javapackagebase;importimportimportpublicclassLineextendsGrapicsElem{ArrayListPointtrack;publicLine(){track=newArrayListPoint();}@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);}}privatevoidAddPoint(Pointp){track.add(p);}@OverridepublicvoiddoMousePress(Pointp){//TODOAuto-generatedmethodstubAddPoint(p);}@OverridepublicvoiddoMouseDrag(Pointp){//TODOAuto-generatedmethodstubAddPoint(p);}@OverridepublicvoiddoMouseRelease(Pointp){//TODOAuto-generatedmethodstubAddPoint(p);}}Rect.javapackagebase;importimportpublicclassRectextendsGrapicsElem{intx,y,width,height;@Overridepublicvoiddraw(Graphicsg){//TODOAuto-generatedmethodstubg.drawRect(getX(),getY(),getWidth(),getHeight());}@OverridepublicvoiddoMousePress(Pointp){super.setStart(p);super.setEnd(p);}@OverridepublicvoiddoMouseDrag(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;importpublicclassCircleextendsRect{@Overridepublicvoiddraw(Graphicsg){//TODOAuto-generatedmethodstubg.drawOval(getX(),getY(),getWidth(),getHeight());}}windowsPaint.javapackagewinpaint;importbase.Circle;importbase.GrapicsElem;importbase.Line;importbase.Rect;publicclasswindowsPaintextendsJPanelimplementsMouseListener,MouseMotionListener{Stringcommand=矩形;ListGrapicsElemLg=newArrayList();GrapicsElemcur;JLabellabel;publicwindowsPaint(Stringtitle){init();}privatevoidinit(){addMouseListener(this);addMouseMotionListener(this);}/***可以使用反射去掉判断语句、*Class?g=Class.forName(base.Rect);*g.newInstance();*@return*/privateGrapicsElemnewInstance(){command=WinToolBar.Command;if(command.equals(矩形)){returnnewRect();}elseif(command.equals(圆形)){returnnewCircle();}elseif(command.equals(线)){returnnewLine();}returnnull;}@Overridepublicvoidpaint(Graphicsg){//TODOAuto-generatedmethodstubsuper.paint(g);Graphics2Dg2d=(Graphics2D)g;//g2d.setColor(cur.getColor());g2d.setStroke(newBasicStroke(10.0f));for(inti=0;iLg.size();i++){GrapicsElemge=Lg.get(i);ge.draw(g);}}//@Override//publicvoidrepaint(){////TODOAuto-generatedmethodstub////TODOAuto-generatedmethodstub//////super.repaint();////Threadthread=newThread(newRunnable(){////@Override////publicvoidrun(){//////TODOAuto-generatedmethodstub////try{////SaveToFile();////}catch(IOExceptione){//////TODOAuto-generatedcatchblock////e.printStackTrace();////}////}////});////thread.start();////////ImageIconbg=newImageIcon(tmp.jpg);////if(bg!=null){////if(label!=null)////this.remove(label);////label=newJLabel(bg);////label.setBounds(0,0,bg.getIconWidth(),bg.getIconHeight());////this.add(label);////super.repaint();////}//////}privatevoidSaveToFile()throwsIOException{//TODOAuto-generatedmethodstubBufferedImagebimg=newBufferedImage(this.getWidth(),this.getHeight(),BufferedImage.TYPE_3BYTE_BGR);Graphics2Dg2d=bimg.createGraphics();this.paint(g2d);ImageIO.write(bimg,jpg,newFile(tmp.jpg));}@Overridep
本文标题:JAVA实验报告——简单绘图程序
链接地址:https://www.777doc.com/doc-6020310 .html