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



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import visad.*;
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) {
    JDesktopPane desktop = new JDesktopPane();
    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 =;
        DisplayImpl display = new DisplayImplJ2D("display" + i);
        ScalarMap[] maps = data.getType().guessMaps(false);
        for (int j=0; j<maps.length; j++) display.addMap(maps[j]);
        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.drawLine(0, 0, w, h);
            g.drawLine(0, h, w, 0);
            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.setBounds(locx[i], locy[i], 200, 200);

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

    // add another frame with some buttons
    JFrame controls = new JFrame("Controls");
    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
    JButton paint = new JButton("Paint");
    paint.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        for (int i=0; i<stuff.length; i++) {
          Graphics g = stuff[i].getGraphics();
    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();
    controls.setLocation(700, 300);


  • 2000 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the visad archives: