云大設(shè)計模式實(shí)驗(yàn)6(命令模式)_第1頁
云大設(shè)計模式實(shí)驗(yàn)6(命令模式)_第2頁
云大設(shè)計模式實(shí)驗(yàn)6(命令模式)_第3頁
云大設(shè)計模式實(shí)驗(yàn)6(命令模式)_第4頁
云大設(shè)計模式實(shí)驗(yàn)6(命令模式)_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、云南大學(xué)軟件學(xué)院 實(shí)驗(yàn)報告課程: 設(shè)計模式 學(xué)期: 2012-2013學(xué)年 第一學(xué)期 任課教師: 陸歌浩 專業(yè): 軟件工程 學(xué)號: 20101120007 姓名: 孫金文 成績: 實(shí)驗(yàn)6 命令模式一、實(shí)驗(yàn)?zāi)康?.熟悉命令模式 2.運(yùn)用命令模式編制簡單的java程序二、實(shí)驗(yàn)內(nèi)容類圖:實(shí)驗(yàn)代碼:package command;import java.awt.Color;import java.awt.Container;import java.awt.Dimension;import java.awt.Frame;import java.awt.Graphics;import java.awt.G

2、ridLayout;import java.awt.Panel;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import java.util.ArrayList;import java.awt.Graphics2D;import java

3、.awt.geom.Ellipse2D;import java.awt.geom.Rectangle2D;import javax.swing.JPanel;public class GraphicsMainpublic static void main(String args) Toolkit kit=Toolkit.getDefaultToolkit();Dimension screenSize=kit.getScreenSize();int screenHeihgt=screenSize.height;int screenWidth=screenSize.width;DrawJFram

4、drawJFram=new DrawJFram();drawJFram.setSize(screenWidth, screenHeihgt);drawJFram.setVisible(true);interface DrawCommand public void execute();public void undo();class EllipseCommand implements DrawCommand Circular circular;public EllipseCommand(Circular c)circular=c;public void execute() circular.dr

5、awEllipse();public void undo() DrawFrame.getJFrame().add(circular);class DrawJFram extends JFrame implements ActionListenerJComboBox comboBox,comboBox2,comboBox3;String str=圓形,橢圓,實(shí)心圓;String str2=方形,正方形,長方形;String str3=三角形,等腰直角三角形,等邊三角形;public DrawJFram ()super( 孫金文-20101120007);this.setDefaultCloseO

