import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import visad.*;
import visad.java3d.DisplayImplJ3D;

public class MemoryTest extends JFrame {

  public MemoryTest() {
    super("Memory test");
    init();
    setSize(500, 500);
    setVisible(true);
  }

  DisplayImplJ3D display;

  public void init() {
    try {

      if (display != null) display.destroy();

      int length = 2000;
      SingletonSet[] singletonSets = new SingletonSet[length];
      RealTupleType type = RealTupleType.SpatialCartesian3DTuple;
      for (int i = 0 ; i < length ; i++) {
        double[] values = new double[] {
          Math.sin(Math.PI*(double)i/200),
          Math.cos(Math.PI*(double)i/200),
          (double)i
        };
        singletonSets[i] = new SingletonSet(new RealTuple(type, values));
      }
      UnionSet set = new UnionSet(singletonSets);
      DataReferenceImpl ref = new DataReferenceImpl("data reference");
      ref.setData(set);

      display = new DisplayImplJ3D("display");
      display.addMap(new ScalarMap(RealType.XAxis, Display.XAxis));
      display.addMap(new ScalarMap(RealType.YAxis, Display.YAxis));
      display.addMap(new ScalarMap(RealType.ZAxis, Display.ZAxis));
      display.addReference(ref);

      JButton redraw = new JButton("redraw");
      redraw.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          init();
          validate();
        }
      });
      getContentPane().removeAll();
      getContentPane().setLayout(new BorderLayout());
      getContentPane().add(display.getComponent(), BorderLayout.CENTER);
      getContentPane().add(redraw, BorderLayout.SOUTH);

    } catch (Exception exc) {
      exc.printStackTrace();
    }
  }

  public static void main(String[] args) {
    MemoryTest memoryTest = new MemoryTest();
  }

}
