PenSimulator.java

00001 package edu.stanford.hci.r3.pen;
00002 
00003 import java.awt.Dimension;
00004 import java.awt.event.MouseEvent;
00005 import java.awt.event.MouseListener;
00006 import java.awt.event.MouseMotionListener;
00007 import java.util.ArrayList;
00008 import java.util.List;
00009 
00010 import javax.swing.JFrame;
00011 import javax.swing.SwingUtilities;
00012 
00013 import edu.stanford.hci.r3.components.InkPanel;
00014 import edu.stanford.hci.r3.pen.ink.Ink;
00015 import edu.stanford.hci.r3.pen.ink.InkSimplification;
00016 import edu.stanford.hci.r3.pen.ink.InkStroke;
00017 import edu.stanford.hci.r3.pen.streaming.listeners.PenListener;
00018 import edu.stanford.hci.r3.util.WindowUtils;
00019 
00038 public class PenSimulator extends PenInput implements MouseListener, MouseMotionListener {
00039 
00040         private InkStroke currentStroke;
00041         private JFrame frame;
00042         private Ink ink;
00043 
00044         private InkPanel inputPanel;
00045         private List<PenListener> listeners = new ArrayList<PenListener>();
00046 
00047         private InkSimplification simplifier = new InkSimplification();
00048 
00049         public PenSimulator() {
00050                 super("Pen Simulator");
00051         }
00052 
00053         public void addLivePenListener(PenListener penListener) {
00054                 // we don't need to call the super's method, because we just keep our own list of penListeners...
00055                 listeners.add(penListener);
00056         }
00057 
00058         private InkPanel getInputPanel() {
00059                 if (inputPanel == null) {
00060                         inputPanel = new InkPanel();
00061 
00062                         // set the background of the panel
00063                         inputPanel.setOpaque(false);
00064                         inputPanel.setPreferredSize(new Dimension(593, 768));
00065                         inputPanel.addMouseListener(this);
00066                         inputPanel.addMouseMotionListener(this);
00067                         inputPanel.displayInvertedInkColor();
00068 
00069                         ink = inputPanel.addNewInk();
00070                 }
00071                 return inputPanel;
00072         }
00073 
00074         @Override
00075         public void mouseClicked(MouseEvent e) {
00076         }
00077 
00078         @Override
00079         public void mouseDragged(MouseEvent e) {
00080                 if (SwingUtilities.isRightMouseButton(e)) {
00081                         return;
00082                 }
00083 
00084                 // DebugUtils.println("Dragging: " + e.getX() + ", " + e.getY());
00085                 PenSample penSample = new PenSample(e.getX(), e.getY(), 128, System.currentTimeMillis());
00086 
00087                 for (PenListener l : listeners) {
00088                         l.sample(penSample);
00089                 }
00090                 currentStroke.addSample(penSample);
00091                 inputPanel.repaint();
00092         }
00093 
00094         @Override
00095         public void mouseEntered(MouseEvent arg0) {
00096         }
00097 
00098         @Override
00099         public void mouseExited(MouseEvent arg0) {
00100         }
00101 
00102         @Override
00103         public void mouseMoved(MouseEvent arg0) {
00104         }
00105 
00106         @Override
00107         public void mousePressed(MouseEvent e) {
00108                 if (SwingUtilities.isRightMouseButton(e)) {
00109                         return;
00110                 }
00111 
00112                 // DebugUtils.println("Pressed: " + e.getX() + ", " + e.getY());
00113                 PenSample sample = new PenSample(e.getX(), e.getY(), 128, System.currentTimeMillis());
00114                 for (PenListener l : listeners) {
00115                         l.penDown(sample);
00116                 }
00117                 currentStroke = new InkStroke();
00118                 currentStroke.addSample(sample);
00119                 ink.addStroke(currentStroke);
00120                 inputPanel.repaint();
00121         }
00122 
00123         @Override
00124         public void mouseReleased(MouseEvent e) {
00125                 if (SwingUtilities.isRightMouseButton(e)) {
00126                         // clear the board!
00127                         inputPanel.clear();
00128                         ink = inputPanel.addNewInk();
00129                 } else {
00130                         // DebugUtils.println("Released: " + e.getX() + ", " + e.getY());
00131                         PenSample penSample = new PenSample(e.getX(), e.getY(), 0, System.currentTimeMillis());
00132                         penSample.setPenUp(true);
00133                         for (PenListener l : listeners) {
00134                                 l.penUp(penSample);
00135                         }
00136                         currentStroke.addSample(penSample);
00137 
00138                         // simplify this stroke!
00139                         simplifier.simplifyStroke(currentStroke);
00140 
00141                         inputPanel.repaint();
00142                 }
00143         }
00144 
00145         /*
00146          * (non-Javadoc)
00147          * 
00148          * @see edu.stanford.hci.r3.pen.PenInput#removeLivePenListener(edu.stanford.hci.r3.pen.streaming.listeners.PenListener)
00149          */
00150         public void removeLivePenListener(PenListener penListener) {
00151                 listeners.remove(penListener);
00152         }
00153 
00154         @Override
00155         public void startLiveMode() {
00156                 // start up a JFrame w/ GUI to get mouse input...
00157                 frame = new JFrame("Pen Simulator");
00158                 frame.setContentPane(getInputPanel());
00159                 // if you exit your pen, we might as well close the entire paper application
00160                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00161                 frame.pack();
00162                 frame.setLocation(WindowUtils.getWindowOrigin(frame, WindowUtils.DESKTOP_CENTER));
00163                 frame.setVisible(true);
00164                 liveMode = true;
00165         }
00166 
00167         @Override
00168         public void stopLiveMode() {
00169                 frame.dispose();
00170                 frame = null;
00171                 liveMode = false;
00172         }
00173 }

Generated on Sat Apr 14 18:21:37 2007 for R3 Paper Toolkit by  doxygen 1.4.7