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.
Related
I am starting to develop my skills in JAVA, however I have a doubt.
I'm creating an object in JAVA, created the constructor and so on, then, it asks "Change the AGE_RECENT value from 1 to 3", I initially declared this as final because I never thought it would change, so no SET or GET were created. I am wondering how can I change the value from 1 to 3 in the SET Method.
I have this variable
private static int AGE_RECENT=1;
I did this.
public void setAgeRecent() {
Vehicle.AGE_RECENT = 3;
}
It works if you run the program, it changes the variable's value, however nothing was declared in that method as every SET method.
Just wondering how can I do this. If this is correct, good, if not, thanks for helping!
As someone asked, the code.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tp1;
/**
*
* #author Nelson
*/
public class Vehicle {
/** Variáveis da classe, têm como função **/
private String registration;
private int registrationYear;
private double consumption;
private double autonomy;
private int cilinderCapacity;
/**
* Final variables. They are final because they do not suffer any kind of modification during the project.
* YEAR_OMISSION is 2016 because the currect year is 2016.
* ENVIRONMENTAL_CHARGE_OMISSION is 0.10(10 cents), gave this value because there is nothing to mention the
especific value, hence why I gave 0.10.
* RATING_RECENT = Is a string, just has the text "RECENT" inside.
* RATING_COMTEMPORY - Another string, just with the "Comtempory" text inside.
* RATING_CLASSIC - Yet again another string, with the "Classic" text.
* AGE_RECENT - It is to help to compare if a vehicle is recent or not, it has the value 3.
* AGE_CLASSIC - It is to again help to compare, value is 20.
*/
private static final int YEAR_OMISSION = 2016;
private static final double ENVIRONMENTAL_CHARGE_OMISSION=0.10;
private static final String RATING_RECENT="Recent";
private static final String RATING_CONTEMPORY="Contempory";
private static final String RATING_CLASSIC="Classic";
private static int AGE_RECENT=1;
private static final int AGE_CLASSIC=20;
/**
* Constructor of the object, it has the Registration
* #param registration
* #param registrationYear - The year the vehicle was first registered.
* #param consumption - How many liters the vehicle consumes.
* #param autonomy - How many KMs a vehicle can go without refuelling.
* #param cilinderCapacity - How many Cubic Inches the engine has.
*/
public Vehicle(String registration,int registrationYear, double consumption, double autonomy, int cilinderCapacity) {
this.registration = registration;
this.registrationYear = registrationYear;
this.consumption = consumption;
this.autonomy = autonomy;
this.cilinderCapacity = cilinderCapacity;
}
/**
* Null Constructor, it has no values, they will be attributed in the MAIN Class.
*/
public Vehicle() {
this.registration = "";
this.registrationYear = 0;
this.consumption = 0;
this.autonomy = 0;
this.cilinderCapacity =0;
this.registrationYear = YEAR_OMISSION;
}
/**
* Copy Constructor.
*/
public Vehicle(Vehicle vehicle) {
this.registration = vehicle.getRegistration();
this.registrationYear = vehicle.getRegistrationYear();
this.consumption = vehicle.getConsumption();
this.autonomy = vehicle.getAutonomy();
this.cilinderCapacity = vehicle.getCilinderCapacity();
}
public String getRegistration() {
return registration;
}
public int getRegistrationYear() {
return registrationYear;
}
public double getConsumption() {
return consumption;
}
public double getAutonomy() {
return autonomy;
}
public int getCilinderCapacity() {
return cilinderCapacity;
}
public double getYearRecent() {
return AGE_RECENT;
}
public double getAgeRecent(){
return AGE_RECENT;
}
public void setRegistration(String registration) {
this.registration = registration;
}
public void setRegistrationYear(int registrationYear) {
this.registrationYear = registrationYear;
}
public void setConsumption(double consumption) {
this.consumption = consumption;
}
public void setAutonomy(double autonomy) {
this.autonomy = autonomy;
}
public void setCilinderCapacity(int cilinderCapacity) {
this.cilinderCapacity = cilinderCapacity;
}
public void setAgeRecent() {
Vehicle.AGE_RECENT = 3;
}
/**
* Calculate the age of the vehicle to compare in the vehicleRating method
* #return The year, which is 2016 minus the year the vehicle was first registered.
*/
private int calculateAge(){
return YEAR_OMISSION-this.registrationYear;
}
/**
* Calculate the Circulation Tax.
* #return Returns the value of the Environmental Charge multiplied by the Cilinder Capacity of the vehicle.
*/
public double calculateCirculationTax(){
return ENVIRONMENTAL_CHARGE_OMISSION*cilinderCapacity;
}
/**
* Classify the vehicle based on the age.
* If the result given by the calculateAge method is minor than the AGE_RECENT variable(3), then it will
return "Recent"
* If the result is between Age_RECENT and AGE_CLASSIC(20), then it will say "Contemporary"
* If none of the IFs apply, it will return "Classic".
**/
public static String vehicleRating(Vehicle vehicle) {
if(vehicle.calculateAge() < Vehicle.AGE_RECENT) {
return Vehicle.RATING_RECENT; }
else if ((vehicle.calculateAge()>=Vehicle.AGE_RECENT)&&(vehicle.calculateAge()<=Vehicle.AGE_CLASSIC)){
return Vehicle.RATING_CONTEMPORY;}
else
return Vehicle.RATING_CLASSIC;
}
#Override
public String toString() {
return "Vehicle{" + "registration=" + registration + ", registrationYear=" + registrationYear + ", consumption=" + consumption + ", autonomy=" + autonomy + ", cilinderCapacity=" + cilinderCapacity + '}';
}
}
A setter that takes no arguments is simply a method, not a setter. In order to work as a setter a method must take a parameter that matches the type of the value being set - in your case, that would be int:
public static void setAgeRecent(int age) {
AGE_RECENT = age;
}
Note a few things here:
Since AGE_RECENT is static, setAgeRecent should be static
Since AGE_RECENT and setAgeRecent are static members of the same class Vehicle, you do not need to qualify AGE_RECENT with Vehicle
Now users of your class would be able to call your static setter as follows:
Vehicle.setAgeRecent(3);
A static varible, or class variable, may be used without the need to create an instance of that class. But its value may be changed freely at runtime.
A final variable is not a variable in a true sense, because it's value can't be changed at runtime.
Thus, you may have a set method for a static variable, but never to a final variable.
So for my class I have to use the Greenfoot IDE. And my goal is to:
“the pig eats all the mushrooms currently in the barrel”
The pig will have to call the barrel’s getMushroom() method to find out how many mushrooms the barrel currently stores. The pig can add this amount to its count of mushrooms eaten. Remember, the mushrooms are already gone from the world - this happened when the barrel “stored them.”
However if I try and go into the Pig class and use Barrel.getMushrooms(); it says non-static method getMushrooms() cannot be referenced from a static context.
But when I try using stuff like
Barrel b1 = new Barrel();
b1.getMushrooms();
My counter with shrooms never works out right..
Class Barrel
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
* Write a description of class Barrel here.
*
* #author (your name)
* #version (a version number or a date)
*/
public class Barrel extends Actor
{
private final double SCALE_FACTOR_5 = 1.05;
private final double SCALE_FACTOR_25 = 1.25;
public int mushroomsStored;
private int ns;
public Barrel() {
mushroomsStored = 0;
ns = 0;
}
/**
* Main method of Barrel
*/
public void act() {
followMouse();
storeMushrooms();
reset();
}
/**
* Follows mouse drag-and-drop motion.
*/
public void followMouse() {
if(Greenfoot.mouseDragged(this)) {
MouseInfo mouse = Greenfoot.getMouseInfo();
setLocation(mouse.getX(), mouse.getY());
}
}
/**
* Eats nearby mushrooms when dropped.
* Increases its current image scale by 5% when it eats one mushroom.
* Increases its current image scale by 25% when it eats five mushrooms.
* If this barrel stores more than 10 mushrooms, this barrel has itself removed from
* this world.
*/
public void storeMushrooms() {
if(Greenfoot.mouseDragEnded(this)) {
List<Mushroom> nearby = getObjectsInRange(75, Mushroom.class);
ns = nearby.size();
for(Mushroom m : nearby) {
getWorld().removeObject(m);
mushroomsStored++;
}
if(ns < 5 ) {
GreenfootImage img = getImage();
int width = (int)(img.getWidth() * SCALE_FACTOR_5);
int height = (int)(img.getHeight() * SCALE_FACTOR_5);
img.scale(width, height);
}
if (ns >= 5) {
GreenfootImage img = getImage();
int width = (int)(img.getWidth() * SCALE_FACTOR_25);
int height = (int)(img.getHeight() * SCALE_FACTOR_25);
img.scale(width, height);
}
if(mushroomsStored == 10) {
getWorld().removeObject(this);
}
}
}
/**
* Returns this barrel to its original (x,y) location and its
* original image scale.
*/
public void reset() {
if(Greenfoot.mouseClicked(this)) {
MouseInfo mouse = Greenfoot.getMouseInfo();
if(mouse.getButton() == 3) {
this.setLocation(565, 350);
setImage(new GreenfootImage("barrel.png"));
mushroomsStored = 0;
}
}
}
/**
* Returns how many mushrooms this barrel has stored.
*/
public int getMushrooms() {
return mushroomsStored;
}
/**
* Automatically called by Greenfoot whenever a Barrel object
* is placed in a world. In this assigment, we use it to remember
* the initial state of this barrel - its (x,y) position and its
* original image size.
*/
public void addedToWorld(World world) {
GreenfootImage img = new GreenfootImage("barrel.png");
setImage(img);
final int originalX = getX();
final int originalY = getY();
final int originalWidth = img.getWidth();
final int originalHeight = img.getHeight();
}
}
Class Pig
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Pig here.
*
* #author (your name)
* #version (a version number or a date)
*/
public class Pig extends Actor
{
/** Keeps track of how many mushrooms this pig has eaten. */
private int shrooms;
/**
* Constructs a Pig object and initializes it as having
* eaten no mushrooms.
*/
public Pig() {
shrooms = 0;
}
/**
* Follows the mouse movement and eats mushrooms on mouse clicks.
* Stops the scenario once this pig has eaten at least 15 mushrooms.
*/
public void act()
{
followMouse();
eatMushrooms();
getMS();
}
public void getMS() {
b
}
public void followMouse() {
if (Greenfoot.mouseMoved(null)) {
MouseInfo mouse = Greenfoot.getMouseInfo();
setLocation(mouse.getX(), mouse.getY());
}
}
public void eatMushrooms() {
if (Greenfoot.mouseClicked(null)) {
Mushroom m = (Mushroom) getOneIntersectingObject(Mushroom.class);
if (m != null) {
shrooms++;
getWorld().removeObject(m);
}
}
if (shrooms > 29) {
Greenfoot.stop();
}
}
}
Your pig needs a reference to the barrel class. Typically in Greenfoot, class A gets a reference to class B in one of two ways:
Class A is passed the reference to B on creation. This is typical if B creates A: for example if a gun fires bullets, the gun passes a gun reference to the bullet.
Class A collides with class B, for example your player character runs into a collectable, or an enemy collides with your player.
I'm not certain, but I suspect you fall under case 2, in which case you want to use:
Barrel b = (Barrel)getOneIntersectingObject(Barrel.class);
The b variable will be null when you're not touching a barrel, and otherwise it will be a Barrel object which you can call methods on.
When calling the method with the class name like
Barril.getMushrooms()
Your method should be coupled to your class and not an instance. How would you do that? By making it a static method.
I assume that you want to have an instance method used in this context so the right way to do it is the one you presented :
Barril b = new Barril();
b.getMushrooms();
But think about it. What is the value of mushroomsStored if you just created your object?
... You guessed it. You did not even initialized it so it will return the value by default : 0
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;
}
}
Problem: When a skeleton enters a place, I want all the Players screens updated using this method in my World object:
/**
* Say `text' in the Place `place'. The text will become visible at the
* bottom of the text window of any Players currently watching `place'.
*
* #param place
* The place where the string will be displayed.
* #param text
* The string to be diplayed.
*/
public void sayAtPlace(Place place, String text) {
synchronized (players) {
Iterator<Player> ls = players.iterator();
while (ls.hasNext()) {
Player p = ls.next();
if (p.currentPlace() == place) {
p.say(text);
}
}
}
}
I've got two classes, Person and Player and I want a Person to write to the textarea when the method goTo is called but I can't make the Person object have a proper reference to a Player that has the textarea:
package adventure;
import java.awt.*;
import java.util.*;
/**
* ADT for persons which is completed which subclasses to creating actors with
* specific properties
*/
public class Person {
public Player player = null;
/**
* Name of the Person.
*/
public String name;
/**
* The World which this Person is associated with.
*/
public World world;
/**
* Tells where this Person is at the moment.
*/
public Place where;
/**
* Create Person named `name' in world `world'.
*
* #param world
* The world where the Person is created.
* #param name
* Name of the Person.
* #param app
* An image used to display the Person.
*/
public Person(World world, String name, Image app) {
this.world = world;
this.name = name;
this.appearance = app;
// this.player = player;
where = world.defaultPlace();
where.enter(this);
inventory = Collections.synchronizedCollection(new LinkedList<Thing>());
}
/**
* Go directly, and quietly, to 'place'. That is to say, without posting a
* message that you're doing so.
*
* #param place
* A string referring to the Place to go to.
* #see #goTo(Place)
* #see #go
*/
public void goTo(String place) {
goTo(world.getPlace(place), null);
}
/**
* Go directly, and quietly, to `whereTo'. That is to say, without posting a
* message that you're doing so.
*
* #param whereTo
* The Place to go to. Can be null in which case nothing happens.
* #see #goTo(String)
* #see #go
*/
public void goTo(Place whereTo, Player player) {
if (whereTo != null) {
where.exit(this);
whereTo.enter(this);
// Update any Player's which are viewing place `where'.
world.update(where);
// Record our new position.
where = whereTo;
// Also update Player's here.
world.update(where);
}
System.out.println("player:"+player);
if(player != null){
player.say("A terrifying skeleton warrior appears!");
}
// send a msg which doors are available
Object[] doorNames = whereTo.exits.keySet().toArray();
String s = "";
int i = 1;
for (Object obj : doorNames) {
if (i < doorNames.length) {
s = s + obj.toString().toLowerCase();
if(i<doorNames.length){
s = s+ ",";
}
} if (i == doorNames.length && i > 1) {
s = s + " and " + obj.toString().toLowerCase();
}
if (i == doorNames.length && i == 1) {
s = obj.toString().toLowerCase();
}
++i;
}
if (player != null) {
player.say("There are doors " + s);
}
}
package adventure;
import java.awt.*;
/**
* A Player object enables commands to control a certain person: »you». This
* object is displayed graphically as a control panel where the user both can
* control and follow the course of events
*/
public class Player extends Panel {
private Person me;
private PlaceView placeview;
private Commands commands;
private TextArea textarea;
private static final long serialVersionUID = 100L;
/**
* Creates a new instance of Player, in World w, reflecting Person p.
*
* #param w
* The world in which the Player will play in.
* #param p
* The Person associated by Player.
*/
Player(World w, Person p) {
setLayout(new BorderLayout());
setSize(650, 540);
me = p;
p.player = this;
placeview = new PlaceView(me);
commands = new Commands(me);
textarea = new TextArea("", 10, 60, TextArea.SCROLLBARS_VERTICAL_ONLY);
textarea.append("You are in a dungeon. The horrible shrieks of the undead chill your bones.\n");
textarea.setEditable(false);
add("West", placeview);
add("East", commands);
add("South", textarea);
w.addPlayer(this);
}
/**
* Display a string in the players graphical interface.
*
* #param text
* A string to display.
*/
void say(String text) {
textarea.append(text + '\n');
textarea.repaint();
}
/**
* Returns the Place of the Person associated with the Player.
*
* #return The Place of the Person associated with the Player.
*/
public Place currentPlace() {
return me.where;
}
}
Do you understand what I'm trying to do? The code I want to work is
System.out.println("player:"+player);
if(player != null && this.name.equals("Skeleton")){
player.say("A terrifying skeleton warrior appears!");
}
But the player reference of the Person that is a zombie is not instanciated. The only Person who has a Player object instanciated is the Player that is also a Person.
Can you help me? I also posted the full versions of the Person, Player and World classes here
http://pastebin.com/RJCcr2ph (Person)
http://pastebin.com/eYSh8L9Q (Player)
http://pastebin.com/DKvRvEY8 (World)
If I understand your question well, you want to have 2 classes in which each class has a reference to the other. What you need is to create one of them (let it be Foo), create the other class (let it be Boo) and pass a reference of Foo to it via the constructor, and then set a reference of Boo inside Foo class via setter method.
For example:
public static void main(String[] args)
{
Foo f = new Foo();
Boo b = new Boo(f);
f.setBoo(b);
}
class Foo
{
private Boo b;
public Foo()
{
// ...
}
public void setBoo(Boo b)
{
this.b = b;
}
}
class Boo
{
private Foo f;
public Boo(Foo f)
{
this.f = f;
}
}
Now, Foo has a reference of Boo, and Boo has a reference of Foo :)
First , you should instantiate the Person's member 'player' in the constructor .
this.player = new Player(world, this);
Then , change the code from
public void goTo(String place) {
goTo(world.getPlace(place), null);
}
to
public void goTo(String place) {
goTo(world.getPlace(place), this.player);
}