From 1a68dca420fc432b2267ef786c040759d2ca50e2 Mon Sep 17 00:00:00 2001 From: Phillip Date: Sun, 20 Feb 2011 21:53:53 +0000 Subject: Spelling corrections, added NewToroidMenuItem --- src/tesseract/newmenu/MenuItem.java | 49 +++++----- src/tesseract/newmenu/NewIcosahedronMenuItem.java | 37 +++++--- src/tesseract/newmenu/NewToroidMenuItem.java | 108 ++++++++++++++++++++++ src/tesseract/objects/Toroid.java | 34 +++++-- 4 files changed, 186 insertions(+), 42 deletions(-) create mode 100644 src/tesseract/newmenu/NewToroidMenuItem.java (limited to 'src/tesseract') diff --git a/src/tesseract/newmenu/MenuItem.java b/src/tesseract/newmenu/MenuItem.java index 9f65958..1be137f 100644 --- a/src/tesseract/newmenu/MenuItem.java +++ b/src/tesseract/newmenu/MenuItem.java @@ -3,13 +3,10 @@ package tesseract.newmenu; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; -import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Toolkit; -import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -42,6 +39,11 @@ public abstract class MenuItem extends JMenuItem implements ActionListener { * default position. */ protected static final Vector3f DEFAULT_POSITION = new Vector3f(0, 0, 0); + + /** + * + */ + protected static final float DEFAULT_MASS = 2f; /** * Jframe to hold panel. */ @@ -50,10 +52,7 @@ public abstract class MenuItem extends JMenuItem implements ActionListener { * Jpanel for variable text boxes. */ private JPanel myPanel; - /** - * JTextFields. - */ - private JTextField[] myFields; + /** * Variable name/type mapping. */ @@ -88,17 +87,16 @@ public abstract class MenuItem extends JMenuItem implements ActionListener { /** * Constructor. * @param theWorld world parameter. + * @param theLabel for menu item. */ - public MenuItem(final World theWorld, String theLabel) { + public MenuItem(final World theWorld, final String theLabel) { super(theLabel); myFrame = new JFrame(); myParameters = new HashMap(); myPanel = new JPanel(); - - myFields = new JTextField[myParameters.keySet().size()]; myReadData = new HashMap (); myWorld = theWorld; - myParameters.put("Positon", new Vector3f()); + myParameters.put("Position", new Vector3f()); myParameters.put("Mass", new Float(0f)); addActionListener(this); } @@ -108,17 +106,17 @@ public abstract class MenuItem extends JMenuItem implements ActionListener { */ protected void makePanel() { Set varNames = myParameters.keySet(); - myFields = new JTextField[myParameters.keySet().size()]; myPanel.setLayout(new GridLayout(myParameters.size(), 2)); - int i = 0; for (String s : varNames) { - myFields[i] = new JTextField(); myPanel.add(new JLabel(s)); - myPanel.add(myFields[i]); - myReadData.put(s, myFields[i]); - i++; + myReadData.put(s, new JTextField()); + myPanel.add(myReadData.get(s)); } - //myPanel = new JPanel(new GridLayout(myParameters.keySet().size(), 2)); + myReadData.get("Position").setText( + MenuItem.DEFAULT_POSITION.toString().substring(1, + MenuItem.DEFAULT_POSITION.toString().length() - 1)); + myReadData.get("Mass").setText(((Float) + MenuItem.DEFAULT_MASS).toString()); } /** @@ -164,7 +162,7 @@ public abstract class MenuItem extends JMenuItem implements ActionListener { /** * - * @return + * @return Default Button */ public JCheckBox getDefaultButton() { return myDefaultButton; @@ -172,7 +170,7 @@ public abstract class MenuItem extends JMenuItem implements ActionListener { /** * - * @return + * @return enter button */ public JButton getEnterButton() { return myEnterButton; @@ -180,7 +178,7 @@ public abstract class MenuItem extends JMenuItem implements ActionListener { /** * - * @return + * @return parameter frame */ public JFrame getParamFrame() { return myParamFrame; @@ -194,6 +192,7 @@ public abstract class MenuItem extends JMenuItem implements ActionListener { * @author Jesse Morgan, Steve Bradshaw */ protected Vector3f parseVector(final String input) { + String[] split = input.split(","); float x = Float.parseFloat(split[0]); @@ -203,10 +202,18 @@ public abstract class MenuItem extends JMenuItem implements ActionListener { return new Vector3f(x, y, z); } + /** + * + * @return position from vector. + */ protected Vector3f getPosition() { return (Vector3f) myParameters.get("Position"); } + /** + * + * @return mass. + */ protected float getMass() { return ((Float) myParameters.get("Mass")).floatValue(); } diff --git a/src/tesseract/newmenu/NewIcosahedronMenuItem.java b/src/tesseract/newmenu/NewIcosahedronMenuItem.java index 910b834..4123486 100644 --- a/src/tesseract/newmenu/NewIcosahedronMenuItem.java +++ b/src/tesseract/newmenu/NewIcosahedronMenuItem.java @@ -2,10 +2,6 @@ package tesseract.newmenu; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; import java.util.Set; import javax.swing.JButton; @@ -25,30 +21,42 @@ import tesseract.objects.Icosahedron; * @author Phillip Cardon */ public class NewIcosahedronMenuItem extends MenuItem { - private static final float DEFAULT_MASS = 2f; + + /** + * + */ private static final long serialVersionUID = 1936364496102891064L; //private static Map myParams; - - public NewIcosahedronMenuItem (World theWorld) { + /** + * Constructor. + * @param theWorld to add objects to. + */ + public NewIcosahedronMenuItem(final World theWorld) { super(theWorld, "Icosahedron(NEW)"); buildParams(); - this.makePanel(); + } + /** + * Adds Parameters for user input. + * Sets default text box text. + */ private void buildParams() { myParameters.put("Scale", new Float(0f)); - + this.makePanel(); + myReadData.get("Scale").setText(((Float) + Icosahedron.DEFAULT_SCALE).toString()); } @Override - public void actionPerformed(ActionEvent e) { + public void actionPerformed(final ActionEvent e) { createParameterMenu(); final JCheckBox defaultButton = getDefaultButton(); final JFrame params = getParamFrame(); final JButton enterButton = getEnterButton(); - + defaultButton.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { if (defaultButton.isSelected()) { @@ -72,7 +80,8 @@ public class NewIcosahedronMenuItem extends MenuItem { } if (event.getSource() == enterButton) { - myWorld.addObject(new Icosahedron(getPosition(), getMass(), getScale())); + myWorld.addObject(new Icosahedron( + getPosition(), getMass(), getScale())); params.dispose(); } } @@ -80,6 +89,10 @@ public class NewIcosahedronMenuItem extends MenuItem { } + /** + * Gets the Scale for Icosahedron. + * @return the scale. + */ private float getScale() { return ((Float) myParameters.get("Scale")).floatValue(); } diff --git a/src/tesseract/newmenu/NewToroidMenuItem.java b/src/tesseract/newmenu/NewToroidMenuItem.java new file mode 100644 index 0000000..89b5000 --- /dev/null +++ b/src/tesseract/newmenu/NewToroidMenuItem.java @@ -0,0 +1,108 @@ +package tesseract.newmenu; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Set; + +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JFrame; +import javax.vecmath.Vector3f; + +import tesseract.World; +import tesseract.objects.Icosahedron; +import tesseract.objects.Toroid; + +/** + * NewToroidMenuItem + * + * Defines a menu item to add an Icosahedron to the world. + * Code recycled from TesseractMenuItem by Steve Bradshaw and Jessie Morgan + * + * @author Phillip Cardon + */ +public class NewToroidMenuItem extends MenuItem { + + /** + * + */ + private static final long serialVersionUID = 1936364496102891064L; + //private static Map myParams; + + /** + * Constructor. + * @param theWorld to add objects to. + */ + public NewToroidMenuItem(final World theWorld) { + super(theWorld, "Toroid(NEW)"); + buildParams(); + + + } + + /** + * Adds Parameters for user input. + * Sets default text box text. + */ + private void buildParams() { + myParameters.put("Tube Radius", new Float(0f)); + myParameters.put("Tube Resolution", new Integer(0)); + myParameters.put("Toroid Radius", new Float(0f)); + myParameters.put("Toroid Resolution", new Integer(0)); + this.makePanel(); + myReadData.get("Position").setText( + MenuItem.DEFAULT_POSITION.toString()); + myReadData.get("Mass").setText(((Float) + MenuItem.DEFAULT_MASS).toString()); + myReadData.get("Tube Radius").setText(".06"); + myReadData.get("Tube Resolution").setText("25"); + myReadData.get("Toroid Radius").setText(".08"); + myReadData.get("Toroid Resolution").setText("30"); + } + + @Override + public void actionPerformed(final ActionEvent e) { + createParameterMenu(); + final JCheckBox defaultButton = getDefaultButton(); + final JFrame params = getParamFrame(); + final JButton enterButton = getEnterButton(); + + defaultButton.addActionListener(new ActionListener() { + public void actionPerformed(final ActionEvent e) { + if (defaultButton.isSelected()) { + myWorld.addObject(new Toroid(MenuItem.DEFAULT_POSITION, + MenuItem.DEFAULT_MASS, 0f, .06f, 25, .08f, 30)); + params.dispose(); + } + } + }); + enterButton.addActionListener(new ActionListener() { + public void actionPerformed(final ActionEvent event) { + Set itr = myParameters.keySet(); + for (String s : itr) { + Object o = myParameters.get(s); + String p = myReadData.get(s).getText(); + if (o.getClass().equals(new Float(0f).getClass())) { + myParameters.put(s, new Float(Float.parseFloat(p))); + } else if (o.getClass().equals(new Vector3f().getClass())) { + myParameters.put(s, parseVector(p)); + } else if (o.getClass().equals(new Integer(0).getClass())) { + myParameters.put(s, new Integer(Integer.parseInt(p))); + } + + } + if (event.getSource() == enterButton) { + myWorld.addObject(new Toroid(getPosition(), getMass(), 0f, + ((Float) myParameters.get("Tube Radius")).floatValue(), + ((Integer) myParameters.get("Tube Resolution") + ).intValue(), + ((Float) myParameters.get("Toroid Radius") + ).floatValue(), + ((Integer) myParameters.get("Toroid Resolution") + ).intValue())); + params.dispose(); + } + } + }); + } +} diff --git a/src/tesseract/objects/Toroid.java b/src/tesseract/objects/Toroid.java index 581ce7d..7528129 100644 --- a/src/tesseract/objects/Toroid.java +++ b/src/tesseract/objects/Toroid.java @@ -23,6 +23,17 @@ import com.sun.j3d.utils.geometry.NormalGenerator; * @version 0.9a */ public class Toroid extends PhysicalObject { + + /** + * Float 4. + */ + private static final float INEERTIA_TENSOR_CONSTANT4 = 4f; + + /** + * Float 5. + */ + private static final float INEERTIA_TENSOR_CONSTANT5 = 5f; + /** * @param position starting position. * @param mass of object. @@ -42,13 +53,15 @@ public class Toroid extends PhysicalObject { if (inverseMass != 0) { float a = sliceRadius * sliceRadius; float b = arcRadius * arcRadius; - inverseInertiaTensor.m00 = ((4f * a + 5f * b) / 8f) * mass; - inverseInertiaTensor.m11 = ((4f * a + 5f * b) / 8f) * mass; - inverseInertiaTensor.m22 = (a + (3f/4f) * b) * mass; + inverseInertiaTensor.m00 = ((INEERTIA_TENSOR_CONSTANT4 * a + + INEERTIA_TENSOR_CONSTANT5 * b) + / (INEERTIA_TENSOR_CONSTANT4 * 2)) * mass; + inverseInertiaTensor.m11 = inverseInertiaTensor.m00; + inverseInertiaTensor.m22 = (a + ((INEERTIA_TENSOR_CONSTANT4 + * 2 - INEERTIA_TENSOR_CONSTANT5) + / INEERTIA_TENSOR_CONSTANT4) * b) * mass; inverseInertiaTensor.invert(); } - - } @@ -56,11 +69,14 @@ public class Toroid extends PhysicalObject { * Creates donut. * @param sliceRadius radius of slice "flesh." * @param sliceDivisions resolution of slice "flesh" circles. + * @param scale of toroid (NYI) * @param arcRadius Radius of donut circle * @param arcDivisions resolution of slices on donut. + * @return Shape3D generated. */ - public Shape3D buildToroid(final float scale, final float sliceRadius, final int sliceDivisions, - final float arcRadius, final int arcDivisions) { + public Shape3D buildToroid(final float scale, final float sliceRadius, + final int sliceDivisions, final float arcRadius, + final int arcDivisions) { Point3f[][] coordinates = new Point3f[arcDivisions][sliceDivisions]; final float arcAngle = (float) (Math.PI * 2.0); final float sliceDivisionAngle = 2 * (float) Math.PI / sliceDivisions; @@ -99,8 +115,8 @@ public class Toroid extends PhysicalObject { } IndexedQuadArray geometry = new IndexedQuadArray(arcDivisions - * sliceDivisions, PointArray.COORDINATES, 4 * sliceDivisions - * (arcDivisions - 1)); + * sliceDivisions, PointArray.COORDINATES, + (2 * 2) * sliceDivisions * (arcDivisions - 1)); for (int j = 0; j < arcDivisions; j++) { geometry.setCoordinates(j * sliceDivisions, coordinates[j]); } -- cgit v1.2.3