front_processing/extern/TUIO_JAVA/src/TuioDemoComponent.java
changeset 9 0f44b7360c8d
parent 3 92f19af39024
child 10 925b7ee746e3
equal deleted inserted replaced
8:e4e7db2435f8 9:0f44b7360c8d
     1 /*
     1 /*
     2 	TUIO Java Demo - part of the reacTIVision project
     2     TUIO Java Demo - part of the reacTIVision project
     3 	http://reactivision.sourceforge.net/
     3     http://reactivision.sourceforge.net/
     4 
     4 
     5 	Copyright (c) 2005-2009 Martin Kaltenbrunner <mkalten@iua.upf.edu>
     5     Copyright (c) 2005-2009 Martin Kaltenbrunner <mkalten@iua.upf.edu>
     6 
     6 
     7 	This program is free software; you can redistribute it and/or modify
     7     This program is free software; you can redistribute it and/or modify
     8 	it under the terms of the GNU General Public License as published by
     8     it under the terms of the GNU General Public License as published by
     9 	the Free Software Foundation; either version 2 of the License, or
     9     the Free Software Foundation; either version 2 of the License, or
    10 	(at your option) any later version.
    10     (at your option) any later version.
    11 
    11 
    12 	This program is distributed in the hope that it will be useful,
    12     This program is distributed in the hope that it will be useful,
    13 	but WITHOUT ANY WARRANTY; without even the implied warranty of
    13     but WITHOUT ANY WARRANTY; without even the implied warranty of
    14 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15 	GNU General Public License for more details.
    15     GNU General Public License for more details.
    16 
    16 
    17 	You should have received a copy of the GNU General Public License
    17     You should have received a copy of the GNU General Public License
    18 	along with this program; if not, write to the Free Software
    18     along with this program; if not, write to the Free Software
    19 	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    19     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    20 */
    20 */
    21 
    21 
    22 import java.awt.*;
    22 import java.awt.*;
    23 import java.awt.geom.*;
    23 import java.awt.geom.*;
    24 import java.awt.event.*;
    24 import java.awt.event.*;
    27 import javax.swing.*;
    27 import javax.swing.*;
    28 import TUIO.*;
    28 import TUIO.*;
    29 
    29 
    30 public class TuioDemoComponent extends JComponent implements TuioListener {
    30 public class TuioDemoComponent extends JComponent implements TuioListener {
    31 
    31 
    32 	private Hashtable<Long,TuioDemoObject> objectList = new Hashtable<Long,TuioDemoObject>();
    32     private Hashtable<Long,TuioDemoObject> objectList = new Hashtable<Long,TuioDemoObject>();
    33 	private Hashtable<Long,TuioCursor> cursorList = new Hashtable<Long,TuioCursor>();
    33     private Hashtable<Long,TuioCursor> cursorList = new Hashtable<Long,TuioCursor>();
    34 	private Hashtable<Long,TuioString> stringList = new Hashtable<Long,TuioString>();
    34     private Hashtable<Long,TuioString> stringList = new Hashtable<Long,TuioString>();
    35 
    35 
    36 	public static final int finger_size = 15;
    36     public static final int finger_size = 15;
    37 	public static final int object_size = 60;
    37     public static final int object_size = 60;
    38 	public static final int table_size = 760;
    38     public static final int table_size = 760;
    39 	
    39     
    40 	public static int width, height;
    40     public static int width, height;
    41 	private float scale = 1.0f;
    41     private float scale = 1.0f;
    42 	public boolean verbose = false;
    42     public boolean verbose = false;
    43 			
    43             
    44 	public void setSize(int w, int h) {
    44     public void setSize(int w, int h) {
    45 		super.setSize(w,h);
    45         super.setSize(w,h);
    46 		width = w;
    46         width = w;
    47 		height = h;
    47         height = h;
    48 		scale  = height/(float)TuioDemoComponent.table_size;	
    48         scale  = height/(float)TuioDemoComponent.table_size;    
    49 	}
    49     }
    50 	
    50     
    51 	public void addTuioObject(TuioObject tobj) {
    51     public void addTuioObject(TuioObject tobj) {
    52 		TuioDemoObject demo = new TuioDemoObject(tobj);
    52         TuioDemoObject demo = new TuioDemoObject(tobj);
    53 		objectList.put(tobj.getSessionID(),demo);
    53         objectList.put(tobj.getSessionID(),demo);
    54 
    54 
    55 		if (verbose) 
    55         if (verbose) 
    56 			System.out.println("add obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle());	
    56             System.out.println("add obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle());    
    57 	}
    57     }
    58 
    58 
    59 	public void updateTuioObject(TuioObject tobj) {
    59     public void updateTuioObject(TuioObject tobj) {
    60 
    60 
    61 		TuioDemoObject demo = (TuioDemoObject)objectList.get(tobj.getSessionID());
    61         TuioDemoObject demo = (TuioDemoObject)objectList.get(tobj.getSessionID());
    62 		demo.update(tobj);
    62         demo.update(tobj);
    63 		
    63         
    64 		if (verbose) 
    64         if (verbose) 
    65 			System.out.println("set obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()+" "+tobj.getMotionSpeed()+" "+tobj.getRotationSpeed()+" "+tobj.getMotionAccel()+" "+tobj.getRotationAccel()); 	
    65             System.out.println("set obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()+" "+tobj.getMotionSpeed()+" "+tobj.getRotationSpeed()+" "+tobj.getMotionAccel()+" "+tobj.getRotationAccel());     
    66 	}
    66     }
    67 	
    67     
    68 	public void removeTuioObject(TuioObject tobj) {
    68     public void removeTuioObject(TuioObject tobj) {
    69 		objectList.remove(tobj.getSessionID());
    69         objectList.remove(tobj.getSessionID());
    70 		
    70         
    71 		if (verbose) 
    71         if (verbose) 
    72 			System.out.println("del obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")");	
    72             System.out.println("del obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")");    
    73 	}
    73     }
    74 
    74 
    75 	public void addTuioCursor(TuioCursor tcur) {
    75     public void addTuioCursor(TuioCursor tcur) {
    76 	
    76     
    77 		if (!cursorList.containsKey(tcur.getSessionID())) {
    77         if (!cursorList.containsKey(tcur.getSessionID())) {
    78 			cursorList.put(tcur.getSessionID(), tcur);
    78             cursorList.put(tcur.getSessionID(), tcur);
    79 			repaint();
    79             repaint();
    80 		}
    80         }
    81 		
    81         
    82 		if (verbose) 
    82         if (verbose) 
    83 			System.out.println("add cur "+tcur.getCursorID()+" ("+tcur.getSessionID()+") "+tcur.getX()+" "+tcur.getY());	
    83             System.out.println("add cur "+tcur.getCursorID()+" ("+tcur.getSessionID()+") "+tcur.getX()+" "+tcur.getY());    
    84 	}
    84     }
    85 
    85 
    86 	public void updateTuioCursor(TuioCursor tcur) {
    86     public void updateTuioCursor(TuioCursor tcur) {
    87 
    87 
    88 		repaint();
    88         repaint();
    89 		
    89         
    90 		if (verbose) 
    90         if (verbose) 
    91 			System.out.println("set cur "+tcur.getCursorID()+" ("+tcur.getSessionID()+") "+tcur.getX()+" "+tcur.getY()+" "+tcur.getMotionSpeed()+" "+tcur.getMotionAccel()); 
    91             System.out.println("set cur "+tcur.getCursorID()+" ("+tcur.getSessionID()+") "+tcur.getX()+" "+tcur.getY()+" "+tcur.getMotionSpeed()+" "+tcur.getMotionAccel()); 
    92 	}
    92     }
    93 	
    93     
    94 	public void removeTuioCursor(TuioCursor tcur) {
    94     public void removeTuioCursor(TuioCursor tcur) {
    95 	
    95     
    96 		cursorList.remove(tcur.getSessionID());	
    96         cursorList.remove(tcur.getSessionID());    
    97 		repaint();
    97         repaint();
    98 		
    98         
    99 		if (verbose) 
    99         if (verbose) 
   100 			System.out.println("del cur "+tcur.getCursorID()+" ("+tcur.getSessionID()+")"); 
   100             System.out.println("del cur "+tcur.getCursorID()+" ("+tcur.getSessionID()+")"); 
   101 	}
   101     }
   102 	
   102     
   103 	public void addTuioString(TuioString tstr) {
   103     public void addTuioString(TuioString tstr) {
   104 	
   104     
   105 		if (!stringList.containsKey(tstr.getSessionID())) {
   105         if (!stringList.containsKey(tstr.getSessionID())) {
   106 			stringList.put(tstr.getSessionID(), tstr);
   106             stringList.put(tstr.getSessionID(), tstr);
   107 			repaint();
   107             repaint();
   108 		}
   108         }
   109 		
   109         
   110 		if (verbose) 
   110         if (verbose) 
   111 			System.out.println("add str "+tstr.getStringID()+" ("+tstr.getSessionID()+") "+tstr.getMessage());	
   111             System.out.println("add str "+tstr.getStringID()+" ("+tstr.getSessionID()+") "+tstr.getMessage());    
   112 	}
   112     }
   113 
   113 
   114 	public void updateTuioString(TuioString tstr) {
   114     public void updateTuioString(TuioString tstr) {
   115 
   115 
   116 		repaint();
   116         repaint();
   117 		
   117         
   118 		if (verbose) 
   118         if (verbose) 
   119 			System.out.println("set str "+tstr.getStringID()+" ("+tstr.getSessionID()+") "+tstr.getMessage()); 
   119             System.out.println("set str "+tstr.getStringID()+" ("+tstr.getSessionID()+") "+tstr.getMessage()); 
   120 	}
   120     }
   121 	
   121     
   122 	public void removeTuioString(TuioString tstr) {
   122     public void removeTuioString(TuioString tstr) {
   123 	
   123     
   124 		stringList.remove(tstr.getSessionID());	
   124         stringList.remove(tstr.getSessionID());    
   125 		repaint();
   125         repaint();
   126 		
   126         
   127 		if (verbose) 
   127         if (verbose) 
   128 			System.out.println("del str "+tstr.getStringID()+" ("+tstr.getSessionID()+")"); 
   128             System.out.println("del str "+tstr.getStringID()+" ("+tstr.getSessionID()+")"); 
   129 	}
   129     }
   130 
   130 
   131 	public void refresh(TuioTime frameTime) {
   131     public void refresh(TuioTime frameTime) {
   132 		repaint();
   132         repaint();
   133 	}
   133     }
   134 	
   134     
   135 	public void paint(Graphics g) {
   135     public void paint(Graphics g) {
   136 		update(g);
   136         update(g);
   137 	}
   137     }
   138 
   138 
   139 	public void update(Graphics g) {
   139     public void update(Graphics g) {
   140 	
   140     
   141 		Graphics2D g2 = (Graphics2D)g;
   141         Graphics2D g2 = (Graphics2D)g;
   142 		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
   142         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
   143 		g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
   143         g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
   144 	
   144     
   145 		g2.setColor(Color.white);
   145         g2.setColor(Color.white);
   146 		g2.fillRect(0,0,width,height);
   146         g2.fillRect(0,0,width,height);
   147 	
   147     
   148 		int w = (int)Math.round(width-scale*finger_size/2.0f);
   148         int w = (int)Math.round(width-scale*finger_size/2.0f);
   149 		int h = (int)Math.round(height-scale*finger_size/2.0f);
   149         int h = (int)Math.round(height-scale*finger_size/2.0f);
   150 		
   150         
   151 		Enumeration<TuioCursor> cursors = cursorList.elements();
   151         Enumeration<TuioCursor> cursors = cursorList.elements();
   152 		while (cursors.hasMoreElements()) {
   152         while (cursors.hasMoreElements()) {
   153 			TuioCursor tcur = cursors.nextElement();
   153             TuioCursor tcur = cursors.nextElement();
   154 			if (tcur==null) continue;
   154             if (tcur==null) continue;
   155 			Vector<TuioPoint> path = tcur.getPath();
   155             Vector<TuioPoint> path = tcur.getPath();
   156 			TuioPoint current_point = path.elementAt(0);
   156             TuioPoint current_point = path.elementAt(0);
   157 			if (current_point!=null) {
   157             if (current_point!=null) {
   158 				// draw the cursor path
   158                 // draw the cursor path
   159 				g2.setPaint(Color.blue);
   159                 g2.setPaint(Color.blue);
   160 				for (int i=0;i<path.size();i++) {
   160                 for (int i=0;i<path.size();i++) {
   161 					TuioPoint next_point = path.elementAt(i);
   161                     TuioPoint next_point = path.elementAt(i);
   162 					g2.drawLine(current_point.getScreenX(w), current_point.getScreenY(h), next_point.getScreenX(w), next_point.getScreenY(h));
   162                     g2.drawLine(current_point.getScreenX(w), current_point.getScreenY(h), next_point.getScreenX(w), next_point.getScreenY(h));
   163 					current_point = next_point;
   163                     current_point = next_point;
   164 				}
   164                 }
   165 			}
   165             }
   166 			
   166             
   167 			// draw the finger tip
   167             // draw the finger tip
   168 			g2.setPaint(Color.lightGray);
   168             g2.setPaint(Color.lightGray);
   169 			int s = (int)(scale*finger_size);
   169             int s = (int)(scale*finger_size);
   170 			g2.fillOval(current_point.getScreenX(w-s/2),current_point.getScreenY(h-s/2),s,s);
   170             g2.fillOval(current_point.getScreenX(w-s/2),current_point.getScreenY(h-s/2),s,s);
   171 			g2.setPaint(Color.black);
   171             g2.setPaint(Color.black);
   172 			g2.drawString(tcur.getCursorID()+"",current_point.getScreenX(w),current_point.getScreenY(h));
   172             g2.drawString(tcur.getCursorID()+"",current_point.getScreenX(w),current_point.getScreenY(h));
   173 		}
   173         }
   174 
   174 
   175 		// draw the objects
   175         // draw the objects
   176 		Enumeration<TuioDemoObject> objects = objectList.elements();
   176         Enumeration<TuioDemoObject> objects = objectList.elements();
   177 		while (objects.hasMoreElements()) {
   177         while (objects.hasMoreElements()) {
   178 			TuioDemoObject tobj = objects.nextElement();
   178             TuioDemoObject tobj = objects.nextElement();
   179 			if (tobj!=null) tobj.paint(g2, width,height);
   179             if (tobj!=null) tobj.paint(g2, width,height);
   180 		}		
   180         }        
   181 	}
   181     }
   182 }
   182 }