Re: Heavyweight vs. lightweight

>Some ten days ago, I sent a mail with subject:
>
>newbie: class with display works only one  instance at a time
>
>Referring (in brief) that trying to use several DiplayImpl2D
>in a GUI based on JInternalFrame's I was not able to have
>more than one display appear at a time.
>
>I solved the problem, or at list got around it, by spawning a
>heavyweight Frame for each display. Dod other encounter the
>same problem? Or is this a general characteristic of VisaD,
>to show only a display for each heavyweight container?

Hi Francesco,

I wrote a little test application to investigate this issue,
but I could not duplicate your problem.  I've included the
application below.  It launches three JInternalFrames with a
DisplayImplJ2D in each, and one more JInternalFrame with a
generic (non-VisAD) JComponent inside.

Additionally, it displays a separate JFrame containing two
buttons: "Paint" and "Repaint".  "Paint" calls the paint()
method of the JComponent from each JInternalFrame, while
"Repaint" calls the repaint() method of those JComponents.
You'll notice that the generic JComponent exhibits the same
behavior as the VisAD components, demonstrating that there
is no difference between VisADCanvasJ2D components and
other lightweight Swing components.

Let the list know if you have any further questions or
problems.

-Curtis

------------------
// IFrameTest.java

import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
import visad.*;
import visad.data.gif.GIFForm;
import visad.java2d.*;

public class IFrameTest {

  private static final String[] filename
    {"visad/ss/cut.gif", "visad/ss/copy.gif", "visad/ss/paste.gif"};
  private static final int[] locx = {100, 150, 20, 180};
  private static final int[] locy = {140, 20, 50, 100};

  private static final JComponent[] stuff
    new JComponent[filename.length + 1];

  public static void main(String[] args) throws Exception {
    // set up main UI
    JFrame frame = new JFrame("Frame Manager");
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    JDesktopPane desktop = new JDesktopPane();
    frame.setContentPane(desktop);
    GIFForm loader = new GIFForm();

    // set up each internal frame
    for (int i = 0; i <= filename.length; i++) {
      if (i < filename.length) {
        // set up data set
        DataReferenceImpl ref = new DataReferenceImpl("ref" + i);
        URL url = IFrameTest.class.getResource(filename[i]);
        Data data = loader.open(url);
        DisplayImpl display = new DisplayImplJ2D("display" + i);
        ref.setData(data);
        ScalarMap[] maps = data.getType().guessMaps(false);
        for (int j=0; j<maps.length; j++) display.addMap(maps[j]);
        display.addReference(ref);
        stuff[i] = (JComponent) display.getComponent();
      }
      else {
        stuff[i] = new JComponent() {
          public void paint(Graphics g) {
            Dimension size = getSize();
            int w = size.width;
            int h = size.height;
            g.setColor(Color.black);
            g.drawLine(0, 0, w, h);
            g.drawLine(0, h, w, 0);
            g.setColor(Color.blue);
            g.drawRect(w / 4, h / 4, w / 2, h / 2);
          }
        };
      }
      stuff[i].setPreferredSize(new Dimension(200, 200));

      // set up internal frame
      JInternalFrame internal
        new JInternalFrame("Frame" + i, true, false, true, true);
      JPanel p = new JPanel();
      internal.setContentPane(p);
      internal.setBounds(locx[i], locy[i], 200, 200);
      p.add(stuff[i]);
      internal.setVisible(true);
      desktop.add(internal);
    }

    frame.setBounds(300, 300, 400, 400);
    frame.setVisible(true);

    // add another frame with some buttons
    JFrame controls = new JFrame("Controls");
    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
    controls.setContentPane(p);
    JButton paint = new JButton("Paint");
    paint.setAlignmentX(JButton.CENTER_ALIGNMENT);
    paint.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        for (int i=0; i<stuff.length; i++) {
          Graphics g = stuff[i].getGraphics();
          stuff[i].paint(g);
          g.dispose();
        }
      }
    });
    p.add(paint);
    JButton repaint = new JButton("Repaint");
    repaint.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        for (int i=0; i<stuff.length; i++) stuff[i].repaint();
      }
    });
    repaint.setAlignmentX(JButton.CENTER_ALIGNMENT);
    p.add(repaint);
    controls.setLocation(700, 300);
    controls.pack();
    controls.setVisible(true);
  }

}