资源描述:
《设计模式实验6_命令模式》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实验报告课程:设计模式实验学期:2010-2011学年第一学期任课教师:专业:学号:姓名:成绩:实验6命令模式1.题目:使用命令模式实现在用户界面中,用户选择需要绘制的图形(矩形、三角形、圆形),并在用户界面中绘制该图形,并能够实现撤销多步的功能。2.模式设计的UML类图:133.程序源代码:(1)命令接口Command.java:publicinterfaceCommand{publicvoidexecute();publicvoidundo();}publicclassNoCommandimplementsCommand{publicvoidexecute(){}publicvoidun
2、do(){}}(2)绘制图形的各个类:importjava.awt.*;importjava.awt.geom.*;importjavax.swing.*;publicclassDrawSquareextendsJPanel{privatestaticDrawSquarepanel=newDrawSquare();publicstaticDrawSquaregetJPanel(){returnpanel;}publicvoidpaintComponent(Graphicsg){Graphics2Dg2=(Graphics2D)g;super.paintComponent(g);doubler
3、ectX=130;doublerectY=40;//设置坐标的初始值doublewidth=100;doubleheight=100;Rectangle2Drect=newRectangle2D.Double(rectX,rectY,width,height);g2.draw(rect);}}publicclassDrawRectanqleLongextendsJPanel{privatestaticDrawRectanqleLongpanel=newDrawRectanqleLong();publicstaticDrawRectanqleLonggetJPanel(){returnpane
4、l;}publicvoidpaintComponent(Graphicsg){Graphics2Dg2=(Graphics2D)g;super.paintComponent(g);doublerectX=110;doublerectY=40;//设置坐标的初始值doublewidth=150;doubleheight=100;Rectangle2Drect=newRectangle2D.Double(rectX,rectY,width,height);g2.draw(rect);}}13publicclassDrawRectanqleHeightextendsJPanel{privatest
5、aticDrawRectanqleHeightpanel=newDrawRectanqleHeight();publicstaticDrawRectanqleHeightgetJPanel(){returnpanel;}publicvoidpaintComponent(Graphicsg){Graphics2Dg2=(Graphics2D)g;super.paintComponent(g);doublerectX=135;doublerectY=30;//设置坐标的初始值doublewidth=85;doubleheight=130;Rectangle2Drect=newRectangle2
6、D.Double(rectX,rectY,width,height);g2.draw(rect);}}publicclassDrawTrianqleextendsJPanel{privatestaticDrawTrianqlepanel=newDrawTrianqle();publicstaticDrawTrianqlegetJPanel(){returnpanel;}publicvoidpaintComponent(Graphicsg){Graphics2Dg2=(Graphics2D)g;super.paintComponent(g);doublepoint1X=180;doublepo
7、int1Y=40;//设置坐标的初始值doublepoint2X=130;doublepoint2Y=120;doublepoint3X=210;doublepoint3Y=150;Line2Dline1=newLine2D.Double(point1X,point1Y,point2X,point2Y);Line2Dline2=newLine2D.Double(point1X,point1Y,point3X,