NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.
To learn about what's going on, see About the Archive Site.
Hi, Thanks to Steve Emmerson, Bill, and others, I have made some rapid process with VisAD. I'd like to start giving something back by sharing a little toy application that does a 2D line plot with a shape that moves along the curve under the control of a slider. I tried to keep it to a bare minimum. It has certainly been a learning experience for me and hopefully it will help others. OK, I do actually have a couple of questions: 1) Why won't the shape appear till after clicking on the slider? (It worked for me before I "cleaned up" my code. I see the same problem in some Test programs.) 2) Is there a convenient way to center the text shape thingy? ("center=true" seems to apply only to the horizontal) 3) Is my code a bunch of crap or am I catching on to this thing? Any comments welcome. Thanks, Doug -- *----------------------------------------------------------------------* | Doug Lindholm, Software Engineer | E-mail: lind@xxxxxxxx | | Research Applications Program | Phone: 303-497-8374 | | National Center for Atmospheric Research | | | P.O. Box 3000 | There's no place | | Boulder, Colorado 80307-3000 | like $HOME | *----------------------------------------------------------------------* import java.rmi.RemoteException; import java.awt.*; import java.awt.event.*; import javax.swing.*; import visad.*; import visad.java2d.*; import java.lang.Math; import visad.util.VisADSlider; public class Plot2D { public DisplayImpl display; public static void main(String[] args) throws Exception { Plot2D plot = new Plot2D(); } public Plot2D() throws Exception { //--- define types ---// RealType x = new RealType("x",null,null); RealType y = new RealType("y",null,null); RealType shape = new RealType("shape",null,null); RealTupleType pos = new RealTupleType(x,y); RealType time_type = RealType.Time; FunctionType ftype = new FunctionType(x,y); FunctionType ftype2 = new FunctionType(pos,shape); FunctionType time_ftype = new FunctionType(time_type,ftype2); //--- define data implementations ---// int n = 100; FlatField field = new FlatField(ftype, new Integer1DSet(n)); double[][] data = new double[1][n]; for (int i=0;i<n;i++) { data[0][i] = Math.sin(i/10.0); } field.setSamples(data); Set time_set = new Linear1DSet(time_type, 0.0, 100.0, n); FieldImpl time_sequence = new FieldImpl(time_ftype, time_set); float[][] coords = new float[2][1]; Gridded2DSet set2; FlatField field2; double[][] data2 = new double[1][1]; for (int i=0;i<n;i++) { coords[0][0] = 1.0f * i; coords[1][0] = (float) ((Real) field.getSample(i)).getValue(); set2 = new Gridded2DSet(pos, coords, 1,1); field2 = new FlatField(ftype2,set2); field2.setSamples(data2); time_sequence.setSample(i,field2); } //--- map data to display ---// display = new DisplayImplJ2D("display"); display.addMap(new ScalarMap(x, Display.XAxis)); display.addMap(new ScalarMap(y, Display.YAxis)); ScalarMap sm_shape = new ScalarMap(shape, Display.Shape); display.addMap(sm_shape); ScalarMap sm = new ScalarMap(RealType.Time, Display.SelectValue); display.addMap(sm); //--- Shape ---// double[] start = {0.0, 0.0, 0.0}; double[] base = {1.0, 0.0, 0.0}; double[] up = {0.0, 1.0, 0.0}; boolean center = true; VisADLineArray[] shapes = new VisADLineArray[1]; shapes[0] = PlotText.render_label("X", start, base, up, center); ShapeControl shape_control = (ShapeControl) sm_shape.getControl(); shape_control.setShapeSet(new Integer1DSet(1)); shape_control.setShapes(shapes); display.addMap(new ConstantMap(0.1, Display.ShapeScale)); //--- Slider ---// final ValueControl vc = (ValueControl) sm.getControl(); vc.setValue(50.0); //??? final DataReference value_ref = new DataReferenceImpl("value"); VisADSlider slider new VisADSlider("value", 0, 100, 0, 1.0, value_ref, RealType.Generic); CellImpl cell = new CellImpl() { public void doAction() throws VisADException, RemoteException { vc.setValue(((Real) value_ref.getData()).getValue()); } }; cell.addReference(value_ref); //--- add data references to display ---// DataReferenceImpl ref = new DataReferenceImpl("ref"); ref.setData(field); display.addReference(ref, null); DataReferenceImpl ref2 = new DataReferenceImpl("ref2"); ref2.setData(time_sequence); display.addReference(ref2, null); //--- create display window ---// JFrame frame = new JFrame("Simple VisAD Application"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); 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 stuff to window ---// panel.add(display.getComponent()); panel.add(slider); //--- set size and make it visible ---// frame.setSize(300, 300); frame.setVisible(true); } }
visad
archives: