




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、實驗報告課程: 設(shè)計模式實驗 學(xué)期: 2010-2011學(xué)年 第一學(xué)期 任課教師: 專業(yè): 學(xué)號: 姓名: 成績: 實驗6 命令模式1.題目: 使用命令模式實現(xiàn)在用戶界面中,用戶選擇需要繪制的圖形(矩形、三角形、圓形),并在用戶界面中繪制該圖形,并能夠?qū)崿F(xiàn)撤銷多步的功能。2.模式設(shè)計的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; /設(shè)置坐標(biāo)的初始值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; /設(shè)置坐標(biāo)的初始值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; /設(shè)置坐標(biāo)的初始值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; /設(shè)置坐標(biāo)的初始值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; /設(shè)置坐標(biāo)的初始值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; /設(shè)置坐標(biāo)的初始值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; /設(shè)置坐標(biāo)的初始值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; /設(shè)置坐標(biāo)的初始值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; /設(shè)置坐標(biāo)的初始值Ellipse2D circle = new Ellipse2D.Double();circle.setFrameFromCenter(circleX, circleY,circleX+radius,circleY+radius);g2.setPaint(Color.BLUE); /設(shè)置圓的顏色g2.fill(circle);g2.draw(circle); /畫圓(3)實現(xiàn)各種命令的類: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 undoCommand =new ArrayList();Command noCommand=new NoCommand();public RemoteContr
30、ol()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.sw
31、ing.*;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_
32、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 JPane
33、l();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()
34、; undoButton = new JButton(撤銷); rectanqleBox.addItem(); rectanqleBox.addItem(正方形);rectanqleBox.addItem(長方形(長寬)); rectanqleBox.addItem(長方形(長寬)) nullJPanel.setVisible(false); DrawSquare.getJPanel().setVisible(false); DrawRectanqleLong.getJPanel().setVisible(false); DrawRectanqleHeight.getJPanel().setV
35、isible(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); re
36、moteControl.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().setVis
37、ible(false); DrawRightanqleTrianqle.getJPanel().setVisible(false); DrawIsoscelesTrianqle.getJPanel().setVisible(false); DrawCircle.getJPanel().setVisible(false); DrawEllipse.getJPanel().setVisible(false); DrawSolidCircle.getJPanel().setVisible(false); remoteControl.thirdItemSelected(0); ); trianqleB
38、ox.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(
39、).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(fals
40、e); 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().setV
41、isible(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(S
42、tring)trianqleBox.getSelectedItem().equals(等腰三角形) nullJPanel.setVisible(false); DrawSquare.getJPanel().setVisible(false); DrawRectanqleLong.getJPanel().setVisible(false); DrawRectanqleHeight.getJPanel().setVisible(false); DrawTrianqle.getJPanel().setVisible(false); DrawRightanqleTrianqle.getJPanel()
43、.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() /事件處理 p
44、ublic 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().setVisi
45、ble(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(Strin
46、g)circleBox.getSelectedItem().equals(橢圓) nullJPanel.setVisible(false); DrawSquare.getJPanel().setVisible(false); DrawRectanqleLong.getJPanel().setVisible(false); DrawRectanqleHeight.getJPanel().setVisible(false); DrawTrianqle.getJPanel().setVisible(false); DrawRightanqleTrianqle.getJPanel().setVisib
47、le(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.s
48、etVisible(false); DrawSquare.getJPanel().setVisible(false); DrawRectanqleLong.getJPanel().setVisible(false); DrawRectanqleHeight.getJPanel().setVisible(false); DrawTrianqle.getJPanel().setVisible(false); DrawRightanqleTrianqle.getJPanel().setVisible(false); DrawIsoscelesTrianqle.getJPanel().setVisib
49、le(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); DrawRightan
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030年中國熱敏打印頭驅(qū)動器數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國熱熔不織布數(shù)據(jù)監(jiān)測研究報告
- 重大投資合同范本
- 2025至2030年中國液晶可視對講門鈴數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國液壓縮管機數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年中國汽缸墊片數(shù)據(jù)監(jiān)測研究報告
- 大型農(nóng)場土地流轉(zhuǎn)合同
- 公務(wù)員勞動合同樣本
- 2025至2030年中國樹脂服裝襯數(shù)據(jù)監(jiān)測研究報告
- 供應(yīng)鏈資金管理協(xié)議范本
- 2024屆湖南省長沙市湖南師大附中等校高三上學(xué)期月考(二)語文試題(解析版)
- 中國變應(yīng)性鼻炎診斷和治療指南(2022版)解讀
- 上??萍及嫘W(xué)二年級下冊綜合實踐活動全冊教案
- 饅頭制作過程
- 大數(shù)據(jù)與能源經(jīng)濟分析
- 《高鐵乘務(wù)安全管理及應(yīng)急處置》課程教案-崔藝琳編寫
- 小班數(shù)學(xué)課件《認(rèn)識長方形》
- 淺談第三方物流的倉儲管理
- 21ZJ111 變形縫建筑構(gòu)造
- 醫(yī)院各委員會職責(zé)制度
- 心理健康與職業(yè)生涯(中職)PPT完整全套教學(xué)課件
評論
0/150
提交評論