Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Selected lines "<------- These" gives an erroror: This method requires a body instead of a semicolon
Both files must work together.
How to fix it I need quick help. Does anyone have any idea but i dont?
package API.Info;
import Mains.MiniEvents;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import java.util.HashMap;
public class ApiInfo {
public static MiniEvents plugin;
public ApiInfo(MiniEvents plugin) {
ApiInfo.plugin = plugin;
}
/**
* #return number of players in the event.
*/
public int eventSize() {
return plugin.getInfo().eventSize();
}
/**
* #return "code" name of the current event running; else, "none"
* It will return one of the following:
* horse, koth, oitc, paint, tdm, ko, lms, parkour, spleef, tnt
*/
public String getEventName() {
return plugin.getInfo().getEventName();
}
/**
* #return TRUE if an event is starting (counting down).
*/
public boolean eventStarting(){
return plugin.getInfo().eventStarting();
}
/**
* #return TRUE if an event has started.
*/
public boolean eventStarted(); <------- These
/**
* #param player - Player to check.
* #return TRUE if a player is currently playing in an event.
*/
public boolean inEvent(Player player); <------- These
/**
* #return the "formal" name of an event that is running.
* param eventName - the event for which to return the formal name for.
*/
public String eventFormalEventName(String eventName); <------- These
/**
* #return time left until the event starts
*/
public int getTimeLeft(); <------- These
/**
* #param player - Gets that player's file.
* #return the FileConfiguration where individual player data is stored.
*/
public FileConfiguration getPlayerFile(Player player); <------- These
/**
* The is a big "inevent" HashSet<Player> that contains a player no matter what event
* that player is in.
*
* #return the "inevent" HashSet.
*/
public HashMap<String, String> getPlayersInEvent(); <------- These
/**
* #return true if a player is currently in spectate mode.
*/
public boolean inSpectateMode(Player player); <------- These
}
File Info.jar
package Util.Methods;
import API.Info.ApiInfo;
import Mains.MiniEvents;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Scoreboard;
import java.util.HashMap;
import java.util.HashSet;
public class Info extends ApiInfo{
public MiniEvents plugin;
public Info(MiniEvents plugin) {
super(plugin);
this.plugin = plugin;
}
public boolean eventstarting = false;
public boolean eventstarted = false;
public boolean cancelled = false;
public final HashSet<String> sbefore = new HashSet<>();
public final HashSet<String> sblue = new HashSet<>();
public final HashSet<String> sred = new HashSet<>();
public final HashSet<String> sfire = new HashSet<>();
public final HashMap<String, String> inevent = new HashMap<>();
public final HashMap<String, Scoreboard> scoreboard = new HashMap<>();
public int eventSize(){
return inevent.size();
}
public String getEventName(){
return plugin.getEventName();
}
public boolean eventStarting(){
return eventstarting;
}
public boolean eventStarted(){
return eventstarted;
}
public boolean inEvent(Player player){
return inevent.containsKey(player.getName());
}
public String eventFormalEventName(String s){
return plugin.getFormalName(s);
}
public int getTimeLeft(){
return plugin.getTimerMain().getTimeLeft();
}
public FileConfiguration getPlayerFile(Player player){
return plugin.playerFile(player.getName().toLowerCase());
}
public HashMap<String, String> getPlayersInEvent(){
return inevent;
}
public boolean inSpectateMode(Player player){
return plugin.getSpectateMode().inspec.contains(player.getName());
}
}
You are trying to define a method without defining it. Methods (when not being abstract or declared in interfaces) always need the method head (void method()) and a body ({ ... }). In the lines you have marked, you are only defining the head but not the body.
If you want to have this method declared without instantly defining the body, you need to make them abstract.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to program something in which a lobster can get eaten by a pelican, but the pelican must disappear after eating said lobster. What code would remove the Pelican from world?
The current (full) code is:
import greenfoot.*;
/**
* Write a description of class Pelican here.
*
* #author (your name)
* #version (a version number or a date)
*/
public class Pelican extends Animal
{
/**
* Act - do whatever the Pelican wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private boolean lobsterEaten=false;
public void act()
{
randomTurn();
turnAtEdge();
lookForLobster();
move();
}
public void randomTurn()
{
if(Greenfoot.getRandomNumber(100)<10)
{
turn(Greenfoot.getRandomNumber(91)-45);
}
}
public void turnAtEdge()
{
if(atWorldEdge())
{
turn(17);
}
}
public void lookForLobster()
{
eat(Lobster.class);
}
}
Animal code:
import greenfoot.*;
import java.util.List;
import java.util.ArrayList;
/**
* Animal. This is the base class for all animals. In addition to the standard Actor
* methods, it provides the ability to move and turn.
*
* #author Michael Kolling
* #version 1.0
*/
public class Animal extends Actor
{
private static final double WALKING_SPEED = 5.0;
/**
* Constructor for Animal - nothing to do.
*/
public Animal()
{
}
/**
* Act - empty method. Animals have no default action.
*/
public void act()
{
}
/**
* Turn 'angle' degrees towards the right (clockwise).
*/
public void turn(int angle)
{
setRotation(getRotation() + angle);
}
/**
* Move forward in the current direction.
*/
public void move()
{
double angle = Math.toRadians( getRotation() );
int x = (int) Math.round(getX() + Math.cos(angle) * WALKING_SPEED);
int y = (int) Math.round(getY() + Math.sin(angle) * WALKING_SPEED);
setLocation(x, y);
}
/**
* Test if we are close to one of the edges of the world. Return true is we are.
*/
public boolean atWorldEdge()
{
if(getX() < 20 || getX() > getWorld().getWidth() - 20)
return true;
if(getY() < 20 || getY() > getWorld().getHeight() - 20)
return true;
else
return false;
}
/**
* Return true if we can see an object of class 'clss' right where we are.
* False if there is no such object here.
*/
public boolean canSee(Class clss)
{
Actor actor = getOneObjectAtOffset(0, 0, clss);
return actor != null;
}
/**
* Try to eat an object of class 'clss'. This is only successful if there
* is such an object where we currently are. Otherwise this method does
* nothing.
*/
public void eat(Class clss)
{
Actor actor = getOneObjectAtOffset(0, 0, clss);
if(actor != null) {
getWorld().removeObject(actor);
}
}
}
You will want to call getWorld().removeObject(this); inside of your Pelican's lookForLobster method. Or you could override Pelican's eat() method and do it there, but only after calling super.eat(); first.
I'm working on a project for school and I'm having some trouble here. I am to go through a file for a trivia game. The format of the file is as follows: The first line of the file will be the category of the questions, the following lines will be pairs of questions and answers. Until it hits a blank line, that indicates that the next line after the blank line starts a new category and it goes on. I am supposed to make an ArrayList that will have 6 indexes. One for each category, then in each of the indexes i should have groups of questions and answers that I will be able to utilize for comparison. I guess essentially an array inside of an arraylist.
I hope that what im trying to accomplish makes sense. Its very confusing to me. But here is the code of what Im trying to work on and with so many parts im getting very messed up.
import java.util.ArrayList;
public class TriviaQuestion {
private String player;
private String category;
private String question;
private String answer;
private int score = 0;
/**
*
*/
public TriviaQuestion() {
player = "unknown";
category = "unknown";
question = "unknown";
answer = "unknown";
score = 0;
}
public TriviaQuestion(String category, String question, String answer){
}
public TriviaQuestion(String question, String answer){
}
public TriviaQuestion(String player, String category, String question,
String answer, int score) {
super();
this.player = player;
this.category = category;
this.question = question;
this.answer = answer;
this.score = score;
}
/**
* #return the player
*/
public String getPlayer() {
return player;
}
/**
* #param player the player to set
*/
public void setPlayer(String player) {
this.player = player;
}
/**
* #return the category
*/
public String getCategory() {
return category;
}
/**
* #param category the category to set
*/
public void setCategory(String category) {
this.category = category;
}
/**
* #return the question
*/
public String getQuestion() {
return question;
}
/**
* #param question the question to set
*/
public void setQuestion(String question) {
this.question = question;
}
/**
* #return the answer
*/
public String getAnswer() {
return answer;
}
/**
* #param answer the answer to set
*/
public void setAnswer(String answer) {
this.answer = answer;
}
/**
* #return the score
*/
public int getScore() {
return score;
}
/**
* #param score the score to set
*/
public void setScore(int score) {
this.score = score;
}
/* (non-Javadoc)
* #see java.lang.Object#toString()
*/
#Override
public String toString() {
return "TriviaQuestion [category=" + category + ", question="
+ question + ", answer=" + answer + "]";
}
}
Then the tester, I tried to take some notes about it at the bottom in class, but they don't make a lot of sense to me at this point.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class TriviaQuestion2 {
/**
* #param args
* #throws IOException
*/
public static void main(String[] args) throws IOException {
File gameFile = new File("trivia.txt");
Scanner inFile = new Scanner(gameFile);
ArrayList<TriviaQuestion> question = new ArrayList<TriviaQuestion> ();
while (inFile.hasNextLine()){
boolean cat = inFile.hasNextLine();
boolean more = true;
while (inFile.hasNextLine() && more);
String answer;
TriviaQuestion temp = new TriviaQuestion(question, answer);
question[0] = new ArrayList<TriviaQuestion>();
new TriviaQuestion(q,a);
question[0].add(temp);
}
}
}
inFile.close();
//use a while loop inside a while loop, and first do category, then question/answer
//there are six categories, use array of trivia questions ArrayList <triviaquestions>[]
//question = new ArrayList<triviaQuesitons>[6]
//question[0] = new ArrayList<triviaQuesitons>();
/**
* while in question[0] and so on read the quesiton and answer
* temp = new tq(question, answer)
* question [0].add(temp)
* question[0].get(i)
*
*/
System.out.println(question);
}
// TODO Auto-generated method stub
}
You want some nested loops to read the file.
That's a poor way to describe the file-format. "Section" describes a section of the file. You can say that each section defines a category, with the first line the name of the category, subequent lines pairs of questions and answers, and terminated by a blank line/ or EOF.
In pseudocode, essentially you want:
List<Category> categories = new ArrayList();
while (in.hasNextLine()) {
String catName = in.readLine();
if (catName.trim().length() == 0)
continue; // skip extra blank lines between Sections.
Category cat = new Category( catName);
categories.add( cat);
while (in.hasNextLine()) {
String line = in.readLine();
if (line.trim().length() == 0)
break; // end of Section.
// parse a Question & it's Answer.
TriviaQuestion question = parseQuestion( line);
cat.addQuestion( question);
}
}
// done.
return categories;
I am supposed to make an ArrayList that will have 6 indexes. One for
each category, then in each of the indexes i should have groups of
questions and answers that I will be able to utilize for comparison.
That's a fairly confused way of talking about something that actually should be pretty simple. You could say "there will be 6 categories". But actually, it's not necessary to fix or predetermine the size of the ArrayList -- so why mumble about this at all?
I guess essentially an array inside of an arraylist.
Use an ArrayList (indirectly) inside of an ArrayList. Lists/ArrayLists are much better to build dynamically, because (unlike an array) they don't have to be pre-sized or grown.
But note that the 'Questions' list, should be held inside of the Category obect.
I am working on adding support for property observations for my open source library droidQuery, however the propertyChange method is not being called in my tests. What do I need to do to get this to work? Here is my code:
ViewObserver.java
package self.philbrown.droidQuery;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import android.view.View;
/**
* Provides Property Change Listening to simulate bindings
* #author Phil Brown
*
*/
public class ViewObserver implements PropertyChangeListener
{
/** The function to call when the interface's method is invoked. */
private Function function;
/**
* Constructor
* #param droidQuery an instance of droidQuery
* #param function the function to call when the value changes. Will include a {#link Observation}
* Object with information about the KVO operation.
*/
public ViewObserver(Function function)
{
this.function = function;
}
#Override
public void propertyChange(PropertyChangeEvent event) //<-- This is never reached!
{
Observation observation = new Observation(event);
function.invoke($.with((View) event.getSource()), observation);
}
/**
* Represents an observation event that occured.
*/
public static class Observation
{
/** The old value prior the this Observation */
public Object oldValue;
/** The new value */
public Object newValue;
/** The name of the property that has changed from {#code oldValue} to {#code newValue}. */
public String property;
/**
* Constructor. Private since it is only used locally.
* #param event
*/
private Observation(PropertyChangeEvent event)
{
oldValue = event.getOldValue();
newValue = event.getNewValue();
property = event.getPropertyName();
}
}
}
Relevant portions of droidQuery.java (the rest available on github):
/** Used for Property Change Listening to simulate KVO Binding in droidQuery */
private static Map<View, Map<String, WeakReference<Observer>>> observers;
//The constructor has this:
//if (observers == null)
// observers = new HashMap<View, Map<String, WeakReference<Observer>>>();
/**
* Observe changes to the given property and respond to modification events. This requires
* a getter and setter method for the given property on the selected views. Passing "*" will
* add the property observer for all of the fields in each selected view. For example:
* <pre>
* $.with(myButton).observe("selected", new Function() {
* #Override
* public void invoke($ droidQuery, Object... params) {
* Observation ob = (Observation) params[0];
* Log.i("$", ob.property + " changed to " + ob.newValue);
* }
* });
* </pre>
* #param property name of the property to observe. If set to "*", all fields will be observed.
* #param onPropertyChanged the Function to call when the given property has changed. The argument
* passed to {#code onPropertyChanged} will be an instance of {#link ViewObserver.Observation},
* and will contain the old value, new value, and the property name. The given instance of droidQuery
* will have the observing view selected.
*/
public $ observe(String property, Function onPropertyChanged)
{
for (View view : views)
{
Map<String, WeakReference<Observer>> kvo = observers.get(view);
if (kvo == null)
{
kvo = new HashMap<String, WeakReference<Observer>>();
}
WeakReference<Observer> ref = kvo.get(property);
if (ref != null && ref.get() != null)
{
if (property.equals("*"))
ref.get().support.removePropertyChangeListener(ref.get().kvo);
else
ref.get().support.removePropertyChangeListener(property, ref.get().kvo);
}
Observer observer = new Observer();
observer.support = new PropertyChangeSupport(view);
observer.kvo = new ViewObserver(onPropertyChanged);
ref = new WeakReference<Observer>(observer);
if (property.equals("*"))
observer.support.addPropertyChangeListener(observer.kvo);
else
observer.support.addPropertyChangeListener(property, observer.kvo);
kvo.put(property, ref);
observers.put(view, kvo);
}
return this;
}
/**
* Contains Objects pertaining to the property change listener for a view in droidQuery.
*/
public static class Observer
{
/** Manages property observers registered to receive events */
public PropertyChangeSupport support;
/** The property observer */
public ViewObserver kvo;
}
I don't really know exactly how to word this but basically I need to get the child class instance of an Actor without assigning it (if that makes since?). Is this possible?
package org.game.world.entity.actor;
import java.util.HashMap;
import java.util.Map;
import org.game.world.entity.Entity;
import org.game.world.entity.actor.npc.NPC;
import org.game.world.entity.actor.player.Player;
import org.game.world.entity.actor.player.PlayerData;
public abstract class Actor extends Entity {
/**
* The type of Actor this Entity should be
* recognized as.
*/
private final ActorType actorType;
/**
* A map of ActionStates, not necessarily 'Attributes'.
*/
private final Map<ActionState, Boolean> actionState = new HashMap<ActionState, Boolean>();
/**
* Constructs a new Actor {#Entity}.
*/
public Actor(ActorType actorType) {
this.actorType = actorType;
actionState.putAll(ActionState.DEFAULT_ACTION_STATES);
}
/**
* Gets the status of a {#Actor} ActionSate.
* #param state The ActionState.
* #return The ActionState flag.
*/
public boolean getActionState(ActionState state) {
return actionState.get(state);
}
/**
* Sets a {#Actor} ActionState flag.
* #param state The ActionState.
* #param flag The flag true:false.
*/
public void setActionState(ActionState state, boolean flag) {
actionState.put(state, flag);
}
/**
* Resets all ActionState's for this Actor.
*/
public void setDefaultActionStates() {
actionState.putAll(ActionState.DEFAULT_ACTION_STATES);
}
/**
* Checks if this Actor is a specific ActorType (i.e NPC)
* #param actorType The ActorType
* #return
*/
public boolean isActorType(ActorType actorType) {
return this.actorType == actorType;
}
/**
* The type of Actor.
*/
public static enum ActorType {
PLAYER,
NPC
}
}
An Actor type.
package org.game.world.entity.actor.player;
import org.game.world.entity.Location;
import org.game.world.entity.actor.Actor;
import org.game.world.entity.actor.SkillLink;
/**
* This class represents a Player {#Actor} in the world.
*
* #author dillusion
*
*/
public class Player extends Actor {
/**
* This Player objects unique set of stored
* data.
*/
private final PlayerData playerData;
/**
* Creates a new Player object in the world.
* #param playerData The set of data unique to this Player.
*/
public Player(PlayerData playerData) {
super(ActorType.PLAYER);
this.playerData = playerData;
}
/**
* Gets the players name.
* #return The name.
*/
public String getName() {
return playerData.name;
}
/**
* Gets the players password.
* #return The password.
*/
public String getPassword() {
return playerData.password;
}
/**
* Gets the players permission level.
* #return The permission.
*/
public Permission getPermission() {
return playerData.permission;
}
/**
* Gets the players SkillLink instance.
* #return The SkillLink.
*/
public SkillLink getSkillLink() {
return playerData.skillLink;
}
#Override
public Location getLocation() {
return playerData.location;
}
#Override
public Location setLocation(Location location) {
return playerData.location = location;
}
}
But let's say I have multiple 'Actors'. I don't want to have to cast if I don't need to.
Sorry if I didn't explain this very well.
I dont know what are you questioning about here - so what i have figured out that you might want to do is to use Player as Actor right? Well that is possible by Java standard and inheritance
Actor temp=new Actor(){//implementing abstract methods if any}
Actor player=new Player(); //that is still fine as Actor is common superclas for player and actor
Player another=(Player)player; // thats just fine after typecasting
////but
another=player; // compile error, type mismatch
another=(Player)temp; // ClassCastException but no compilation error;
But still you can use different Actors and Players as Actors.
My question is: How do I access values from another thread?
I have two .java files, Main.java and TrackHands.java
Main.java
/**
* This is the main class, it is used to start the program. The only use of this
* is to make everything more organized.
*/
package Kinect;
//import processing.core.PApplet;
/**
* #author Tony Nguyen <Tony.Nguyen#HvA.nl>
*
*/
public class Main
{
public static void main(String _args[])
{
Thread trackHands = new Thread(new TrackHands());
trackHands.start();
}
}
TrackHands.java
/*
* This uses the normal Java layout to track the user and prints out the coordinates of the left and right hand
*/
package Kinect;
import SimpleOpenNI.*;
import processing.core.PApplet;
import processing.core.PVector;
/**
* #author Tony Nguyen <Tony.Nguyen#HvA.nl>
* #version 1.0
*/
public class TrackHands extends PApplet implements Runnable
{
private int handLeftX, handLeftY = 0; // Holds the coordinates of the left hand
SimpleOpenNI kinect = new SimpleOpenNI(this); // kinect object
/**
* Constructor Takes no parameters
*/
public TrackHands()
{
}
/**
* run This will be executed when the thread starts
*/
#Override
public void run()
{
IntVector userList = new IntVector(); // Make a vector of ints to store the list of users
PVector leftHand = new PVector(); // Make a vector to store the left hand
PVector convertedLeftHand = new PVector();
kinect.enableDepth();
kinect.enableUser(SimpleOpenNI.SKEL_PROFILE_ALL);
kinect.setMirror(true);
while (true)
{
kinect.update();
kinect.getUsers(userList); // Write the list of detected users into the vector
if (userList.size() > 0) // Checks if a user is found
{
int userId = userList.get(0); // Get first user
if (kinect.isTrackingSkeleton(userId)) // If successfully calibrated
{
kinect.getJointPositionSkeleton(userId,
SimpleOpenNI.SKEL_LEFT_HAND, leftHand); // Put the position of the left hand into that vector
kinect.convertRealWorldToProjective(leftHand,
convertedLeftHand);
this.handLeftX = round(convertedLeftHand.x);
this.handLeftY = round(convertedLeftHand.y);
}
}
}
}
// User-tracking callbacks!
public void onNewUser(int userId)
{
System.out.println("Start pose detection");
kinect.startPoseDetection("Psi", userId);
}
public void onEndCalibration(int userId, boolean successful)
{
if (successful)
{
System.out.println(" User calibrated !!!");
kinect.startTrackingSkeleton(userId);
} else
{
System.out.println(" Failed to calibrate user !!!");
kinect.startPoseDetection("Psi", userId);
}
}
public void onStartPose(String pose, int userId)
{
System.out.println("Started pose for user");
kinect.stopPoseDetection(userId);
kinect.requestCalibrationSkeleton(userId, true);
}
}
I have tried to use a getter and a setter to get the values from TrackHands.java into another thread.
Tried creating objects and passing the values as parameters, but then my program will not use these new values in the run() method.
To get values from TrackHands, use a get method that accesses an instance variable that is set in run()
class TrackHands {
Object output;
public void run() {
while(true) {
output = new Object();
}
}
public Object getOutput() {
return output;
}
}
Pass TrackHands into your consumer object and use it to call get getOutput() method.
Passing values in is a bit trickier, because you might cause race condition. Try something like this
class TrackHands {
Object input = null;
public boolean setInput(Object input) {
if(this.input == null) {
this.input = input;
return true;
} else {
return false;
}
}
}
When your run() method uses input, set it to null so that another thread can pass in another input. Your producer thread will use this loop to pass in input:
public void sendInput(TrackHands th, Object input) {
boolean done = false;
while(!done) {
done = th.setInput(input);
}
}
This will keep trying to pass in input until it succeeds.
setInput uses the synchronized keyword so that only one thread can call this method at once, otherwise you'll get a race condition.
A friend of mine solved my problem.
I want to thank everyone for helping me!
Main.java
/**
* This is the main class, it is used to start the program. The only use of this
* is to make everything more organized.
*/
package Kinect;
//import processing.core.PApplet;
/**
* #author Tony Nguyen <Tony.Nguyen#HvA.nl>
*
*/
public class Main
{
public static void main(String _args[])
{
// PApplet.main(new String[]
// {
// Sensor.class.getName()
// });
ValueStore valueStore = new ValueStore(); // ADDED THIS LINE
Thread trackHands = new Thread(new TrackHands(valueStore)); // ADDED THIS LINE
trackHands.start();
}
}
TrackHands.java
/*
* This uses the normal Java layout to track the user and prints out the coordinates of the left and right hand
*/
package Kinect;
import SimpleOpenNI.*;
import processing.core.PApplet;
import processing.core.PVector;
/**
* #author Tony Nguyen <Tony.Nguyen#HvA.nl>
* #version 1.0
*/
public class TrackHands extends PApplet implements Runnable
{
private int handLeftX, handLeftY, handRightX, handRightY = 0; // Holds the coordinates of the left hand
SimpleOpenNI kinect = new SimpleOpenNI(this); // kinect object
private ValueStore valuesStore; // ADDED THIS LINE
/**
* Constructor Takes no parameters
*/
public TrackHands()
{
}
public TrackHands(ValueStore valuesStore)
{
this.valuesStore = valuesStore;
}
/**
* run This will be executed when the thread starts
*/
#Override
public void run()
{
IntVector userList = new IntVector(); // Make a vector of ints to store the list of users
PVector leftHand = new PVector(); // Make a vector to store the left hand
PVector rightHand = new PVector(); // Make a vector to store the right hand
PVector convertedLeftHand = new PVector(); // Make a vector to store the actual left hand
PVector convertedRightHand = new PVector(); // Make a vector to store the actual right hand
kinect.enableDepth();
kinect.enableUser(SimpleOpenNI.SKEL_PROFILE_ALL);
kinect.setMirror(true);
while (true)
{
kinect.update();
kinect.getUsers(userList); // Write the list of detected users into the vector
if (userList.size() > 0) // Checks if a user is found
{
int userId = userList.get(0); // Get first user
if (kinect.isTrackingSkeleton(userId)) // If successfully calibrated
{
kinect.getJointPositionSkeleton(userId,
SimpleOpenNI.SKEL_LEFT_HAND, leftHand); // Put the position of the left hand into that vector
kinect.getJointPositionSkeleton(userId,
SimpleOpenNI.SKEL_RIGHT_HAND, rightHand); // Put the position of the left hand into that vector
kinect.convertRealWorldToProjective(leftHand,
convertedLeftHand);
kinect.convertRealWorldToProjective(rightHand,
convertedRightHand);
this.handLeftX = round(convertedLeftHand.x);
this.handLeftY = round(convertedLeftHand.y);
this.handRightX = round(convertedRightHand.x);
this.handRightY = round(convertedRightHand.y);
valuesStore.setHandValues(handLeftX, handLeftY, handRightX, handRightY); // ADDED THIS LINE
}
}
}
}
// User-tracking callbacks!
public void onNewUser(int userId)
{
System.out.println("Start pose detection");
kinect.startPoseDetection("Psi", userId);
}
public void onEndCalibration(int userId, boolean successful)
{
if (successful)
{
System.out.println(" User calibrated !!!");
kinect.startTrackingSkeleton(userId);
} else
{
System.out.println(" Failed to calibrate user !!!");
kinect.startPoseDetection("Psi", userId);
}
}
public void onStartPose(String pose, int userId)
{
System.out.println("Started pose for user");
kinect.stopPoseDetection(userId);
kinect.requestCalibrationSkeleton(userId, true);
}
}
Then added a class to store the values so another class can access it.
ValueStore.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Kinect;
/**
*
* #author Tony Nguyen <Tony.Nguyen#HvA.nl>
*/
public class ValueStore
{
private int leftX, leftY, rightX, rightY = 0;
public void setHandValues(int leftX, int leftY, int rightX, int rightY)
{
this.leftX = leftX;
this.leftY = leftY;
this.rightX = rightX;
this.rightY = rightY;
}
public int getLeftX()
{
return this.leftX;
}
}