[OpenMap Users] Potential bug in OMEllipse class

From: Wen Chang <Wen.Chang@drdc-rddc.gc.ca>
Date: Mon Mar 22 2004 - 14:24:18 EST

Hi! Don and other openmappers, I have been using openmap 4.6 version to
display moving objects. I was trying to use OMEllipse as part of my
display. However, everytime I display OMEllipse class, it disappear in
the half of the application, while my timer is running. I suspect that
it's something to do with the generate() method.

here I reproduce the scenario by modifying AnimationTester and GLPoint
which extends OMEllipse instead of OMCircles. By testing those two
classes, you should be able to find the bug. Also, the bug only appears
when swing timer is running.

hope this would help to solve the bugs!

Cheers

Wen

// **********************************************************************
//
// <copyright>
//
// BBN Technologies, a Verizon Company
// 10 Moulton Street
// Cambridge, MA 02138
// (617) 873-8000
//
// Copyright (C) BBNT Solutions LLC. All rights reserved.
//
// </copyright>
// **********************************************************************
//
// $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/graphicLoader/AnimationTester.java,v $
// $RCSfile: AnimationTester.java,v $
// $Revision: 1.3 $
// $Date: 2004/01/26 18:18:07 $
// $Author: dietrick $
//
// **********************************************************************

package com.bbn.openmap.graphicLoader;

import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.*;
import javax.swing.event.*;

import com.bbn.openmap.LatLonPoint;
import com.bbn.openmap.dataAccess.dted.DTEDFrameCache;
import com.bbn.openmap.omGraphics.*;
import com.bbn.openmap.proj.Projection;
import com.bbn.openmap.proj.DrawUtil;
import com.bbn.openmap.tools.drawing.*;
import com.bbn.openmap.tools.terrain.LOSGenerator;
import com.bbn.openmap.util.Debug;

/**
 * The AnimationTester is a simple GraphicLoader that lets you toss a
 * bunch of sprites (circles) up on the map to watch them wiggle, to
 * get a feel of the paint delay of the map. You can add sprites to
 * the map (they get placed randomly), and clear the list, and adjust
 * the length of delay between repaints().
 */
public class AnimationTester extends AbstractGraphicLoader {

    OMGraphicList nodes = new OMGraphicList();
    float factor = 1f;

    public AnimationTester() {
    }

    public void manageGraphics() {
// Debug.message("animationtester", "AnimationTester.manageGraphics");
        Projection p = getProjection();

        Iterator it = nodes.iterator();
        while (it.hasNext()) {
            GLPoint point = (GLPoint) it.next();
            point.moveRandomly(factor);
            point.generate(p);
        }

        OMGraphicHandler receiver = getReceiver();
        if (receiver != null) {
            receiver.setList(nodes);
        }
    }

    public void addNode() {

        float ranLat = (float)(Math.random() * 180) - 90f;
        float ranLon = (float)(Math.random() * 360) - 180f;
//******* this section of codes are modified to test OMEllipse class
        LatLonPoint ll = new LatLonPoint(ranLat, ranLon);
        GLPoint point = new GLPoint(ll, 100, 200);
//******* this section of codes are modified to test OMEllipse class
// GLPoint point = new GLPoint(ranLat, ranLon, 5, true);
        point.setFillPaint(Color.red);
        nodes.add(point);
    }

    public void clearNodes() {
        nodes.clear();
    }

    JPanel panel = null;
    String addCmd = "AddSpriteCommand";
    String clearCmd = "ClearSpriteCommand";
    String timerCmd = "TimerCommand";

    public Component getGUI() {
        if (panel == null) {
            panel = new JPanel(new GridLayout(0, 1));
            
            JPanel buttonBox = new JPanel();
            JButton button = new JButton("Add Sprite");
            button.setActionCommand(addCmd);
            button.addActionListener(this);
            buttonBox.add(button);

            button = new JButton("Clear Sprites");
            button.setActionCommand(clearCmd);
            button.addActionListener(this);
            buttonBox.add(button);
            panel.add(buttonBox);

            JLabel label = new JLabel("Timer interval in seconds:");
            panel.add(label);
            JSlider slider = new JSlider(
                JSlider.HORIZONTAL, 0/*min*/, 50/*max*/, 20/*inital*/);
            java.util.Hashtable dict = new java.util.Hashtable();
            dict.put(new Integer(10), new JLabel("1"));
            dict.put(new Integer(20), new JLabel("2"));
            dict.put(new Integer(30), new JLabel("3"));
            dict.put(new Integer(40), new JLabel("4"));
            dict.put(new Integer(50), new JLabel("5"));
            slider.setLabelTable(dict);
            slider.setPaintLabels(true);
            slider.setMajorTickSpacing(5);
            slider.setPaintTicks(true);
            
            slider.addChangeListener(new ChangeListener() {
                    public void stateChanged(ChangeEvent ce) {
                        JSlider slider2 = (JSlider) ce.getSource();
                        if (slider2.getValueIsAdjusting()) {
                            manageGraphics();
                            float interval = ((float)(slider2.getValue()) + .01f) * 100f;
                            Debug.output("Animation Tester delay set to: " + interval/1000f + " seconds");
                            setUpdateInterval((int)interval);

                        }
                    }
                });
            panel.add(slider);
            
            JCheckBox timerButton = new JCheckBox("Run Timer", getTimer().isRunning());
            timerButton.setActionCommand(TimerCmd);
            timerButton.addActionListener(this);

            panel.add(timerButton);

        }
        return panel;
    }

