设计模式实验6命令模式.doc_第1页
设计模式实验6命令模式.doc_第2页
设计模式实验6命令模式.doc_第3页
设计模式实验6命令模式.doc_第4页
设计模式实验6命令模式.doc_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验报告课程: 设计模式实验 学期: 2010-2011学年 第一学期 任课教师: 专业: 学号: 姓名: 成绩: 实验6 命令模式1.题目: 使用命令模式实现在用户界面中,用户选择需要绘制的图形(矩形、三角形、圆形),并在用户界面中绘制该图形,并能够实现撤销多步的功能。2.模式设计的UML类图:3.程序源代码:(1)命令接口Command.java:public interface Command public void execute();public void undo();public class NoCommand implements Commandpublic void execute()public void undo() (2)绘制图形的各个类:import java.awt.*;import java.awt.geom.*;import javax.swing.*;public class DrawSquare extends JPanel private static DrawSquare panel=new DrawSquare(); public static DrawSquare getJPanel() return panel; public void paintComponent(Graphics g)Graphics2D g2 = (Graphics2D)g;super.paintComponent(g);double rectX =130;double rectY = 40; /设置坐标的初始值double width = 100;double height = 100;Rectangle2D rect = new Rectangle2D.Double(rectX,rectY,width,height);g2.draw(rect); public class DrawRectanqleLong extends JPanel private static DrawRectanqleLong panel=new DrawRectanqleLong();public static DrawRectanqleLong getJPanel() return panel; public void paintComponent(Graphics g)Graphics2D g2 = (Graphics2D)g;super.paintComponent(g);double rectX = 110;double rectY = 40; /设置坐标的初始值double width = 150;double height = 100;Rectangle2D rect = new Rectangle2D.Double(rectX,rectY,width,height);g2.draw(rect); public class DrawRectanqleHeight extends JPanel private static DrawRectanqleHeight panel=new DrawRectanqleHeight();public static DrawRectanqleHeight getJPanel() return panel; public void paintComponent(Graphics g)Graphics2D g2 = (Graphics2D)g;super.paintComponent(g);double rectX = 135;double rectY = 30; /设置坐标的初始值double width = 85;double height = 130;Rectangle2D rect = new Rectangle2D.Double(rectX,rectY,width,height);g2.draw(rect); public class DrawTrianqle extends JPanel private static DrawTrianqle panel=new DrawTrianqle();public static DrawTrianqle getJPanel()return panel; public void paintComponent(Graphics g)Graphics2D g2 = (Graphics2D)g;super.paintComponent(g);double point1X =180;double point1Y = 40; /设置坐标的初始值double point2X =130;double point2Y = 120;double point3X =210;double point3Y = 150;Line2D line1 = new Line2D.Double(point1X,point1Y,point2X,point2Y);Line2D line2 = new Line2D.Double(point1X,point1Y,point3X,point3Y);Line2D line3 = new Line2D.Double(point2X,point2Y,point3X,point3Y);g2.draw(line1); g2.draw(line2); g2.draw(line3); public class DrawRightanqleTrianqle extends JPanelprivate static DrawRightanqleTrianqle panel=new DrawRightanqleTrianqle();public static DrawRightanqleTrianqle getJPanel() return panel; public void paintComponent(Graphics g)Graphics2D g2 = (Graphics2D)g;super.paintComponent(g);double point1X =180;double point1Y = 20; /设置坐标的初始值double point2X =130;double point2Y = 150;double point3X =230;double point3Y = 150;Line2D line1 = new Line2D.Double(point1X,point1Y,point2X,point2Y);Line2D line2 = new Line2D.Double(point1X,point1Y,point3X,point3Y);Line2D line3 = new Line2D.Double(point2X,point2Y,point3X,point3Y);g2.draw(line1); g2.draw(line2); g2.draw(line3); public class DrawIsoscelesTrianqle extends JPanelprivate static DrawIsoscelesTrianqle panel=new DrawIsoscelesTrianqle();public static DrawIsoscelesTrianqle getJPanel() return panel; public void paintComponent(Graphics g)Graphics2D g2 = (Graphics2D)g;super.paintComponent(g);double point1X =150;double point1Y = 20; /设置坐标的初始值double point2X =150;double point2Y = 150;double point3X =250;double point3Y = 150;Line2D line1 = new Line2D.Double(point1X,point1Y,point2X,point2Y);Line2D line2 = new Line2D.Double(point1X,point1Y,point3X,point3Y);Line2D line3 = new Line2D.Double(point2X,point2Y,point3X,point3Y);g2.draw(line1); g2.draw(line2); g2.draw(line3); public class DrawCircle extends JPanelprivate static DrawCircle panel=new DrawCircle();public static DrawCircle getJPanel() return panel; public void paintComponent(Graphics g)Graphics2D g2 = (Graphics2D)g;super.paintComponent(g);double circleX = 180;double circleY = 90;double radius = 50; /设置坐标的初始值Ellipse2D circle = new Ellipse2D.Double();circle.setFrameFromCenter(circleX, circleY,circleX+radius,circleY+radius);g2.draw(circle); /画圆public class DrawEllipse extends JPanel private static DrawEllipse panel=new DrawEllipse();public static DrawEllipse getJPanel()return panel; public void paintComponent(Graphics g)Graphics2D g2 = (Graphics2D)g;super.paintComponent(g);double ellipseX = 115;double ellipseY = 30; /设置坐标的初始值double width = 150;double height = 100;Rectangle2D rect = new Rectangle2D.Double(ellipseX,ellipseY,width,height);Ellipse2D ellipse = new Ellipse2D.Double();ellipse.setFrame(rect);g2.draw(ellipse); public class DrawSolidCircle extends JPanelprivate static DrawSolidCircle panel=new DrawSolidCircle();public static DrawSolidCircle getJPanel()return panel; public void paintComponent(Graphics g)Graphics2D g2 = (Graphics2D)g;super.paintComponent(g);double circleX = 180;double circleY = 90;double radius = 50; /设置坐标的初始值Ellipse2D circle = new Ellipse2D.Double();circle.setFrameFromCenter(circleX, circleY,circleX+radius,circleY+radius);g2.setPaint(Color.BLUE); /设置圆的颜色g2.fill(circle);g2.draw(circle); /画圆(3)实现各种命令的类:public class DrawSquareCommand implements CommandDrawSquare drawSquare;public DrawSquareCommand(DrawSquare drawSquare) this.drawSquare = drawSquare; public void execute()drawSquare.setVisible(true);DrawFrame.getJFrame().add(drawSquare);public void undo()drawSquare.setVisible(true);DrawFrame.getJFrame().add(drawSquare); public class DrawRectanqleLongCommand implements CommandDrawRectanqleLong drawRectanqleLong;public DrawRectanqleLongCommand(DrawRectanqleLong drawRectanqleLong) this.drawRectanqleLong = drawRectanqleLong;public void execute()drawRectanqleLong.setVisible(true);DrawFrame.getJFrame().add(drawRectanqleLong);public void undo()drawRectanqleLong.setVisible(true);DrawFrame.getJFrame().add(drawRectanqleLong); public class DrawRectanqleHeightCommand implements CommandDrawRectanqleHeight drawRectanqleHeight;public DrawRectanqleHeightCommand(DrawRectanqleHeight drawRectanqleHeight)this.drawRectanqleHeight = drawRectanqleHeight; public void execute()drawRectanqleHeight.setVisible(true);DrawFrame.getJFrame().add(drawRectanqleHeight);public void undo()drawRectanqleHeight.setVisible(true);DrawFrame.getJFrame().add(drawRectanqleHeight); public class DrawTrianqleCommand implements CommandDrawTrianqle drawTrianqle;public DrawTrianqleCommand(DrawTrianqle drawTrianqle) this.drawTrianqle = drawTrianqle; public void execute()drawTrianqle.setVisible(true);DrawFrame.getJFrame().add(drawTrianqle);public void undo()drawTrianqle.setVisible(true);DrawFrame.getJFrame().add(drawTrianqle); public class DrawRightanqleTrianqleCommand implements CommandDrawRightanqleTrianqle drawRightanqleTrianqle;public DrawRightanqleTrianqleCommand(DrawRightanqleTrianqle drawRightanqleTrianqle)this.drawRightanqleTrianqle = drawRightanqleTrianqle;public void execute()drawRightanqleTrianqle.setVisible(true);DrawFrame.getJFrame().add(drawRightanqleTrianqle);public void undo()drawRightanqleTrianqle.setVisible(true);DrawFrame.getJFrame().add(drawRightanqleTrianqle); public class DrawIsoscelesTrianqleCommand implements CommandDrawIsoscelesTrianqle drawIsoscelesTrianqle;public DrawIsoscelesTrianqleCommand(DrawIsoscelesTrianqle drawIsoscelesTrianqle)this.drawIsoscelesTrianqle = drawIsoscelesTrianqle;public void execute()drawIsoscelesTrianqle.setVisible(true);DrawFrame.getJFrame().add(drawIsoscelesTrianqle);public void undo()drawIsoscelesTrianqle.setVisible(true);DrawFrame.getJFrame().add(drawIsoscelesTrianqle); public class DrawCircleCommand implements CommandDrawCircle drawCircle;public DrawCircleCommand(DrawCircle drawCircle)this.drawCircle = drawCircle;public void execute()drawCircle.setVisible(true);DrawFrame.getJFrame().add(drawCircle);public void undo()drawCircle.setVisible(true);DrawFrame.getJFrame().add(drawCircle); public class DrawEllipseCommand implements CommandDrawEllipse drawEllipse;public DrawEllipseCommand(DrawEllipse drawEllipse)this.drawEllipse = drawEllipse;public void execute()drawEllipse.setVisible(true);DrawFrame.getJFrame().add(drawEllipse);public void undo()drawEllipse.setVisible(true);DrawFrame.getJFrame().add(drawEllipse); public class DrawSolidCircleCommand implements CommandDrawSolidCircle drawSolidCircle;public DrawSolidCircleCommand(DrawSolidCircle drawSolidCircle)this.drawSolidCircle = drawSolidCircle;public void execute()drawSolidCircle.setVisible(true);DrawFrame.getJFrame().add(drawSolidCircle);public void undo()drawSolidCircle.setVisible(true);DrawFrame.getJFrame().add(drawSolidCircle); (4)遥控控制类RemoteControl.java:import java.util.ArrayList;public class RemoteControl Command firstCommands;Command secondCommands;Command thirdCommands;ArrayList undoCommand =new ArrayList();Command noCommand=new NoCommand();public RemoteControl()firstCommands = new Command3;secondCommands = new Command3;thirdCommands = new Command3;for(int i=0;i1)undoCommand.get(undoCommand.size()-1).undo();undoCommand.remove(undoCommand.size()-1);elseSystem.out.println(Here is end, you cant undo);(5)用户界面类DrawFrame.java:import java.awt.*;import javax.swing.*;import java.awt.event.*;public class DrawFrame extends JFrame private static DrawFrame frame = new DrawFrame();public static DrawFrame getJFrame()return frame; public DrawFrame() super(XXXXX); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); initComponents(); private void initComponents() remoteControl.setCommand(0, square, rectanqleLong, rectanqleHeight);remoteControl.setCommand(1, trianql, rightTrianqle, isoscelesTrianqle);remoteControl.setCommand(2, circl, ellipse, solidCircle);northPanel = new JPanel();southPanel = new JPanel();nullJPanel = new JPanel(); JLabel rectanqleLabel = new JLabel(矩形 ,SwingConstants.LEFT);JLabel trianqleLabel = new JLabel(三角形,SwingConstants.CENTER);JLabel circleLabel = new JLabel(圆形,SwingConstants.RIGHT); rectanqleBox = new JComboBox(); trianqleBox = new JComboBox(); circleBox = new JComboBox(); undoButton = new JButton(撤销); rectanqleBox.addItem(); rectanqleBox.addItem(正方形);rectanqleBox.addItem(长方形(长宽)); rectanqleBox.addItem(长方形(长宽)) nullJPanel.setVisible(false); DrawSquare.getJPanel().setVisible(false); DrawRectanqleLong.getJPanel().setVisible(false); DrawRectanqleHeight.getJPanel().setVisible(false); DrawTrianqle.getJPanel().setVisible(false); DrawRightanqleTrianqle.getJPanel().setVisible(false); DrawIsoscelesTrianqle.getJPanel().setVisible(false); DrawCircle.getJPanel().setVisible(false); DrawEllipse.getJPanel().setVisible(false); DrawSolidCircle.getJPanel().setVisible(false); remoteControl.secondItemSelected(0); else if(String)rectanqleBox.getSelectedItem().equals(长方形(长宽)) nullJPanel.setVisible(false); DrawSquare.getJPanel().setVisible(false); DrawRectanqleLong.getJPanel().setVisible(false); DrawRectanqleHeight.getJPanel().setVisible(false); DrawTrianqle.getJPanel().setVisible(false); DrawRightanqleTrianqle.getJPanel().setVisible(false); DrawIsoscelesTrianqle.getJPanel().setVisible(false); DrawCircle.getJPanel().setVisible(false); DrawEllipse.getJPanel().setVisible(false); DrawSolidCircle.getJPanel().setVisible(false); remoteControl.thirdItemSelected(0); ); trianqleBox.addActionListener(new ActionListener() /事件处理 public void actionPerformed(ActionEvent event) if(String)trianqleBox.getSelectedItem().equals(一般三角形) nullJPanel.setVisible(false); DrawSquare.getJPanel().setVisible(false); DrawRectanqleLong.getJPanel().setVisible(false); DrawRectanqleHeight.getJPanel().setVisible(false); DrawTrianqle.getJPanel().setVisible(false); DrawRightanqleTrianqle.getJPanel().setVisible(false); DrawIsoscelesTrianqle.getJPanel().setVisible(false); DrawCircle.getJPanel().setVisible(false); DrawEllipse.getJPanel().setVisible(false); DrawSolidCircle.getJPanel().setVisible(false); remoteControl.firstItemSelected(1); else if(String)trianqleBox.getSelectedItem().equals(直角三角形) nullJPanel.setVisible(false); DrawSquare.getJPanel().setVisible(false); DrawRectanqleLong.getJPanel().setVisible(false); DrawRectanqleHeight.getJPanel().setVisible(false); DrawTrianqle.getJPanel().setVisible(false); DrawRightanqleTrianqle.getJPanel().setVisible(false); DrawIsoscelesTrianqle.getJPanel().setVisible(false); DrawCircle.getJPanel().setVisible(false); DrawEllipse.getJPanel().setVisible(false); DrawSolidCircle.getJPanel().setVisible(false); remoteControl.secondItemSelected(1); else if(String)trianqleBox.getSelectedItem().equals(等腰三角形) nullJPanel.setVisible(false); DrawSquare.getJPanel().setVisible(false); DrawRectanqleLong.getJPanel().setVisible(false); DrawRectanqleHeight.getJPanel().setVisible(false); DrawTrianqle.getJPanel().setVisible(false); DrawRightanqleTrianqle.getJPanel().setVisible(false); DrawIsoscelesTrianqle.getJPanel().setVisible(false); DrawCircle.getJPanel().setVisible(false); DrawEllipse.getJPanel().setVisible(false); DrawSolidCircle.getJPanel().setVisible(false); remoteControl.thirdItemSelected(1); ); circleBox.addActionListener(new ActionListener() /事件处理 public void actionPerformed(ActionEvent event) if(String)circleBox.getSelectedItem().equals(一般圆) nullJPanel.setVisible(false); DrawSquare.getJPanel().setVisible(false); DrawRectanqleLong.getJPanel().setVisible(false); Draw

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论