// Java 3D Test Program // TuplePanel.java // Copyright (c) 1999 ENDO Yasuyuki // mailto:yasuyuki@javaopen.org // http://www.javaopen.org/j3dbook/index.html import java.awt.Panel; import java.awt.Label; import java.awt.TextField; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.util.Vector; import java.util.Enumeration; import javax.vecmath.Tuple3f; import javax.vecmath.Color3f; import javax.vecmath.Point3f; import javax.vecmath.Vector3f; import javax.vecmath.Tuple4f; import javax.vecmath.Color4f; import javax.vecmath.Point4f; import javax.vecmath.Vector4f; import javax.vecmath.Quat4f; public class TuplePanel extends Panel { public static final int TUPLE_3 = 0; public static final int TUPLE_4 = 1; protected int type = TUPLE_3; protected Label xlabel = new Label("X"); protected Label ylabel = new Label("Y"); protected Label zlabel = new Label("Z"); protected Label wlabel = new Label("W"); protected float x = 0.0f; protected float y = 0.0f; protected float z = 0.0f; protected float w = 0.0f; protected TextField xfield = new TextField(Float.toString(x)); protected TextField yfield = new TextField(Float.toString(y)); protected TextField zfield = new TextField(Float.toString(z)); protected TextField wfield = new TextField(Float.toString(w)); protected Tuple3f tuple3f = null; protected Tuple4f tuple4f = null; protected Vector listeners = new Vector(); public int getType() { return type; } public void setXLabel(String label) { xlabel.setText(label); } public void setYLabel(String label) { ylabel.setText(label); } public void setZLabel(String label) { zlabel.setText(label); } public void setWLabel(String label) { wlabel.setText(label); } public void setLabels(String[] labels) { if (labels.length > 0) setXLabel(labels[0]); if (labels.length > 1) setYLabel(labels[1]); if (labels.length > 2) setZLabel(labels[2]); if (labels.length > 3) setWLabel(labels[3]); } public int getColumns() { return xfield.getColumns(); } public void setColumns(int columns) { xfield.setColumns(columns); yfield.setColumns(columns); zfield.setColumns(columns); wfield.setColumns(columns); } public Tuple3f getTuple3f() { return tuple3f; } public Tuple4f getTuple4f() { return tuple4f; } public void set(Tuple3f tuple) { if (type != TUPLE_3) throw new IllegalArgumentException("Tuple3f needed."); tuple3f.x = x = tuple.x; tuple3f.y = y = tuple.y; tuple3f.z = z = tuple.z; xfield.setText(Float.toString(x)); yfield.setText(Float.toString(y)); zfield.setText(Float.toString(z)); } public void set(Tuple4f tuple) { if (type != TUPLE_4) throw new IllegalArgumentException("Tuple4f needed."); tuple4f.x = x = tuple.x; tuple4f.y = y = tuple.y; tuple4f.z = z = tuple.z; tuple4f.w = w = tuple.w; xfield.setText(Float.toString(x)); yfield.setText(Float.toString(y)); zfield.setText(Float.toString(z)); wfield.setText(Float.toString(w)); } public TuplePanel () {} public TuplePanel (Tuple3f tuple) { init(tuple); } public TuplePanel (Tuple4f tuple) { init(tuple); } public TuplePanel (Color3f color) { init(color); } public TuplePanel (Color4f color) { init(color); } public void init(Tuple3f tuple) { type = TUPLE_3; tuple3f = tuple; set(tuple); init(); } public void init(Tuple4f tuple) { type = TUPLE_4; tuple4f = tuple; set(tuple); init(); } public void init(Color3f color) { String[] labels = {"R", "G", "B"}; setLabels(labels); init( (Tuple3f)color ); } public void init(Color4f color) { String[] labels = {"R", "G", "B", "A"}; setLabels(labels); init( (Tuple4f)color ); } protected void init() { if (this.getComponentCount() > 0) this.removeAll(); this.add(xlabel); xfield.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try { x = Float.parseFloat(e.getActionCommand()); notifyValues(x, y, z, w); } catch (NumberFormatException ex) {} } }); this.add(xfield); this.add(ylabel); yfield.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try { y = Float.parseFloat(e.getActionCommand()); notifyValues(x, y, z, w); } catch (NumberFormatException ex) {} } }); this.add(yfield); this.add(zlabel); zfield.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try { z = Float.parseFloat(e.getActionCommand()); notifyValues(x, y, z, w); } catch (NumberFormatException ex) {} } }); this.add(zfield); if (type == TUPLE_4) { this.add(wlabel); wfield.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { try { w = Float.parseFloat(e.getActionCommand()); notifyValues(x, y, z, w); } catch (NumberFormatException ex) {} } }); this.add(wfield); } } protected void notifyValues(float x, float y, float z, float w) { if (type == TUPLE_3) { tuple3f.x = x; tuple3f.y = y; tuple3f.z = z; fireTupleEvent(tuple3f); } else if (type == TUPLE_4) { tuple4f.x = x; tuple4f.y = y; tuple4f.z = z; tuple4f.w = w; fireTupleEvent(tuple4f); } } public void addTupleEventListener(TupleEventListener l) { listeners.addElement(l); } public void removeTupleEventListener(TupleEventListener l) { listeners.removeElement(l); } protected synchronized void fireTupleEvent(Tuple3f tuple) { if (listeners.size() == 0) return; Vector v = (Vector)listeners.clone(); Enumeration e = v.elements(); while (e.hasMoreElements()) { TupleEventListener l = (TupleEventListener)e.nextElement(); l.tupleStateChanged( new TupleEvent(this, tuple) ); } } protected synchronized void fireTupleEvent(Tuple4f tuple) { if (listeners.size() == 0) return; Vector v = (Vector)listeners.clone(); Enumeration e = v.elements(); while (e.hasMoreElements()) { TupleEventListener l = (TupleEventListener)e.nextElement(); l.tupleStateChanged( new TupleEvent(this, tuple) ); } } }