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.

newbie: class with display works only one instance at a time

Hi, 
        I have written DisplaySezioneGeo, a class much like the simpler
2-D display examples. Another class generates some JInternalFrame's, and I 
use method getComponent() in DisplaySezioneGeo to show on a JPanel the
corresponding data. However, it works only at the first instantiation of
DisplaySezioneGeo. I have put the RealType's and FunctionType's as static
because otherwise I got an exception when defining them more than once. 
        Every time I try to use more than one DisplaySezioneGeo at a time,
the additional plots don't appear on Monitor. I get non exception message,
also the JComponent returned by getComponent is not null. 
        I copy the class below, because I don't understand what could be
irrelevant; I hope I do not break the list's netiquette.

Thanks,
Francesco

import visad.*;
import visad.java2d.*;
import java.rmi.RemoteException; 
import java.awt.*; 
import javax.swing.*;  
import java.util.Date; 

public class DisplaySezioneGeo extends Object {
  /*Variabili connesse al resto di IdroMonitor
  */
  protected Stazione sta = null; 

  //aggiornamento piu' recente di questo oggetto
  protected Date ultimoAggiornamento = null; 

  /*variabili interne non VisaD
  */
  //true se l'oggetto puo' fornire il grafico di una vera sezione
  boolean status = false; 

  /*Variabili strettamente VisaD
  */
  //non necessitano dai dati concreti per essere costruiti
  protected static RealType larghezza; 
  protected static RealType altezza; 
  protected static RealType indice; 
  protected static RealTupleType l_a_tuple; 
  protected static FunctionType func_i_tuple; 
  protected static FunctionType func_l_a; 
  protected DisplayImpl display; 
  protected ScalarMap larghezzaMap, altezzaMap; 

  //necessitano dai dati concreti per essere costruiti
  protected Set larghezza_set; 
  protected FlatField vals_ff; 
  protected DataReferenceImpl data_ref; 

  //Le quantita' per i livelli: [3] e' il livello dell'acqua, [0,1,2] sono
nell'ordine gli allarmi
  protected Set[] lvl_set = new Set[4]; 
  protected FlatField[] lvl_ff = new FlatField[4]; 
  protected DataReferenceImpl[] lvl_ref = new DataReferenceImpl[4]; 

  /* Crea l'oggetto in modo da poterlo avere e inizializza
     alcune variabili VisaD indipendenti dai dati
     effettivi. 
  */
  public DisplaySezioneGeo() throws visad.VisADException,
java.rmi.RemoteException
  {
    //Eseguito solo la prima volta
    if(larghezza == null) 
    {
      larghezza = new RealType("larghezza", SI.meter, null); 
      altezza = new RealType("altezza", SI.meter, null); 
      indice = new RealType("indice"); 
      l_a_tuple = new RealTupleType( larghezza, altezza); 
      func_i_tuple = new FunctionType(indice, l_a_tuple); 
      func_l_a = new FunctionType(larghezza, altezza); 
    }

    display = new DisplayImplJ2D("display1"); 

    //Rapporto tra le dimensioni degli assi
    try
    {
      display.getProjectionControl().setAspect(new double[] {1.35, 1.0}); 
    }
    catch (VisADException exc) {}
    catch (RemoteException exc) {}

    GraphicsModeControl dispGMC = (GraphicsModeControl) 
display.getGraphicsModeControl(); 
    dispGMC.setScaleEnable(true); 
    dispGMC.setPointMode(false); 
    dispGMC.setLineWidth(1.5f); 

    DisplayRenderer dispR = (DisplayRendererJ2D) 
display.getDisplayRenderer(); 
    dispR.setBackgroundColor(0.8f, 0.8f, 0.8f); 
    dispR.setCursorColor(0.f, 0.f, 0.f); 

    larghezzaMap = new ScalarMap( larghezza, Display.XAxis ); 
    altezzaMap = new ScalarMap( altezza, Display.YAxis ); 
    display.addMap(larghezzaMap); 
    display.addMap(altezzaMap); 
  }

