I realize this is somewhat basic but, I can’t find the answer anywhere.
(And I hope I chose the correct category.)
I have a 3D readout panel for location or rotation (or velocity or rotation velocity). I can enter new numbers and hit Enter - but how do I pass the value back to the display panel on the main panel? Right now I pass in the main panel and refer to that from inside. I don’t like it. Is there another/better way to pass back values?
I have:
locDisplayHor = new Display3DHorizontal(" x=", "y=", "z =", 50f, 50f, 50f, this, 0);
but I can’t add an ActionListener() to it. It’s a panel not a JTextArea (which is inside).
/**
*
* @author Owner
*/
/*
* example
* title
* x= x y= y z= z
*/
package core.utils;
import Ant.Ant;
import PercentageLayout.PercentageLayout;
import com.jme3.app.SimpleApplication;
import com.jme3.math.Vector3f;
import core.AntHill;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.ParseException;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.NumberFormatter;
//import javax.vecmath.*;
import javax.swing.JPanel;
import javax.vecmath.Vector3d;
public class Display3DHorizontal extends Panel implements ActionListener{
public static final int NORMAL = 0;
public static final int DEGREE = 1;
public static final int DISTANCE = 2;
public static final int SN = 2;
public static final int L = 0;
public static final int R = 1;
public static final int LV = 2;
public static final int RV = 3;
JPanel footer;
JLabel xLabel, yLabel, zLabel;
JTextField xText, yText, zText, header;
JTextField jtf;
String xString = "*", yString = "*", zString = "*";
float x = 1f, y = 1f, z = 1f;
int format = 0;
int thing = 0;
//SimpleApplication sa;
Ant currentAnt;
AntHill sa;
DecimalFormat scientificNotationFormat = new DecimalFormat("0.000E00");
DecimalFormat degreeFormat = new DecimalFormat("000.00");
NumberFormatter scientificNotationFormatter = new NumberFormatter(scientificNotationFormat);
NumberFormatter degreeFormatter = new NumberFormatter(degreeFormat);
public Display3DHorizontal(){
}
//public Display3DHorizontal(String xs, String ys, String zs, float x, float y, float z, SimpleApplication sa){
public Display3DHorizontal(String xs, String ys, String zs, float x, float y, float z, SimpleApplication sa, int thing){
this.xString = xs;
this.yString = ys;
this.zString = zs;
this.x = x;
this.y = y;
this.z = z;
this.sa = (AntHill)sa;
//this.jtf = jtf;//tried to pass in the JTextField i wanted to change
this.thing = thing;
//scientificNotationFormatter.setOverwriteMode(true);
//scientificNotationFormatter.setAllowsInvalid(false);
setMinimumSize(new Dimension(50, 50));
setVisible(true);
//setLayout(new PercentageLayout(new int[]{100}, new int[]{16, 16, 16, 16, 16, 16}, true));//h, v
//setLayout(new PercentageLayout(new int[]{100}, new int[]{10, 20, 10, 20, 10, 20}));//h, v
add(xLabel = new JLabel(xString), Integer.toString(0));
add(xText = new JTextField(Float.toString(x)), Integer.toString(1));
xText.addActionListener(this);
add(yLabel = new JLabel(yString), Integer.toString(2));
add(yText = new JTextField(Float.toString(y)), Integer.toString(3));
yText.addActionListener(this);
add(zLabel = new JLabel(zString), Integer.toString(4));
add(zText = new JTextField(Float.toString(z)), Integer.toString(5));
zText.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e){
float nx = 0f;
float ny = 0f;
float nz = 0f;
Vector3f v = new Vector3f();
float[] a = new float[4];
//NOW WHAT DO I DO WITH IT
//How does one instance of this return a location and
//another instance of this return a rotation (euler)
//This works but I think there is a better way.
if (thing == L){
nx = Float.parseFloat(xText.getText());
ny = Float.parseFloat(yText.getText());
nz = Float.parseFloat(zText.getText());
v = new Vector3f(nx, ny, nz);
sa.setCurrentAntLocation(v);
//setmytest();//no scope
}
else if (thing == R){
a[0] = nx;
a[1] = ny;
a[2] = nz;
sa.setCurrentAntRotation(a);
}
}
public void setDisplay(Vector3f p){
if (format == NORMAL){
xText.setText(Double.toString(p.x));
yText.setText(Double.toString(p.y));
zText.setText(Double.toString(p.z));
}
else if (format == DEGREE){
xText.setText(degreeFormat(p.x));
yText.setText(degreeFormat(p.y));
zText.setText(degreeFormat(p.z));
}
else if (format == SN){//to kilometers
xText.setText(scientificNotationFormat(p.x/1000));
yText.setText(scientificNotationFormat(p.y/1000));
zText.setText(scientificNotationFormat(p.z/1000));
}
else
System.out.println("Display3D format code error");
//repaint();//done every cycle anyway = causes backup
}
public void setDisplay(float[] a){
if (format == NORMAL){
xText.setText(Double.toString(a[0]));
yText.setText(Double.toString(a[1]));
zText.setText(Double.toString(a[2]));
}
else if (format == DEGREE){
xText.setText(degreeFormat(a[0]));
yText.setText(degreeFormat(a[1]));
zText.setText(degreeFormat(a[2]));
}
else if (format == SN){//to kilometers
xText.setText(scientificNotationFormat(a[0]/1000));
yText.setText(scientificNotationFormat(a[1]/1000));
zText.setText(scientificNotationFormat(a[2]/1000));
}
else
System.out.println("Display3D format code error");
//repaint();//done every cycle anyway = causes backup
}
public void setDisplay(Vector3d p){
if (format == NORMAL){
xText.setText(Double.toString(p.x));
yText.setText(Double.toString(p.y));
zText.setText(Double.toString(p.z));
}
else if (format == DEGREE){
xText.setText(degreeFormat(p.x));
yText.setText(degreeFormat(p.y));
zText.setText(degreeFormat(p.z));
}
else if (format == SN){//to kilometers
xText.setText(scientificNotationFormat(p.x/1000));
yText.setText(scientificNotationFormat(p.y/1000));
zText.setText(scientificNotationFormat(p.z/1000));
}
else
System.out.println("Display3D format code error");
//repaint();//done every cycle anyway = causes backup
}
public String degreeFormat(double n){
String s = new String("");
try{
s = degreeFormatter.valueToString(n);
}
catch(ParseException e){
s = new String("Display3D.string parse error");
}
return(s);
}
public String scientificNotationFormat(double n){
String s = new String("");
try{
s = scientificNotationFormatter.valueToString(n);
}
catch(ParseException e){
s = new String("string parse error");
}
return(s);
}
public void setFormat(int f){
format = f;
}
}