    public void actionPerformed(ActionEvent ae) {
        String command = ae.getActionCommand();

        if (command == addCmd) {
            addNode();
            if (!getTimer().isRunning()) {
                manageGraphics();
            }
        } else if (command == clearCmd) {
            clearNodes();
            if (!getTimer().isRunning()) {
                manageGraphics();
            }
        } else if (command == TimerCmd) {
            JCheckBox check = (JCheckBox)ae.getSource();
            if (check.isSelected()) {
                manageGraphics();
                getTimer().restart();
            } else {
                getTimer().stop();
            }
        } else {
            manageGraphics();
        }
    }
}

// **********************************************************************
//
// <copyright>
//
// BBN Technologies, a Verizon Company
// 10 Moulton Street
// Cambridge, MA 02138
// (617) 873-8000
//
// Copyright (C) BBNT Solutions LLC. All rights reserved.
//
// </copyright>
// **********************************************************************
//
// $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/graphicLoader/GLPoint.java,v $
// $RCSfile: GLPoint.java,v $
// $Revision: 1.2 $
// $Date: 2004/01/26 18:18:07 $
// $Author: dietrick $
//
// **********************************************************************

package com.bbn.openmap.graphicLoader;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import com.bbn.openmap.Environment;
import com.bbn.openmap.omGraphics.OMPoint;
import com.bbn.openmap.LatLonPoint;
import com.bbn.openmap.proj.*;
import com.bbn.openmap.util.Debug;
import com.bbn.openmap.util.PaletteHelper;
import com.bbn.openmap.omGraphics.OMEllipse;
import com.bbn.openmap.proj.Length;

/**
 * A GLPoint is a location on the map. It can move around randomly
 * if it's not set to be stationary, and will change color if it can
 * see any other point.
 */