6、peration(JFrame.EXIT_ON_CLOSE);setcomputerWindow();private void setcomputerWindow() / TODO Auto-generated method stubContainer ct=this.getContentPane();ct.setLayout(new GridLayout(6,1);Panel p1=new Panel();p1.add(new JLabel(圓形:);comboBox=new JComboBox(str);p1.add(comboBox);p1.add(new JLabel(方形:);com

7、boBox2=new JComboBox(str2);p1.add(comboBox2);p1.add(new JLabel(三角形:);comboBox3=new JComboBox(str3);p1.add(comboBox3);comboBox.addActionListener(this);comboBox2.addActionListener(this);comboBox3.addActionListener(this);ct.add(p1,North);public void actionPerformed(ActionEvent e) if(e.getSource().equal

8、s(comboBox)if(comboBox.getSelectedItem().equals(圓形)Circular circular=new Circular(100, 100);this.add(circular,South);circular.drawCircular();this.setVisible(true);else if(comboBox.getSelectedItem().equals(橢圓)Circular circular=new Circular(30, 70);this.add(circular,South);circular.drawEllipse();this.

9、setVisible(true);else if(comboBox.getSelectedItem().equals(實(shí)心圓)Circular circular=new Circular(50, 50);this.add(circular,South);circular.drawSolidRound();this.setVisible(true);else if(e.getSource().equals(comboBox2)if(comboBox2.getSelectedItem().equals(方形)Rectangle rectangle=new Rectangle(100, 100);t

10、his.add(rectangle,South);rectangle.drawhighRectangle();this.setVisible(true);else if(comboBox2.getSelectedItem().equals(長方形)Rectangle rectangle=new Rectangle(100, 100);this.add(rectangle,South);rectangle.drawlongRectangle();this.setVisible(true);else if(comboBox2.getSelectedItem().equals(正方形)Rectang

11、le rectangle=new Rectangle(100, 100);this.add(rectangle,South);rectangle.drawsquare();this.setVisible(true); else if(e.getSource().equals(comboBox3) if(comboBox3.getSelectedItem().equals(三角形) Triangle triangle=new Triangle(500, 10);this.add(triangle,South);triangle.drawTriangle();this.setVisible(tru

12、e); else if(comboBox3.getSelectedItem().equals(等腰直角三角形) Triangle triangle=new Triangle(500, 30);this.add(triangle,South);triangle.drawIsoscelesTriangle();this.setVisible(true);else if(comboBox3.getSelectedItem().equals(等邊三角形) Triangle triangle=new Triangle(500, 50);this.add(triangle);triangle.draweQ

13、uilateralTriangle();this.setVisible(true);class DrawControl ArrayList dcommand=new ArrayList();public void addCommand(DrawCommand dc)dcommand.add(dc);public void removeCommand(DrawCommand dc)for(int i=0;idcommand.size();i+)if(dcommand.get(i).getClass().equals(getClass()dcommand.remove(i);public void

14、 executeCommand(DrawCommand dc)for(int i=0;i 1) dcommand.get(dcommand.size() - 1).undo();dcommand.remove(dcommand.size() - 1); elseSystem.out.println(Here is end, you cant undo);class CircularCommand implements DrawCommand Circular circular;public CircularCommand(Circular c)circular=c;public void ex

15、ecute() circular.drawCircular();public void undo() / TODO Auto-generated method stubclass Circular extends JPanelEllipse2D e =null;private int w,h; int f;public Circular(int w,int h)this.w=w;this.h=h;/畫圓型public void drawCircular()e=new Ellipse2D.Double(w/2,h/4,50,50);f=1;repaint();/畫橢圓形public void d

16、rawEllipse()e=new Ellipse2D.Double(w/2,h/4,20,70);f=1;repaint();/畫實(shí)心圓public void drawSolidRound()f=2;e=new Ellipse2D.Double(w/2,h/4,50,50);repaint();public void paintComponent(Graphics g)super.paintComponent(g);if(f=1)Graphics2D g2=(Graphics2D) g;g2.draw(e);elseGraphics2D g2=(Graphics2D) g;/g2.draw(

17、e);g2.setPaint(Color.RED);g2.fill(e);class TriangleCommand implements DrawCommandTriangle triangle;public TriangleCommand(Triangle t)triangle=t;public void execute() triangle.drawTriangle();public void undo() / TODO Auto-generated method stubclass Triangle extends JPanelprivate int x;private int y;p

18、rivate int width,height;public Triangle(int w,int h)width=w;height=h;/畫三角形public void drawTriangle()int p=width/2,width/2-100,width/2;x=p;int k=height/4,height/4+90,height/4+50;y=k;repaint();/畫等腰三角形public void drawIsoscelesTriangle()int p=width/2,width/2-100,width/2;x=p;int k=height/4,height/4,heigh

19、t/4+100;y=k;repaint();/畫等邊三角形public void draweQuilateralTriangle()int p=width/2,width/2-100,width/2-50;x=p;int k=height/4,height/4,(int) (height/4+Math.sqrt(Math.pow(100, 2.0)-Math.pow(50, 2.0);y=k;repaint();public void paintComponent(Graphics g)super.paintComponent(g);if(x!=null)Graphics2D g2=(Grap

20、hics2D) g;g2.fillPolygon(x,y,3);class squareCommand implements DrawCommandRectangle rectangle;public squareCommand(Rectangle r)rectangle=r;public void execute() rectangle.drawsquare();public void undo() / TODO Auto-generated method stubclass SolidRoundCommand implements DrawCommand Circular circular

21、;public SolidRoundCommand(Circular c)circular=c;public void execute() circular.drawSolidRound();public void undo() / TODO Auto-generated method stubclass Rectangle extends JPanelRectangle2D rec = null; private int w,h; public Rectangle(int w,int h) this.w=w; this.h=h; /畫正方形 public void drawsquare()

22、rec=new Rectangle2D.Double(w/2, h/4, 50, 50); this.repaint(); /畫長方形 public void drawlongRectangle() rec=new Rectangle2D.Double(w/2, h/4, 100, 30); this.repaint(); /畫方形 public void drawhighRectangle() rec=new Rectangle2D.Double(w/2, h/4, 20, 100); this.repaint(); public void paintComponent(Graphics g)super.paintComponent(g);if(rec!=null)Graphics2D g2=(Graphics2D) g;g2.setPaint(Color.GREEN);g2.fill(rec);class QuilateralTriangleCommand implements DrawCommandTriangle triangle;public QuilateralTriangleCommand(Triangle t)triangle=t;public void execute() triangle.draweQ

溫馨提示

  • 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論