Re: Fw: Reload the visad applet of html but can't show

Hi Monica,

It is interesting to see another meteorological service looking VisAD...

Have a read through Sun's tutorial page on applet life cycles if you
haven't already:
http://java.sun.com/docs/books/tutorial/deployment/applet/lifeCycle.html

For example its important that your initialisation be done in the "init"
method (eg creating displays), and cleaned up in the "destroy" method of
the applet.

Further, it looks like there are some tricks to using swing in an
applet. Following the Sun tutorial:
http://java.sun.com/docs/books/tutorial/deployment/applet/containerMethods.html
I created the attached "VisadSimple.java" and a trivial html page
"VisadSimple.html" for running it in appletviewer or a web page. This
simple visad program is a 2D display based on Test72, the Streamline
display example program.

I then found that how well this worked (ie the reloading) depended on my
environment. I found at least 1 environment were it worked every time!
(Running Java 1.4.2 on IBM/AIX and using appletviewer to do the reloading).

Using Firefox/Linux/Java 1.4.2_07 it worked about 2/3s of the time
(showing a blank wireframe display the other third of the time).

There are a number of bug reports which seem to be related to this:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5099332
http://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=52b8202137e5a2bca2b94e78b:YfiG?bug_id=4879990

So, assuming my example code is right, it might require some research to
find an appropriate browser/plugin that works reliably enough for you.

Cheers,

James

PS: For those interested, here's the (crude) procedure I used to
compile/test the code (adding the compiled code to visad.jar):
javac VisadSimple.java
jar -uvf visad.jar VisadSimple*.class
appletviewer VisadSimple.html


monica wrote:

> Hi Santi :
> Thanks you can spent time to answer my question....
> But I have no sense in "implement the method destroy or stop of the
> applet and clean there the current information displayed".
> I try to add void stop() and destroy() methods in my applet, but I
> still don't know write code in those code..
> May I use display.stop() ? display.destroy() ? What code I can use it
> ? It confuse me..
> Even I use these code in my applet like this :
> public void stop()
> {
> System.out.println("stop");
>
> try
> {
> remove(display.getComponent());
> display.removeAllReferences();
> display.clearMaps();
> display = null;
> }
> catch(Exception ex)
> {
> }
> }
> }
> It doesn't help me to solve my big problem... sad :(
> Where I can find resource ? Is any applet example when I reload applet
> could show correct view...?
> Thanks lot~~
> By monica


-- 
James Kelly  
Meteorological Systems Section                    Bureau of Meteorology
PO Box 1289K                                  Melbourne 3001, Australia
Phone: 61-3-9669-4724 Fax: 61-3-9669-4128     Email: J.Kelly@xxxxxxxxxx

/*
 * Java(TM) SE 6 version.
 */

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import visad.*;
import visad.java3d.DisplayImplJ3D;
import visad.java2d.DisplayImplJ2D;
import java.rmi.RemoteException;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


//Since we're adding a Swing component, we now need to
//extend JApplet. We need to be careful to access
//components only on the event-dispatching thread.
public class VisadSimple extends JApplet {

    JFrame jframe;
    DisplayImpl display;
    Component displayComponent;
    DataReferenceImpl dataRef;

    FlatField field;
    RealType[] types;
    RealTupleType earth_location;
    RealType u;
    RealType v;
    RealType[] uvType;
    RealTupleType uvTupleType;
    FunctionType uvFunctionType;


    public void init() {
        //Execute a job on the event-dispatching thread:
        //creating this applet's GUI.
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    try {
                        createGUI();
                    } catch (Exception e) {
                        System.err.println("createGUI/VisAD didn't successfully 
complete");
                    }
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't successfully complete");
        }

        // initializing
    }

    private void createGUI()         
        throws RemoteException, VisADException {

        //Create the display
        display = new DisplayImplJ2D("display");
        // getContentPane().setLayout(new java.awt.GridLayout(1,0));

        //Setup Data to display
        types = new RealType[] {RealType.Latitude, RealType.Longitude};
        earth_location = new RealTupleType(types);
        u = RealType.getRealType("u_component");
        v = RealType.getRealType("v_component");
        uvType = new RealType[] {u, v};
        uvTupleType = new RealTupleType(uvType);
        uvFunctionType = new FunctionType(earth_location, uvTupleType);
        field = FlatField.makeField(uvFunctionType, 32, false);

        //Define how do display data
        display.addMap(new ScalarMap(RealType.Latitude, Display.YAxis));
        display.addMap(new ScalarMap(RealType.Longitude, Display.XAxis));
        ScalarMap xflow = new ScalarMap(u, Display.Flow1X);
        display.addMap(xflow);
        display.addMap(new ScalarMap(v, Display.Flow1Y));

        FlowControl flowControl = (FlowControl) xflow.getControl();
        flowControl.enableStreamlines(true);

        dataRef = new DataReferenceImpl("dataRef");
        dataRef.setData(field);
        display.addReference(dataRef, null);
        displayComponent = display.getComponent();

        //Add the display to the applet.
        getContentPane().setLayout(new BorderLayout());           
        getContentPane().add(displayComponent, BorderLayout.CENTER); 
    }

    public void destroy() {
        // preparing for unloading...
        cleanUp();
    }
    
    private void cleanUp() {
        //Execute a job on the event-dispatching thread:
        //taking the displayComponent out of this applet.
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    remove(displayComponent);
                }
            });
        } catch (Exception e) {
            System.err.println("cleanUp didn't successfully complete");
        }
        display = null;
        displayComponent = null;
    }

    //In the Sun Tutorial, they advise....
    //Invoke this method ONLY from the event-dispatching thread.
    // private void addItem(String newWord) {
        // where "field" is a JTextField for example...
        // String t = field.getText();
        // System.out.println(newWord);
        // field.setText(t + newWord);
    // }
}
  • 2007 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the visad archives: