Hallo allerseits ;-)
Ich hänge derzeit an einem Javaproblem und komm einfach nicht weiter. Wahrscheinlich ist es eine banale Sache, aber ihr kennt das sicherlich....Frust *G*
Also ich habe 4 unterschiedliche Java Dateien. 3 seperate Klassen und eine Mainklasse.
Nun möchte ich die Werte von einer Klasse in der anderen verwenden:
Die Werte double massRightTop, double massLeftTop aus WiiRemoteProxy.java brauche ich bei der CubeController.java Klasse, wo ich einem Key zuweise (ist eine Controller Datei bei jME)
Ich poste euch mal alle 4 Klassen, dann sieht man das vll. genauer.
Main.java:
CubeGameState.java
CubeController.java:
und zum Schluss die WiiRemoteProxy.java (testet ob das balance board verbunden ist)
So ich hoffe ich habe euch nicht mit dem ganzen Code abgeschreckt ;-)
LG Mareikiii
Ich hänge derzeit an einem Javaproblem und komm einfach nicht weiter. Wahrscheinlich ist es eine banale Sache, aber ihr kennt das sicherlich....Frust *G*
Also ich habe 4 unterschiedliche Java Dateien. 3 seperate Klassen und eine Mainklasse.
Nun möchte ich die Werte von einer Klasse in der anderen verwenden:
Code:
public void massInputReceived(BBMassEvent evt)
{
totalmass = evt.getTotalMass();
System.out.println(totalmass);
double massRightTop = evt.getMass(MassConstants.TOP, MassConstants.RIGHT);
double massLeftTop = evt.getMass(MassConstants.TOP, MassConstants.LEFT);
double massRightBottom = evt.getMass(MassConstants.BOTTOM, MassConstants.RIGHT);
double massLeftBottom = evt.getMass(MassConstants.BOTTOM, MassConstants.LEFT);
//if bellow 20kg
if (totalmass < 20)
{// deltas are +/- how much kg in each direction
double horizontalDelta = (massRightTop + massRightBottom) - (massLeftTop + massLeftBottom);
double verticalDelta = (massLeftBottom + massRightBottom) - (massLeftTop + massRightTop);
// we now need to make them relative to mass and work out position
horizontal = (horizontalDelta / totalmass * 400) + 400;
vertical = (verticalDelta / totalmass * 300) + 300;
} else {
horizontal = 400;
vertical = 300;
}
}
Die Werte double massRightTop, double massLeftTop aus WiiRemoteProxy.java brauche ich bei der CubeController.java Klasse, wo ich einem Key zuweise (ist eine Controller Datei bei jME)
Code:
for (CubeAction action : CubeAction.values()) {
manager.addControl(action.name());
}
//bind keys
bindKey(EXIT, KEY_X);
bindKey(UP, HIER BRAUCHE ICH DEN WERT!);
bindKey(DOWN, MassConstants.BOTTOM);
bindKey(LEFT, MassConstants.LEFT);
bindKey(RIGHT, MassConstants.RIGHT);
//bind mouse buttons
bindMouseButton(LEFT, LEFT_BUTTON);
bindMouseButton(RIGHT, RIGHT_BUTTON);
}
Ich poste euch mal alle 4 Klassen, dann sieht man das vll. genauer.
Main.java:
Code:
package gamecontrols;
import wiiremotej.*;
import wiiremotej.event.*;
import com.jmex.game.StandardGame;
import com.jmex.game.state.GameState;
import com.jmex.game.state.GameStateManager;
import gamecontrols.WiiRemoteProxy;
public class Main{
public WiiRemoteProxy remote;
private static StandardGame standardGame;
public static void main(String[]args){
System.setProperty("bluecove.jsr82.psm_minimum_off", "true");
standardGame = new StandardGame("GameControl", StandardGame.GameType.GRAPHICAL, null);
standardGame.start();
GameState cubeState = new CubeGameState();
cubeState.remote = new WiiRemoteProxy(null);
cubeState.remote.findWii();
//System.out.println(MassConstants.TOP);
GameStateManager.getInstance().attachChild(cubeState);
cubeState.setActive(true);
}
}
CubeGameState.java
Code:
package gamecontrols;
import wiiremotej.BalanceBoard;
import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.light.PointLight;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.state.LightState;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jmex.game.state.BasicGameState;
/**
*
* @author Gronau
*/
public class CubeGameState extends BasicGameState {
private static String texture = "jmetest/data/images/Monkey.jpg";
public WiiRemoteProxy remote;
public CubeGameState() {
super("cubeGameState");
final Box box = new Box("MonkeyBox", new Vector3f(0, 0, 0), 5, 5, 5);
//Material: gray
final MaterialState ms = DisplaySystem.getDisplaySystem().getRenderer().createMaterialState();
ms.setEmissive(new ColorRGBA(0.5f, 0.5f, 0.5f, 1));
box.setRenderState(ms);
//Texture: the Monkey
final TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
final Texture t = TextureManager.loadTexture(
BasicGameState.class.getClassLoader().getResource(texture),
Texture.MinificationFilter.BilinearNoMipMaps,
Texture.MagnificationFilter.Bilinear);
ts.setTexture(t);
box.setRenderState(ts);
box.setModelBound(new BoundingBox());
box.updateModelBound();
Node boxNode = new Node("MonkeyBoxNode");
boxNode.attachChild(box);
getRootNode().attachChild(boxNode);
//Spot on!
final PointLight light = new PointLight();
light.setDiffuse(new ColorRGBA(0.75f, 0.75f, 0.75f, 0.75f));
light.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f));
light.setLocation(new Vector3f(100, 100, 100));
light.setEnabled(true);
final LightState lightState = DisplaySystem.getDisplaySystem().getRenderer().createLightState();
lightState.setEnabled(true);
lightState.attach(light);
getRootNode().setRenderState(lightState);
getRootNode().updateRenderState();
//Oh, and don't forget the controller...
getRootNode().addController(new CubeController(boxNode));
}
}
CubeController.java:
Code:
package gamecontrols;
import com.jme.input.controls.GameControl;
import com.jme.input.controls.GameControlManager;
import com.jme.input.controls.binding.KeyboardBinding;
import com.jme.input.controls.binding.MouseButtonBinding;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import static gamecontrols.CubeController.CubeAction.*;
import static com.jme.input.KeyInput.*;
import static com.jme.input.controls.binding.MouseButtonBinding.*;
import java.util.ArrayList;
import java.awt.*;
import javax.swing.*;
import wiiremotej.*;
import wiiremotej.event.*;
import java.lang.Math;
import gamecontrols.WiiRemoteProxy;;
public class CubeController extends Controller{
public WiiRemoteProxy remote;
enum CubeAction {LEFT, RIGHT, UP, DOWN, EXIT};
private final static float SPEED = 2F;
private final Node node;
private final GameControlManager manager;
private float vAngle = 0F;
private float hAngle = 0F;
public CubeController(Node node) {
this.node = node;
this.manager = new GameControlManager();
//create all actions
for (CubeAction action : CubeAction.values()) {
manager.addControl(action.name());
}
//bind keys
bindKey(EXIT, KEY_X);
bindKey(UP, bb_value_up);
bindKey(DOWN, MassConstants.BOTTOM);
bindKey(LEFT, MassConstants.LEFT);
bindKey(RIGHT, MassConstants.RIGHT);
//bind mouse buttons
bindMouseButton(LEFT, LEFT_BUTTON);
bindMouseButton(RIGHT, RIGHT_BUTTON);
}
private void bindKey(CubeAction action, int... keys) {
final GameControl control = manager.getControl(action.name());
for (int key : keys) {
control.addBinding(new KeyboardBinding(key));
}
}
private void bindMouseButton(CubeAction action, int mouseButton) {
final GameControl control = manager.getControl(action.name());
control.addBinding(new MouseButtonBinding(mouseButton));
}
private float value(CubeAction action) {
return manager.getControl(action.name()).getValue();
}
@Override
public void update(float time) {
if (value(EXIT) > 0) {
System.exit(0); //OK, this is just a demo...
}
hAngle += SPEED * time * (value(RIGHT) - value(LEFT));
vAngle += SPEED * time * (value(DOWN) - value(UP));
node.getLocalRotation().fromAngles(vAngle, hAngle, 0f);
}
}
und zum Schluss die WiiRemoteProxy.java (testet ob das balance board verbunden ist)
Code:
package gamecontrols;
import java.util.ArrayList;
import java.awt.*;
import javax.swing.*;
import wiiremotej.*;
import wiiremotej.event.*;
import java.lang.Math;
import com.jme.math.Vector3f;
public class WiiRemoteProxy extends BalanceBoardAdapter implements BalanceBoardListener{
private WiiRemote mcl_remotee;
public Vector3f mcl_wiiTx = new Vector3f();
private BalanceBoard remote;
private static double totalmass = 0;
private static boolean connected = false;
private static double horizontal = 400;
private static double vertical = 300;
public void findWii(){
BalanceBoard remote = null;
try
{
remote = WiiRemoteJ.connectToBalanceBoard("00233199ddc0");
remote.addBalanceBoardListener(this);
//requesting for status
remote.setLEDIlluminated(true);
remote.requestStatus();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public WiiRemoteProxy(BalanceBoard remote)
{
connected = true;
this.remote = remote;
}
public void disconnected()
{
System.out.println("Remote disconnected... Please Wii again.");
System.exit(0);
}
public void buttonInputReceived(BBButtonEvent evt)
{
if(evt.isPressed())
remote.disconnect();
}
public void statusReported(BBStatusEvent evt)
{
//Print out the level of battery
System.out.println(evt.getBatteryLevel());
}
public void massInputReceived(BBMassEvent evt)
{
totalmass = evt.getTotalMass();
System.out.println(totalmass);
double massRightTop = evt.getMass(MassConstants.TOP, MassConstants.RIGHT);
double massLeftTop = evt.getMass(MassConstants.TOP, MassConstants.LEFT);
double massRightBottom = evt.getMass(MassConstants.BOTTOM, MassConstants.RIGHT);
double massLeftBottom = evt.getMass(MassConstants.BOTTOM, MassConstants.LEFT);
//if bellow 20kg
if (totalmass < 20)
{// deltas are +/- how much kg in each direction
double horizontalDelta = (massRightTop + massRightBottom) - (massLeftTop + massLeftBottom);
double verticalDelta = (massLeftBottom + massRightBottom) - (massLeftTop + massRightTop);
// we now need to make them relative to mass and work out position
horizontal = (horizontalDelta / totalmass * 400) + 400;
vertical = (verticalDelta / totalmass * 300) + 300;
} else {
horizontal = 400;
vertical = 300;
}
}
}
So ich hoffe ich habe euch nicht mit dem ganzen Code abgeschreckt ;-)
LG Mareikiii