// // EtchASketch.java // import visad.*; import visad.util.*; import visad.java3d.*; import visad.bom.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.rmi.*; import java.net.MalformedURLException; public class EtchASketch { /** type 'java EtchASketch' to run */ public static void main(String args[]) throws VisADException, RemoteException { // construct EtchASketch application EtchASketch etch = new EtchASketch(args); // construct arrays to hold display and data DisplayImpl[] displays = new DisplayImpl[1]; DataReference[] refs = new DataReference[1]; // set up data and display for stand-alone etch.setupServer(displays, refs); // set up user interface setupUI(displays, refs); } // constructor does nothing: // all the work is done in setupServer and setupUI public EtchASketch (String args[]) throws VisADException, RemoteException { } /** set up data and display for stand-alone */ void setupServer(DisplayImpl[] displays, DataReference[] refs) throws VisADException, RemoteException { // create RealTypes for the X and Y axes of the drawing space RealType x = new RealType("x"); RealType y = new RealType("y"); // create the RealTupleType for the drawing space RealTupleType xy = new RealTupleType(x, y); // construct Java3D display and mappings DisplayImpl display = new DisplayImplJ3D("display1", new TwoDDisplayRendererJ3D()); // map the data drawing space to the display X and Y axes ScalarMap xmap = new ScalarMap(x, Display.XAxis); ScalarMap ymap = new ScalarMap(y, Display.YAxis); display.addMap(xmap); display.addMap(ymap); // set the details of the linear mappings from data to display axes xmap.setRange(-1.0, 1.0); ymap.setRange(-1.0, 1.0); // turn off bounding box display.getDisplayRenderer().setBoxOn(false); // construct starter UnionSet, containing one invisible curve Gridded2DSet set1 = new Gridded2DSet(xy, new float[][] {{-1000.0f}, {-1000.0f}}, 1); Gridded2DSet[] sets = {set1}; UnionSet set = new UnionSet(xy, sets); // link starter UnionSet to display // // note CurveManipulationRendererJ3D provides // the etch-a-sketch behavior DataReferenceImpl ref = new DataReferenceImpl("set"); ref.setData(set); CurveManipulationRendererJ3D cmrenderer = new CurveManipulationRendererJ3D(0, 0, false); display.addReferences(cmrenderer, ref); // plug data and display in arrays displays[0] = display; refs[0] = ref; } /** set up user interface */ static void setupUI(DisplayImpl[] displays, DataReference[] refs) throws VisADException, RemoteException { // create JFrame (i.e., a window) for display and slider JFrame frame = new JFrame("EtchASketch"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); // create JPanel in JFrame JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.setAlignmentY(JPanel.TOP_ALIGNMENT); panel.setAlignmentX(JPanel.LEFT_ALIGNMENT); frame.getContentPane().add(panel); // add display to JPanel panel.add(displays[0].getComponent()); // create JPanel for delete button JPanel button_panel = new JPanel(); button_panel.setLayout(new BoxLayout(button_panel, BoxLayout.X_AXIS)); button_panel.setAlignmentY(JPanel.TOP_ALIGNMENT); button_panel.setAlignmentX(JPanel.LEFT_ALIGNMENT); // create button for deleting last curve drawn CurveDelete cd = new CurveDelete(refs[0]); JButton del = new JButton("delete last"); del.addActionListener(cd); del.setActionCommand("del"); button_panel.add(del); panel.add(button_panel); // set size of JFrame and make it visible frame.setSize(500, 500); frame.setVisible(true); } } /** ActionListener for delete button */ class CurveDelete implements ActionListener { DataReference ref; CurveDelete(DataReference r) { ref = r; } public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals("del")) { try { // get UnionSet containing set of curves UnionSet set = (UnionSet) ref.getData(); // extract array of SampledSets - one for each curve SampledSet[] sets = set.getSets(); // create new array that deletes last curve SampledSet[] new_sets = new SampledSet[sets.length - 1]; System.arraycopy(sets, 0, new_sets, 0, sets.length - 1); // link new UnionSet, without last curve, to display ref.setData(new UnionSet(set.getType(), new_sets)); } catch (VisADException ex) { } catch (RemoteException ex) { } } } }