Sunday 1 November 2015

Virtual Canvas

//Filename : FingerBall.java and PongCanvas.java
/* Virtual Canvas . The Virtual Canvas allows the user to draw in 3D space without the need of any wearable device, it uses a tool ,which is nothing but an elongated stick to draw in 3D space.
Requirements:Leap Motion Controller
Youtube Link of the video : https://youtu.be/RF2lRllYuOE
+Leap Motion
+Leap Motion Developers
*/


//FingerBall.java

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import com.leapmotion.leap.*;
import com.leapmotion.leap.Controller.PolicyFlag;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
class Sample extends Listener {
public void onConnect(Controller controller) {
System.out.println("Connnected");
controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
controller.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);
controller.enableGesture(Gesture.Type.TYPE_SWIPE);
}
public void onInitialise() {
System.out.println("Initialised");
}
public void onFrame(Controller controller) {

}
}
public class FingerBall {
public static void main(String[] args) throws Exception {
Sample listener = new Sample();
PongCanvas fc = new PongCanvas();
java.awt.Frame f = new java.awt.Frame();
Controller controller = new Controller();
controller.policyFlags();
controller.setPolicyFlags(PolicyFlag.POLICY_BACKGROUND_FRAMES);
controller.addListener(listener);
System.out.println("Press Enter to quit...");
try {
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setLocation(0, 0);
f.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
f.setBackground(Color.black);
fc.start();
f.add(fc);
f.pack();
f.setVisible(true);
System.in.read();
} catch (IOException e) {
}
controller.removeListener(listener);
}
}



// PongCanvas.java

import java.awt.Canvas;
import com.leapmotion.leap.*;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class PongCanvas extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
private Rectangle rect = null;
private Thread looper;
private boolean running;
private int diameter;
private BufferedImage bufferedimage;
static int ch = 0;
Hand hand[] = new Hand[5];
Finger finger[][] = new Finger[5][15];
Pointable pointable = new Pointable();
int xCurrent2 = 0;
int yCurrent2 = 0;
int checker = 0;
static Graphics g1 = null;
PongCanvas() {
setSize(java.awt.Frame.MAXIMIZED_HORIZ, java.awt.Frame.MAXIMIZED_VERT);
}
public void init() {
}
public void start() {
if (!running) {
running = true;
looper = new Thread(this);
looper.start();
}
}
public void stop() {
running = false;
}
public void captureScreen() throws Exception {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd hh mm ss a");
Calendar now = Calendar.getInstance();
Robot robot = new Robot();
BufferedImage screenShot = robot.createScreenCapture(new Rectangle(20,
20, 1330, 700));
ImageIO.write(screenShot, "PNG", new File("/Users/mohammedalichherawalla/Documents/workspace/UtilityKit/images/"
+ formatter.format(now.getTime()) + ".png"));

}
public void run() {
try {
Controller controller = new Controller();
while (running) {
Frame frame = controller.frame();
GestureList gestures = frame.gestures();
Gesture gesture = gestures.get(0);
Gesture gesture1 = gestures.get(1);
Gesture gesture2 = gestures.get(2);
if (gesture.type() == Gesture.Type.TYPE_KEY_TAP
|| gesture1.type() == Gesture.Type.TYPE_KEY_TAP
|| gesture2.type() == Gesture.Type.TYPE_KEY_TAP) {
captureScreen();
ch = 3;

} else if (gesture.type() == Gesture.Type.TYPE_SCREEN_TAP
|| gesture1.type() == Gesture.Type.TYPE_SCREEN_TAP
|| gesture2.type() == Gesture.Type.TYPE_SCREEN_TAP) {
ch = 1;

} else if (gesture.type() == Gesture.Type.TYPE_SWIPE
|| gesture1.type() == Gesture.Type.TYPE_SWIPE
|| gesture2.type() == Gesture.Type.TYPE_SWIPE) {
ch = 2;
}
repaint();
}
} catch (Exception e) {
running = false;
}
}
public void update(Graphics g) {
if (!getBounds().equals(rect)) {
rect = getBounds();
diameter = 30;
bufferedimage = new BufferedImage(1366, 768,
BufferedImage.TYPE_INT_RGB);
}
if (running) {
paint(bufferedimage.getGraphics());
g.drawImage(bufferedimage, 0, 0, null);
}
}
public void paint(Graphics g) {
if (rect == null) {
return;
}
g1 = g;
Controller controller = new Controller();
Frame frame = controller.frame();
HandList allHands = frame.hands();
int countOfHands = allHands.count();
FingerList allFingers = frame.fingers();
int countOfFingers = allFingers.count();
if (ch == 0) {
g.setColor(Color.blue);
Font monoFont = new Font("Monospaced", Font.BOLD | Font.ITALIC, 36);
g.setColor(Color.white);
g.setFont(monoFont);
FontMetrics fm = g.getFontMetrics();
fm.stringWidth("Virtual Canvas ");
fm.getAscent();
g.drawString("Virtual Canvas", 120, 50);
fm.stringWidth("Screen Tap to clear screen ");
fm.getAscent();
g.drawString("Screen Tap to clear screen", 120, 150);
fm.stringWidth("Virtual Canvas ");
fm.getAscent();
g.drawString("Key press to save work", 120, 250);
fm.stringWidth("Key press to save work");
fm.getAscent();
g.drawString("Swipe to start Virtual Canvas !", 120, 350);
}
else if (ch == 1) {
int xCurrent1 = 0;
int yCurrent1 = 0;
g.setColor(Color.black);
g.fillRect(0, 0, rect.width, rect.height);
}
else if (ch == 2) {
int xCurrent1 = 0;
int yCurrent1 = 0;
g.setColor(Color.black);
g.fillRect(0, 0, 1360, 60);
g.setColor(Color.blue);
Font monoFont = new Font("Monospaced", Font.BOLD | Font.ITALIC, 36);
g.setColor(Color.white);
g.setFont(monoFont);
FontMetrics fm = g.getFontMetrics();
fm.stringWidth("Virtual Canvas ");
fm.getAscent();
g.drawString("Virtual Canvas", 120, 50);
PointableList p = frame.pointables();
int countOfPointables = p.count();
int type1 = 0;
int i = 0;
if (frame.pointables().get(0).isTool())
{
pointable = frame.pointables().get(0);
xCurrent1 = (int) pointable.tipPosition().getX() + 550;
yCurrent1 = 600 - (int) pointable.tipPosition().getY();
diameter=(int) pointable.tipPosition().getZ();;
diameter/=7;
if(diameter<5){
diameter=5;
}
g.setColor(Color.white);
g.drawRect((int) xCurrent1, (int) yCurrent1, diameter,
diameter );

}
} else if (ch == 3) {
g.setColor(Color.black);
g.fillRect(0, 0, 1360, 766);

g.setColor(Color.blue);
Font monoFont = new Font("Monospaced", Font.BOLD | Font.ITALIC, 36);
g.setColor(Color.white);
g.setFont(monoFont);
FontMetrics fm = g.getFontMetrics();
fm.stringWidth("Saved ");
fm.getAscent();
g.drawString("Saved", 120, 50);
}

}
}

No comments:

Post a Comment