我正在处理leapmotion。我正在制作一个简单的应用程序,它将跟踪index_finger运动并绘制它... 一切都很好...我的项目是跟踪我的指针和草图按需要但问题是当我停止草绘(我使用圆形运动启用草图和点击运动停止)没有什么可以告诉我我的光标在哪里是...我必须猜测我的index_finger指针的位置。 这是我到目前为止所做的......
import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
import com.leapmotion.leap.Controller;
import com.leapmotion.leap.Finger;
import com.leapmotion.leap.Frame;
import com.leapmotion.leap.Pointable;
import com.leapmotion.leap.Hand;
import com.leapmotion.leap.Tool;
import com.leapmotion.leap.Vector;
import com.leapmotion.leap.Gesture;
import com.leapmotion.leap.processing.LeapMotion;
LeapMotion leapMotion;
ConcurrentMap<Integer, Integer> fingerColors;
ConcurrentMap<Integer, Integer> toolColors;
ConcurrentMap<Integer, Vector> fingerPositions;
ConcurrentMap<Integer, Vector> toolPositions;
float hey = 21;
float hi = 15;
float hoo = 0;
boolean circle_me = false;
boolean tap_me = false;
void setup()
{
size(16 * 50, 9 * 50);
background(250);
frameRate(60);
ellipseMode(CENTER);
leapMotion = new LeapMotion(this);
fingerColors = new ConcurrentHashMap<Integer, Integer>();
fingerPositions = new ConcurrentHashMap<Integer, Vector>();
}
void draw()
{
text("Sketch Pad", 250,80);
for (Map.Entry entry : fingerPositions.entrySet())
{
Integer fingerId = (Integer) entry.getKey();
// println ("finger_id: " + fingerId);
Vector position = (Vector) entry.getValue();
// fill(fingerColors.get(fingerId));
if (keyPressed && (key == 'd') || circle_me == true){
smooth();
fill(random(0,255),random(0,255),random(0,255),120);
// fill(255, 0, 0, 25);
//stroke(0);
noStroke();
ellipseMode(CENTER);
ellipse(leapMotion.leapToSketchX(position.getX()), leapMotion.leapToSketchY(position.getY()), 25.0, 25.0);
}
//else
//pencil(position);
if(tap_me){
circle_me = false;
tap_me = false;
}
/*
else{
//fill(100);
//rect(0,0,width,height);
ellipseMode(CENTER);
ellipse(leapMotion.leapToSketchX(position.getX()), leapMotion.leapToSketchY(position.getY()), 5.0, 5.0);
}
*/
if (keyPressed && (key == 'e'))
background(255);
}
}
boolean pencil(Vector position){
if(hoo == 0){
hi = leapMotion.leapToSketchX(position.getX());
hey = leapMotion.leapToSketchY(position.getY());
hoo++;
}
line(leapMotion.leapToSketchX(position.getX()), leapMotion.leapToSketchY(position.getY()), hi, hey);
hi = leapMotion.leapToSketchX(position.getX());
hey = leapMotion.leapToSketchY(position.getY());
strokeWeight(1);
// println("x: " + position.getX() + "\n y: " + position.getY());
return true;
}
void onInit(final Controller controller)
{
controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
// enable background policy
controller.setPolicyFlags(Controller.PolicyFlag.POLICY_BACKGROUND_FRAMES);
}
void onFrame(final Controller controller)
{
Frame frame = controller.frame();
for (Gesture gesture : frame.gestures())
{
if ("TYPE_CIRCLE".equals(gesture.type().toString()) && "STATE_START".equals(gesture.state().toString())) {
//circle_me = true;
println("circled " + gesture.duration());
}
else if ("TYPE_CIRCLE".equals(gesture.type().toString()) && "STATE_STOP".equals(gesture.state().toString())) {
circle_me = true;
println("circledS " + gesture.duration());
}
else if ("TYPE_KEY_TAP".equals(gesture.type().toString()) && "STATE_STOP".equals(gesture.state().toString())) {
if ("TYPE_KEY_TAP".equals(gesture.type().toString()) && "STATE_STOP".equals(gesture.state().toString())) {
tap_me = true;
println("tapped " + gesture.duration());
}
}
}
fingerPositions.clear();
for (Finger finger : frame.fingers())
{
int fingerId = finger.id();
color c = color(random(0, 255), random(0, 255), random(0, 255));
fingerColors.putIfAbsent(fingerId, c);
if("TYPE_INDEX".equals(finger.type().toString())){
fingerPositions.put(fingerId, finger.stabilizedTipPosition());
String finger1 = finger.type().toString();
// println ("index: " + finger.stabilizedTipPosition());
//println("x: " + finger.getX() + "\n y: " + finger.getY());
}
/*
if("TYPE_THUMB".equals(finger.type().toString())){
String finger2 = finger.type().toString();
// fingerPositions.put(fingerId, finger.stabilizedTipPosition());
// println ("thumb: " + finger.stabilizedTipPosition());
}
*/
}
}
当草绘模式处于非活动状态时,我需要一些像光标一样的东西。
答案 0 :(得分:0)
你必须画一个&#34;屏外缓冲区&#34;使用PGraphics对象然后将其和光标绘制到屏幕上。见https://www.processing.org/reference/createGraphics_.html
这是一个例子,我改编自我做过的其他事情。按一个键打开和关闭绘画:
import com.leapmotion.leap.*;
int width = 800;
int height = 600;
int maxBrushSize = 120;
color canvasColor = 0xffffff;
float alphaVal = 10;
PGraphics canvas;
Boolean isDrawing = true;
Controller leap = new Controller();
void setup()
{
frameRate(120);
size(width, height);
canvas = createGraphics(width, height);
background(canvasColor);
stroke(0x00ffffff);
}
void draw(){
Frame frame = leap.frame();
Pointable pointer = frame.pointables().frontmost();
if( pointer.isValid() )
{
background(canvasColor);
color frontColor = color( 255, 0, 0, alphaVal );
color backColor = color( 0, 0, 255, alphaVal );
InteractionBox iBox = frame.interactionBox();
Vector tip = iBox.normalizePoint(pointer.tipPosition());
float x = tip.getX() * width;
float y = height - tip.getY() * height;
float brushSize = maxBrushSize - maxBrushSize * tip.getZ();
if(isDrawing){
canvas.beginDraw();
canvas.fill(frontColor);
canvas.noStroke();
canvas.ellipse( x, y, brushSize, brushSize);
canvas.endDraw();
}
image(canvas, 0, 0); //Draw canvas to screen
noFill();
stroke(0, 0, 0);
ellipse( x, y, brushSize, brushSize); // draw cursor
}
}
void keyPressed()
{
isDrawing = !isDrawing;
}