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

下载本文档

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

文档简介

1、实验报告课程: 设计模式实验 学期: 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 exec

2、ute()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 = (Graphic

3、s2D)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 DrawRectanqle

4、Long();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,wid

5、th,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

6、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

7、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);Lin

8、e2D 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();

9、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

10、= 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 Dra

11、wIsoscelesTrianqle 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;do

12、uble 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 Dr

13、awCircle 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 circl

14、e = 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

15、)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(ellip

16、se); 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;doubl

17、e 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 Dr

18、awSquareCommand(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 CommandDra

19、wRectanqleLong drawRectanqleLong;public DrawRectanqleLongCommand(DrawRectanqleLong drawRectanqleLong) this.drawRectanqleLong = drawRectanqleLong;public void execute()drawRectanqleLong.setVisible(true);DrawFrame.getJFrame().add(drawRectanqleLong);public void undo()drawRectanqleLong.setVisible(true);D

20、rawFrame.getJFrame().add(drawRectanqleLong); public class DrawRectanqleHeightCommand implements CommandDrawRectanqleHeight drawRectanqleHeight;public DrawRectanqleHeightCommand(DrawRectanqleHeight drawRectanqleHeight)this.drawRectanqleHeight = drawRectanqleHeight; public void execute()drawRectanqleH

21、eight.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 drawTrianq

22、le) 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 CommandDrawRightanqleTrianql

23、e drawRightanqleTrianqle;public DrawRightanqleTrianqleCommand(DrawRightanqleTrianqle drawRightanqleTrianqle)this.drawRightanqleTrianqle = drawRightanqleTrianqle;public void execute()drawRightanqleTrianqle.setVisible(true);DrawFrame.getJFrame().add(drawRightanqleTrianqle);public void undo()drawRighta

24、nqleTrianqle.setVisible(true);DrawFrame.getJFrame().add(drawRightanqleTrianqle); public class DrawIsoscelesTrianqleCommand implements CommandDrawIsoscelesTrianqle drawIsoscelesTrianqle;public DrawIsoscelesTrianqleCommand(DrawIsoscelesTrianqle drawIsoscelesTrianqle)this.drawIsoscelesTrianqle = drawIs

25、oscelesTrianqle;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 drawCirc

26、le;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 Comman

27、dDrawEllipse 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 DrawS

28、olidCircleCommand implements CommandDrawSolidCircle drawSolidCircle;public DrawSolidCircleCommand(DrawSolidCircle drawSolidCircle)this.drawSolidCircle = drawSolidCircle;public void execute()drawSolidCircle.setVisible(true);DrawFrame.getJFrame().add(drawSolidCircle);public void undo()drawSolidCircle.

29、setVisible(true);DrawFrame.getJFrame().add(drawSolidCircle); (4)遥控控制类RemoteControl.java:import java.util.ArrayList;public class RemoteControl Command firstCommands;Command secondCommands;Command thirdCommands;ArrayList<Command> undoCommand =new ArrayList();Command noCommand=new NoCommand();pub

30、lic RemoteControl()firstCommands = new Command3;secondCommands = new Command3;thirdCommands = new Command3;for(int i=0;i<3;i+)firstCommandsi = noCommand;secondCommandsi = noCommand;thirdCommandsi = noCommand;public void setCommand(int slot,Command firstCommand,Command secondCommand,Command thirdC

31、ommand)firstCommandsslot = firstCommand;secondCommandsslot = secondCommand;thirdCommandsslot = thirdCommand;public void firstItemSelected(int slot)firstCommandsslot.execute();undoCommand.add(firstCommandsslot);public void secondItemSelected(int slot)secondCommandsslot.execute();undoCommand.add(secon

32、dCommandsslot);public void thirdItemSelected(int slot)thirdCommandsslot.execute();undoCommand.add(thirdCommandsslot);public void undoButtonWasPushed()if(undoCommand.size()>1)undoCommand.get(undoCommand.size()-1).undo();undoCommand.remove(undoCommand.size()-1);elseSystem.out.println("Here is

33、end, you can't 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"); se

34、tSize(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);remote

35、Control.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

36、JLabel("圆形",SwingConstants.RIGHT); rectanqleBox = new JComboBox(); trianqleBox = new JComboBox(); circleBox = new JComboBox(); undoButton = new JButton("撤销"); rectanqleBox.addItem(""); rectanqleBox.addItem("正方形");rectanqleBox.addItem("长方形(长>宽)");

37、rectanqleBox.addItem("长方形(长<宽)"); trianqleBox.addItem("");trianqleBox.addItem("一般三角形"); trianqleBox.addItem("直角三角形");trianqleBox.addItem("等腰三角形"); circleBox.addItem("");circleBox.addItem("一般圆"); circleBox.addItem("椭圆"

38、);circleBox.addItem("实心圆"); northPanel.setLayout(new GridLayout(2,3); northPanel.add(rectanqleLabel);northPanel.add(trianqleLabel); northPanel.add(circleLabel); northPanel.add(rectanqleBox); northPanel.add(trianqleBox); northPanel.add(circleBox); southPanel.add(undoButton); add(northPanel,

39、BorderLayout.NORTH); add(southPanel,BorderLayout.SOUTH); add(nullJPanel); rectanqleBox.addActionListener(new ActionListener() /事件处理 public void actionPerformed(ActionEvent event) if(String)rectanqleBox.getSelectedItem().equals("正方形") nullJPanel.setVisible(false); DrawSquare.getJPanel().set

40、Visible(false); DrawRectanqleLong.getJPanel().setVisible(false); DrawRectanqleHeight.getJPanel().setVisible(false); DrawTrianqle.getJPanel().setVisible(false); DrawRightanqleTrianqle.getJPanel().setVisible(false); DrawIsoscelesTrianqle.getJPanel().setVisible(false); DrawCircle.getJPanel().setVisible

41、(false); DrawEllipse.getJPanel().setVisible(false); DrawSolidCircle.getJPanel().setVisible(false); remoteControl.firstItemSelected(0); else if(String)rectanqleBox.getSelectedItem().equals("长方形(长>宽)") nullJPanel.setVisible(false); DrawSquare.getJPanel().setVisible(false); DrawRectanqleLo

42、ng.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(

43、).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(fals

44、e); DrawRectanqleHeight.getJPanel().setVisible(false); DrawTrianqle.getJPanel().setVisible(false); DrawRightanqleTrianqle.getJPanel().setVisible(false); DrawIsoscelesTrianqle.getJPanel().setVisible(false); DrawCircle.getJPanel().setVisible(false); DrawEllipse.getJPanel().setVisible(false); DrawSolid

45、Circle.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().

46、setVisible(false); DrawRectanqleLong.getJPanel().setVisible(false); DrawRectanqleHeight.getJPanel().setVisible(false); DrawTrianqle.getJPanel().setVisible(false); DrawRightanqleTrianqle.getJPanel().setVisible(false); DrawIsoscelesTrianqle.getJPanel().setVisible(false); DrawCircle.getJPanel().setVisi

47、ble(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.g

48、etJPanel().setVisible(false); DrawRectanqleHeight.getJPanel().setVisible(false); DrawTrianqle.getJPanel().setVisible(false); DrawRightanqleTrianqle.getJPanel().setVisible(false); DrawIsoscelesTrianqle.getJPanel().setVisible(false); DrawCircle.getJPanel().setVisible(false); DrawEllipse.getJPanel().se

49、tVisible(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); DrawRec

50、tanqleHeight.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(Acti

温馨提示

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

评论

0/150

提交评论