View Javadoc

1   /*
2    * @(#)$Id: SwingMouseReaderPanel.java 787 2010-03-27 15:06:04Z bpuype $
3    *
4    * Author		:	Ueli Kurmann, igesture@uelikurmann.ch
5    *                  
6    *
7    * Purpose		: 
8    *
9    * -----------------------------------------------------------------------
10   *
11   * Revision Information:
12   *
13   * Date				Who			Reason
14   *
15   * 04.05.2008			ukurmann	Initial Release
16   *
17   * -----------------------------------------------------------------------
18   *
19   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20   *
21   * This software is the proprietary information of ETH Zurich.
22   * Use is subject to license terms.
23   * 
24   */
25  
26  package org.ximtec.igesture.io.mouseclient;
27  
28  import java.awt.Cursor;
29  import java.awt.Graphics;
30  import java.awt.Point;
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  import javax.swing.JPanel;
35  
36  import org.sigtec.ink.Note;
37  import org.sigtec.ink.Trace;
38  import org.ximtec.igesture.core.Gesture;
39  import org.ximtec.igesture.io.GestureDevicePanel;
40  import org.ximtec.igesture.io.GestureEventListener;
41  
42  /**
43   * Comment
44   * 
45   * @version 1.0 04.05.2008
46   * @author Ueli Kurmann
47   */
48  public class SwingMouseReaderPanel extends GestureDevicePanel implements GestureEventListener {
49  
50    private SwingMouseReader reader;
51  
52    public SwingMouseReaderPanel(SwingMouseReader reader) {
53      this.reader = reader;
54      setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
55    }
56  
57    public void drawLine(Point lastPoint, Point currentPoint) {
58  
59      if (getGraphics() != null && currentPoint != null && lastPoint != null
60          && lastPoint.distance(currentPoint) < 1000000) {
61  
62        getGraphics().drawLine((int) lastPoint.getX(), (int) lastPoint.getY(), (int) currentPoint.getX(),
63            (int) currentPoint.getY());
64  
65      }
66    }
67  
68    public void clear() {
69      if (getGraphics() != null) {
70        getGraphics().clearRect(0, 0, getWidth(), getHeight());
71      }
72  
73      repaint();
74    }
75  
76    @Override
77    public void paint(Graphics g) {
78      super.paint(g);
79      redrawGesture();
80    }
81  
82    @Override
83    public void repaint() {
84      super.repaint();
85      redrawGesture();
86    }
87  
88    private void redrawGesture() {
89      if (reader != null) {
90  
91        Note note = reader.getGesture().getGesture();
92        if (note != null) {
93          List<Point> buffer = new ArrayList<Point>();
94          for (Trace trace : note.getTraces()) {
95            for (org.sigtec.ink.Point point : trace.getPoints()) {
96              buffer.add(new Point((int) point.getX(), (int) point.getY()));
97            }
98          }
99  
100         for (int i = 0; i < buffer.size() - 1; i++) {
101           drawLine(buffer.get(i), buffer.get(i + 1));
102         }
103       }
104     }
105   }
106 
107 /* (non-Javadoc)
108  * @see org.ximtec.igesture.io.GestureEventListener#handleChunks(java.util.List)
109  */
110 @Override
111 public void handleChunks(List<?> chunks) {
112 }
113 
114 /* (non-Javadoc)
115  * @see org.ximtec.igesture.io.GestureEventListener#handleGesture(org.ximtec.igesture.core.Gesture)
116  */
117 @Override
118 public void handleGesture(Gesture<?> gesture) {
119 }
120 
121 }