  //aggiorna i dati da mostrare sulla sezione
  public void aggiorna() 
  {
    if(ultimoAggiornamento == null ||
ultimoAggiornamento.before(sta.ultimaLettura)) 
    {
      /*Il livello dell'acqua: deve essere preparato ad ogni aggiornamento
      */
      double[] dadum
sta.sezione.getIntersezioni(sta.strumenti[sta.inUso.posizione].livello); 
      float[][] p = new float[1][2]; 

      //Tolgo il dato precedente
      try
      {
        if(lvl_ref[3] != null) 
          display.removeReference(lvl_ref[3]); 
      }
      catch(visad.VisADException e) 
      {
        status = false; 
        return; 
      }
      catch(java.rmi.RemoteException e) 
      {
        status = false; 
        return; 
      }

      if(dadum == null) 
      {
        p[0][0] = (float)sta.sezione.xmin; 
        p[0][1] = (float)sta.sezione.xmax; 
      }
      else
      {
        p[0][0] = (float) dadum[0]; 
        p[0][1] = (float) dadum[dadum.length - 1]; 
      }
      try
      {
        lvl_set[3] = new Irregular1DSet(larghezza, p); 
        lvl_ff[3] = new FlatField(func_l_a, lvl_set[3]); 
        p[0][0] = (float)sta.strumenti[sta.inUso.posizione].livello; 
        p[0][1] = (float)sta.strumenti[sta.inUso.posizione].livello; 
        lvl_ff[3].setSamples(p); 
        lvl_ref[3] = new DataReferenceImpl("Livello"); 
        lvl_ref[3].setData( lvl_ff[3] ); 

        ConstantMap[] lvlCMap = { new ConstantMap( 0.f, Display.Red ),
        new ConstantMap( 0.f, Display.Green ),
        new ConstantMap( 1.f, Display.Blue ),
        new ConstantMap( 2.0f, Display.LineWidth )  }; 

      display.addReference( lvl_ref[3], lvlCMap); 
      }
      catch(visad.VisADException e) 
      {
        status = false; 
        return; 
      }
      catch(java.rmi.RemoteException e) 
      {
        status = false; 
        return; 
      }
    }
  }

  //restituisce, se esiste, una JComponent con l'aspetto del grafico della
sezione
  public JComponent getComponent() 
  {
    if(status) 
      return (JComponent)display.getComponent(); 
    else
      return null; 
  }

  public boolean getStatus() 
  {
    return status; 
  }

  // Prepara il display della sezione; a preparare il livello ci pensa
aggiorna() 
  public void setStazione(Stazione s) 
  {
    status = true; 
    if(sta != s) 
    {
      sta = s; 
      if(sta == null) 
      {
        status = false; 
        return; 
      }
      else
      {
        SezioneGeo g = sta.sezione; 
        double[][] punti = g.getPunti(); 
        if(punti == null) 
        {
          status = false; 
          return; 
        }
        else
          if(punti.length == 2 && punti[0].length > 0) 
          {
            try
            {
              //disegna la sezione
              float[][] p = new float[1][punti[0].length]; 
              for(int i=0; i<punti[0].length; i++) 
                p[0][i] = (float)punti[0][i]; 
              larghezza_set = new Irregular1DSet(larghezza, p); 

              vals_ff = new FlatField(func_l_a, larghezza_set); 
              for(int i=0; i<punti[0].length; i++) 
                p[0][i] = (float)punti[1][i]; 
              vals_ff.setSamples(p); 
              data_ref = new DataReferenceImpl("data_ref"); 
              data_ref.setData( vals_ff ); 

              ConstantMap[] sezCMap = { new ConstantMap( 0.4f, Display.Red
),
              new ConstantMap( 0.2f, Display.Green ),
              new ConstantMap( 0.2f, Display.Blue ),
              new ConstantMap( 2.0f, Display.LineWidth )  }; 

              display.addReference( data_ref, sezCMap); 

              //disegna i livelli di allarme
              p = new float[1][2]; 
              //Definizione sintetica dei colori
              float[][] col = {{0.0f, 1.0f, 0.0f}, {1.0f, 1.0f, 0.0f},
{1.0f, 0.0f, 0.0f}}; 
              //ciclo sui livelli di allarme
              for(int k=0; k<3; k++) 
              {
                double[] dadum = g.getIntersezioni(sta.allarmi[k]); 
                p[0][0] = (float)dadum[0]; 
                p[0][1] = (float)dadum[dadum.length-1]; 
                lvl_set[k] = new Irregular1DSet(larghezza, p); 
                lvl_ff[k] = new FlatField(func_l_a, lvl_set[k]); 
                p[0][0] = (float)s.allarmi[k]; 
                p[0][1] = (float)s.allarmi[k]; 
                lvl_ff[k].setSamples(p); 
                lvl_ref[k] = new
DataReferenceImpl("Allarme_ref_"+String.valueOf(k)); 
                lvl_ref[k].setData( lvl_ff[k] ); 

                ConstantMap[] lvlCMap = { new ConstantMap( col[k][0],
Display.Red ),
                new ConstantMap( col[k][1], Display.Green ),
                new ConstantMap( col[k][2], Display.Blue ),
                new ConstantMap( 1.0f, Display.LineWidth )  }; 

                display.addReference( lvl_ref[k], lvlCMap); 
              }

            }
            catch(visad.VisADException e) 
            {
              status = false; 
              System.out.println("Aho! Eccezio2"); 
              return; 
            }
            catch(java.rmi.RemoteException e) 
            {
              status = false; 
              return; 
            }
          }
          else
          {
            status = false; 
            return; 
          }
      }
    }
  }

}



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