实验6命令模式
实验
报告
软件系统测试报告下载sgs报告如何下载关于路面塌陷情况报告535n,sgs报告怎么下载竣工报告下载
课程:
设计
领导形象设计圆作业设计ao工艺污水处理厂设计附属工程施工组织设计清扫机器人结构设计
模式实验 学期: 2010-2011学年 第一学期 任课教师: 专业: 软件工程 学号: 姓名: 成绩:
实验6 命令模式
1.题目:
使用命令模式实现在用户界面中,用户选择需要绘制的图形(矩形、三角形、圆形),并在用户界面中绘制该图形,并能够实现撤销多步的功能。
2.模式设计的UML类图:
1
3.程序源代码:
(1)命令接口Command.java:
public interface Command
{
public void execute();
public void undo();
}
public class NoCommand implements Command
{
public 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);
}}
2
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 JPanel
{
private 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);
3
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 JPanel
{
private 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 JPanel
{
private 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)
{
4
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 JPanel
{
private 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 Command {
DrawSquare 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 Command {
DrawRectanqleLong drawRectanqleLong;
public DrawRectanqleLongCommand(DrawRectanqleLong
drawRectanqleLong)
5
{ 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 Command {
DrawRectanqleHeight 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 Command {
DrawTrianqle 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 Command {
DrawRightanqleTrianqle drawRightanqleTrianqle;
public DrawRightanqleTrianqleCommand(DrawRightanqleTrianqle
drawRightanqleTrianqle)
{ this.drawRightanqleTrianqle = drawRightanqleTrianqle; }
public void execute(){
drawRightanqleTrianqle.setVisible(true);
DrawFrame.getJFrame().add(drawRightanqleTrianqle);
}
6
public void undo(){
drawRightanqleTrianqle.setVisible(true);
DrawFrame.getJFrame().add(drawRightanqleTrianqle);
} }
public class DrawIsoscelesTrianqleCommand implements Command {
DrawIsoscelesTrianqle 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 Command {
DrawCircle 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 Command {
DrawEllipse 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 Command {
DrawSolidCircle drawSolidCircle;
7
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)遥控控制类RemoteContro.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 Command[3];
secondCommands = new Command[3];
thirdCommands = new Command[3];
for(int i=0;i<3;i++)
{
firstCommands[i] = noCommand;
secondCommands[i] = noCommand;
thirdCommands[i] = noCommand;
}
}
public void setCommand(int slot,Command firstCommand,Command
secondCommand,Command thirdCommand)
{
firstCommands[slot] = firstCommand;
secondCommands[slot] = secondCommand;
thirdCommands[slot] = thirdCommand;
}
public void firstItemSelected(int slot)
{
firstCommands[slot].execute();
undoCommand.add(firstCommands[slot]);
}
public void secondItemSelected(int slot)
{
secondCommands[slot].execute();
undoCommand.add(secondCommands[slot]);
}
8
public void thirdItemSelected(int slot)
{
thirdCommands[slot].execute();
undoCommand.add(thirdCommands[slot]);}
public void undoButtonWasPushed()
{
if(undoCommand.size()>1){
undoCommand.get(undoCommand.size()-1).undo();
undoCommand.remove(undoCommand.size()-1);}
else
System.out.println("Here is 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("XXXXXXXXXXX");
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("长方形(长<宽)");
trianqleBox.addItem("");trianqleBox.addItem("一般三角形");
trianqleBox.addItem("直角三角形");trianqleBox.addItem("等腰三角形");
circleBox.addItem("");circleBox.addItem("一般圆");
circleBox.addItem("椭圆");circleBox.addItem("实心圆");
northPanel.setLayout(new GridLayout(2,3));
northPanel.add(rectanqleLabel);northPanel.add(trianqleLabel);
northPanel.add(circleLabel); northPanel.add(rectanqleBox);
9
northPanel.add(trianqleBox); northPanel.add(circleBox);
southPanel.add(undoButton);
add(northPanel,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().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(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.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("一般三角形"))
{
10
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);
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(2);
}
else if(((String)circleBox.getSelectedItem()).equals("椭圆")){
11
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(2);
}
else if(((String)circleBox.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(2);
} } });
undoButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
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.undoButtonWasPushed();
} }); }
RemoteControl remoteControl = new RemoteControl();
DrawSquareCommand square = new DrawSquareCommand(DrawSquare.getJPanel());
DrawRectanqleLongCommand rectanqleLong = new
DrawRectanqleLongCommand(DrawRectanqleLong.getJPanel());
DrawRectanqleHeightCommand rectanqleHeight = new
DrawRectanqleHeightCommand(DrawRectanqleHeight.getJPanel());
DrawTrianqleCommand trianql = new
DrawTrianqleCommand(DrawTrianqle.getJPanel());
DrawRightanqleTrianqleCommand rightTrianqle = new
DrawRightanqleTrianqleCommand(DrawRightanqleTrianqle.getJPanel());
DrawIsoscelesTrianqleCommand isoscelesTrianqle = new
DrawIsoscelesTrianqleCommand(DrawIsoscelesTrianqle.getJPanel());
DrawCircleCommand circl = new DrawCircleCommand(DrawCircle.getJPanel()); DrawEllipseCommand ellipse = new DrawEllipseCommand(DrawEllipse.getJPanel()); DrawSolidCircleCommand solidCircle = new
DrawSolidCircleCommand(DrawSolidCircle.getJPanel());
12
private JPanel northPanel;private JPanel southPanel;
private JPanel nullJPanel;private JComboBox rectanqleBox;
private JComboBox trianqleBox;private JComboBox circleBox;
private JButton undoButton;
private static final int DEFAULT_WIDTH = 400;
private static final int DEFAULT_HEIGHT = 300; }
(6)遥控器类RemoteLoader.java:
import javax.swing.JFrame;
public class RemoteLoader {
public static void main(String[] args) {
DrawFrame.getJFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawFrame.getJFrame().setVisible(true);
} }
4.实验结果截图:
13