云南大学设计模式实验报告4-命令模式

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

云南大学软件学院实验报告序号:姓名:学号:专业:软件工程日期:2015.12.21成绩:实验四命令模式的运用一、实验目的:命令模式将“请求”封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象,命令模式也支持可撤销的操作。在熟悉命令模式相关理论知识的基础上,使用命令模式实现图片处理程序。二、实验要求:使用命令模式实现图片处理程序,要求如下:1.图片处理程序要有3张图片。2.每张图片至少有3种操作。3.实现类似遥控器的菜单,动态的选择对图片进行的处理。4.要有“撤消操作”,撤消操作要可以撤销至最后一步。1、设计并绘制该程序的类图;2、依照设计的类图使用Java语言编写代码,并实现该程序;3、除了核心的模式相关类实现外,提供测试环境,按照难度高低,分别是:a)控制台程序,Client硬编码初始化模式和测试环境,运行结果文本输出;b)控制台程序,Client初始化测试环境,并根据用户输入运算,运行结果文本输出;c)设计并实现用户UI,Client初始化测试环境,并根据用户在UI控件上的输入运算,运行结果文本输出;三、实验内容:实验类图:实验代码:1DrawControl类publicclassDrawControl{ArrayListDrawCommanddcommand=newArrayListDrawCommand();publicvoidaddCommand(DrawCommanddc){dcommand.add(dc);}publicvoidremoveCommand(DrawCommanddc){for(inti=0;idcommand.size();i++)if(dcommand.get(i).getClass().equals(getClass())){dcommand.remove(i);}}publicvoidexecuteCommand(DrawCommanddc){for(inti=0;idcommand.size();i++)if(dcommand.get(i).getClass().equals(getClass())){dcommand.get(i).execute();}}publicvoidundoCommand(DrawCommanddc){if(dcommand.size()1){dcommand.get(dcommand.size()-1).undo();dcommand.remove(dcommand.size()-1);}elseSystem.out.println(Hereisend,youcan'tundo);}}2DrawCommand接口类packagedesign4;interfaceDrawCommand{publicvoidexecute();publicvoidundo();}3Circular类publicclassCircularextendsJPanel{Ellipse2De=null;privateintw,h;intf;publicCircular(intw,inth){this.w=w;this.h=h;}//画圆型publicvoiddrawCircular(){e=newEllipse2D.Double(w/2,h/4,50,50);f=1;repaint();}//画椭圆形publicvoiddrawEllipse(){e=newEllipse2D.Double(w/2,h/4,20,70);f=1;repaint();}//画实心圆publicvoiddrawSolidRound(){f=2;e=newEllipse2D.Double(w/2,h/4,50,50);repaint();}publicvoidpaintComponent(Graphicsg){super.paintComponent(g);if(f==1){Graphics2Dg2=(Graphics2D)g;g2.draw(e);}else{Graphics2Dg2=(Graphics2D)g;//g2.draw(e);g2.setPaint(Color.RED);g2.fill(e);}}}4CircularCommand类packagedesign4;publicclassCircularCommandimplementsDrawCommand{Circularcircular;publicCircularCommand(Circularc){circular=c;}publicvoidexecute(){circular.drawCircular();}publicvoidundo(){//TODOAuto-generatedmethodstub}}5DrawJFrampackagedesign4;importjava.awt.Container;importjava.awt.GridLayout;importjava.awt.Panel;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JComboBox;importjavax.swing.JFrame;importjavax.swing.JLabel;publicclassDrawJFramextendsJFrameimplementsActionListener{JComboBoxcomboBox,comboBox2,comboBox3;String[]str={圆形,椭圆,实心圆};String[]str2={方形,正方形,长方形};String[]str3={三角形,等腰直角三角形,等边三角形};publicDrawJFram(){super();this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setcomputerWindow();}privatevoidsetcomputerWindow(){//TODOAuto-generatedmethodstubContainerct=this.getContentPane();Containerct=this.getContentPane();ct.setLayout(newGridLayout(6,1));Panelp1=newPanel();p1.add(newJLabel(圆形:));comboBox=newJComboBox(str);p1.add(comboBox);p1.add(newJLabel(方形:));comboBox2=newJComboBox(str2);p1.add(comboBox2);p1.add(newJLabel(三角形:));comboBox3=newJComboBox(str3);p1.add(comboBox3);comboBox.addActionListener(this);comboBox2.addActionListener(this);comboBox3.addActionListener(this);ct.add(p1,North);}publicvoidactionPerformed(ActionEvente){if(e.getSource().equals(comboBox)){if(comboBox.getSelectedItem().equals(圆形)){Circularcircular=newCircular(100,100);this.add(circular,South);circular.drawCircular();this.setVisible(true);}elseif(comboBox.getSelectedItem().equals(椭圆)){Circularcircular=newCircular(30,70);this.add(circular,South);circular.drawEllipse();this.setVisible(true);}elseif(comboBox.getSelectedItem().equals(实心圆)){Circularcircular=newCircular(50,50);this.add(circular,South);circular.drawSolidRound();this.setVisible(true);}}elseif(e.getSource().equals(comboBox2)){if(comboBox2.getSelectedItem().equals(方形)){Rectanglerectangle=newRectangle(100,100);this.add(rectangle,South);rectangle.drawhighRectangle();this.setVisible(true);}elseif(comboBox2.getSelectedItem().equals(长方形)){Rectanglerectangle=newRectangle(100,100);this.add(rectangle,South);rectangle.drawlongRectangle();this.setVisible(true);}elseif(comboBox2.getSelectedItem().equals(正方形)){Rectanglerectangle=newRectangle(100,100);this.add(rectangle,South);rectangle.drawsquare();this.setVisible(true);}}elseif(e.getSource().equals(comboBox3)){if(comboBox3.getSelectedItem().equals(三角形)){Triangletriangle=newTriangle(500,10);this.add(triangle,South);triangle.drawTriangle();this.setVisible(true);}elseif(comboBox3.getSelectedItem().equals(等腰直角三角形)){Triangletriangle=newTriangle(500,30);this.add(triangle,South);triangle.drawIsoscelesTriangle();this.setVisible(true);}elseif(comboBox3.getSelectedItem().equals(等边三角形)){Triangletriangle=newTriangle(500,50);this.add(triangle);triangle.draweQuilateralTriangle();this.setVisible(true);}}}}6pictureMainpackagedesign4;importjava.awt.Dimension;importjava.awt.Toolkit;publicclasspictureMain{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubToolkitkit=Toolkit.getDefaultToolkit();DimensionscreenSize=kit.getScreenSize();intscreenHeihgt=screenSize.height;intscreenWidth=screenSize.width;DrawJFramdrawJFram=newDrawJFram();drawJFram.setSize(screenWidth,scr

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功