1 /**
   2  * CommandPattern example
   3  */
   4 
   5 import java.util.*;
   6 import java.util.Iterator;
   7 import java.awt.*;
   8 import java.awt.event.*;
   9 import javax.swing.*;
  10 
  11 interface Command {
  12     public abstract void execute();
  13 }
  14 
  15 class MacroCommand implements Command {
  16     private Stack commands = new Stack();  // for easy undo use Stack
  17     public void execute() {  // using Recursion
  18         Iterator it = commands.iterator();
  19         while(it.hasNext()) {
  20             ((Command)it.next()).execute();
  21         }
  22     }
  23     public void append(Command cmd) {
  24         if(cmd != this) {
  25             commands.push(cmd);
  26         }
  27     }
  28     public void undo() {
  29         if(!commands.empty()) {
  30             commands.pop();
  31         }
  32     }
  33     public void clear() {
  34         commands.clear();
  35     }
  36 }
  37 
  38 class DrawCommand implements Command {
  39     protected Drawable drawable;
  40     private Point position;
  41     public DrawCommand(Drawable drawable, Point position) {
  42         this.drawable = drawable;
  43         this.position = position;
  44     }
  45     public void execute() {
  46         drawable.draw(position.x, position.y);
  47     }
  48 }
  49 
  50 interface Drawable {
  51     public abstract void draw(int x, int y);
  52 }
  53 
  54 class DrawCanvas extends Canvas implements Drawable {
  55     private Color color = Color.red;
  56     private int radius = 6;
  57     private MacroCommand history;
  58     public DrawCanvas(int width, int height, MacroCommand history) {
  59         setSize(width, height);
  60         setBackground(Color.white);
  61         this.history = history;
  62     }
  63     // entire drawing using MacroCommand
  64     public void paint(Graphics g) {
  65         history.execute();
  66     }
  67     public void draw(int x, int y) {
  68         Graphics g = getGraphics();
  69         g.setColor(color);
  70         g.fillOval(x-radius, y-radius, radius*2, radius*2);
  71     }
  72 }
  73 
  74 public class CommandDrawer extends JFrame implements ActionListener, 
  75         MouseMotionListener, WindowListener {
  76     private MacroCommand history = new MacroCommand();
  77     private DrawCanvas canvas = new DrawCanvas(400,400,history);
  78     private JButton clearButton = new JButton("clear");
  79     public CommandDrawer(String title) {
  80         super(title);
  81         this.addWindowListener(this);
  82         canvas.addMouseMotionListener(this);
  83         clearButton.addActionListener(this);
  84 
  85         Box buttonBox = new Box(BoxLayout.X_AXIS);
  86         buttonBox.add(clearButton);
  87         Box mainBox = new Box(BoxLayout.Y_AXIS);
  88         mainBox.add(buttonBox);
  89         mainBox.add(canvas);
  90         getContentPane().add(mainBox);
  91         pack();
  92         setVisible(true);
  93     }
  94     
  95     // ActionListener implementation
  96     public void actionPerformed(ActionEvent e) {
  97         if(e.getSource() == clearButton) {
  98             history.clear();
  99             canvas.repaint();
 100         }
 101     }
 102     public void mouseMoved(MouseEvent e) {
 103     }
 104     // when mouse event, creat command to draw oval and execute
 105     public void mouseDragged(MouseEvent e) {
 106         Command cmd = new DrawCommand(canvas, e.getPoint());
 107         history.append(cmd);
 108         cmd.execute();
 109     }
 110 
 111     public void windowClosing(WindowEvent e) {
 112         System.exit(0);
 113     }
 114     public void windowActivated(WindowEvent e) {}
 115     public void windowClosed(WindowEvent e) {}
 116     public void windowDeactivated(WindowEvent e) {}
 117     public void windowDeiconified(WindowEvent e) {}
 118     public void windowIconified(WindowEvent e) {}
 119     public void windowOpened(WindowEvent e) {}
 120 
 121     public static void main(String[] args) {
 122         new CommandDrawer("Command Pattern Sample");
 123     }
 124 }
 125 
 126 
 127         
web biohackers.net