public class GLPoint extends OMEllipse//OMPoint
    implements ActionListener, FocusListener {

    public static Color CONNECTED_COLOR = Color.green;
    public static Color DISCONNECTED_COLOR = Color.red;
    protected boolean stationary = true;
    protected String name;
    protected int height = 0;// meters.

/* public GLPoint(float lat, float lon, int radius, boolean isOval) {
        super(lat, lon, radius);
        setOval(isOval);
    }
*/
    protected float lat;
    protected float lon;
    
    public GLPoint(LatLonPoint ll, double majorAxis, double minorAxis){
        super(ll, majorAxis, minorAxis, Length.KM, Math.PI*2);
        lat = ll.getLatitude();
        lon = ll.getLongitude();
    }
    
    public float getLat(){
        return lat;
    }

    public float getLon(){
        return lon;
    }
    
    public void setLat(float lt){
        lat = lt;
    }

    public void setLon(float ln){
        lat = ln;
    }
    
    public void resetConnected() {
        setFillPaint(DISCONNECTED_COLOR);
    }
    
    public void connected(boolean connected) {
        if (connected) {
            setFillPaint(CONNECTED_COLOR);
        }
    }

    protected void moveRandomly(float factor) {
        double hor = Math.random() - .5;
        double vert = Math.random() - .5;

        setLat(getLat() + (float)vert*factor);
        setLon(getLon() + (float)hor*factor);
    }

    public void move(float factor) {
        if (!stationary) {
            moveRandomly(factor);
        }
    }

    public void move(int distance, Length units, float Az) {
        LatLonPoint newLocation = GreatCircle.spherical_between(
            ProjMath.degToRad(getLat()),
            ProjMath.degToRad(getLon()),
            units.toRadians(distance), Az);

        setLat(newLocation.getLatitude());
        setLon(newLocation.getLongitude());
    }

    public void setStationary(boolean set) {
        stationary = set;
        if (movementButton != null) {
            movementButton.setSelected(set);
        }
    }

    public boolean getStationary() {
        return stationary;
    }

    public void setName(String set) {
        name = set;
    }

    public String getName() {
        return name;
    }

    public void setHeight(int h) {
        height = h;
        if (heightField != null) {
            heightField.setText(Integer.toString(h));
        }
        if (Debug.debugging("graphicloader")) {
            Debug.output("GLPoint: " + getName() +
                         " setting height to : " + h);
        }
    }

    public int getHeight() {
        return height;
    }

    protected transient java.awt.Container palette = null;

    /**
     * Make the palette visible. Will automatically determine if
     * we're running in an applet environment and will use a
     * JInternalFrame over a JFrame if necessary.
     */
    public void showPalette() {
        if (Environment.getBoolean(Environment.UseInternalFrames)){

            final JLayeredPane desktop =
                Environment.getInternalFrameDesktop();

            // get the window
            palette = PaletteHelper.getPaletteInternalWindow(
                getGUI(),
                getName(),
                new InternalFrameAdapter() {
                        public void internalFrameClosed(InternalFrameEvent e) {
                            if (desktop != null) {
                                desktop.remove((JInternalFrame)palette);
                                desktop.repaint();
                            }
                            palette = null;
                        };
                    });
            // add the window to the desktop
            if (desktop != null) {
                desktop.add(palette);
                palette.setVisible(true);
            }
        } else {
            if (palette == null) {
                palette = PaletteHelper.getPaletteWindow(
                    getGUI(),
                    getName(),
                    new ComponentAdapter(){
                            public void componentHidden(ComponentEvent e){
// firePaletteEvent(false);
                            };
                        } );
            }
            palette.setVisible(true);
            ((JFrame)palette).setState(java.awt.Frame.NORMAL);
        }
    }
    
    /**
     * Hide the GLPoint's palette.
     */
    public void hidePalette() {
        if (palette == null) {
            return;
        }

        if (Environment.getBoolean(Environment.UseInternalFrames)){
            // close the palette
            try {
                ((JInternalFrame)palette).setClosed(true);
            } catch (java.beans.PropertyVetoException evt) {
                com.bbn.openmap.util.Assert.assertExp(
                    false, "GLPoint.hidePalette(): internal error!");
            }
        } else {
            palette.setVisible(false);
        }
    }

    JCheckBox movementButton = null;
    JTextField heightField = null;

    /**
     * Gets the gui controls associated with the GLPoint.
     * This default implementation returns null indicating
     * that the GLPoint has no gui controls.
     *
     * @return java.awt.Component or null
     */
    public java.awt.Component getGUI() {
        JPanel panel = new JPanel(new GridLayout(0, 1));

        // Only want to do this once...
        if (movementButton == null) {
            movementButton = new JCheckBox("Stationary", getStationary());
            movementButton.addActionListener(this);
            movementButton.setActionCommand(MoveCmd);
        }

        panel.add(movementButton);

        JPanel heightPanel = new JPanel(new GridLayout(0, 3));

        heightPanel.add(new JLabel("Object height: "));
        if (heightField == null) {
            heightField = new JTextField(Integer.toString(height), 10);
            heightField.setHorizontalAlignment(JTextField.RIGHT);
            heightField.addActionListener(this);
            heightField.addFocusListener(this);
        }
        heightPanel.add(heightField);
        heightPanel.add(new JLabel(" meters"));
        
        panel.add(heightPanel);

        return panel;
    }

    public final static String MoveCmd = "MoveCommand";

    public void actionPerformed(java.awt.event.ActionEvent ae){
        String cmd = ae.getActionCommand();
        if (cmd == MoveCmd) {
            JCheckBox check = (JCheckBox)ae.getSource();
            setStationary(check.isSelected());
        } else {
            try {
                setHeight(Integer.parseInt(cmd));
            } catch (NumberFormatException nfe) {
                setHeight(getHeight());
            }
        }
    }

    public void focusGained(FocusEvent e) {}
    public void focusLost(FocusEvent e) {
        try {
            setHeight(Integer.parseInt(((JTextField)(e.getSource())).getText()));
        } catch (NumberFormatException nfe) {
            setHeight(0);
        }
    }

}

--
[To unsubscribe to this list send an email to "majdart@bbn.com"
with the following text in the BODY of the message "unsubscribe openmap-users"]
Received on Mon Mar 22 14:27:07 2004

This archive was generated by hypermail 2.1.8 : Thu May 12 2005 - 07:18:38 EDT