Re: [OpenMap Users] How to animate OMGraphic and/or make AnimationTester class running

From: Christophe Ganivet <ganivet_at_cena.fr>
Date: Fri, 01 Jun 2007 15:34:20 +0200

Here is my full source code.
I have 3 classes.
1) the frame
2) the layer
3) the object to be displayed

1) My frame window :

package frame;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

import layer.MovingPlotLayer;

import com.bbn.openmap.LatLonPoint;
import com.bbn.openmap.LayerHandler;
import com.bbn.openmap.MapBean;
import com.bbn.openmap.MapHandler;
import com.bbn.openmap.MultipleSoloMapComponentException;
import com.bbn.openmap.gui.BasicMapPanel;
import com.bbn.openmap.gui.MapPanel;
import com.bbn.openmap.gui.OMToolSet;
import com.bbn.openmap.gui.OpenMapFrame;
import com.bbn.openmap.gui.ToolPanel;

import data.PlotItem;

/**
 * Frame to display OpenMap components.
 */
public final class MapFrame extends OpenMapFrame implements ActionListener {

    //
------------------------------------------------------------------------
    // Attributes
    //
------------------------------------------------------------------------

    /**
     * The "BasicMapPanel"
     */
    private MapPanel m_mapPanel = null;

    /**
     * Default MapHandler for the the BasicMapPanel
     */
    private MapHandler m_mapHandler = null;

    /**
     * The default MapBean that the BasicMapPanel created.
     */
    private MapBean m_mapBean = null;


    private PlotItem m_item = null;
   
    //
------------------------------------------------------------------------
    // Methods
    //
------------------------------------------------------------------------

    /**
     * Constructor
     *
     * _at_param frameTitle
     * Frame title
     */
    public MapFrame() {
        super("Frame", true);
        buildGUI();
       
        m_item = new PlotItem(m_mapBean.getCenter());
        MovingPlotLayer.getLayer().addPlot(m_item);
        MovingPlotLayer.getLayer().createGraphics();
       
        // Call quit when the window's close box is clicked.
        addWindowListener(new WindowAdapter() {
            public void windowClosing(final WindowEvent e) {
                System.exit(0);
            }
        });
    }

    /**
     * Build UI components
     */
    private void buildGUI() {
        setPreferredSize(new Dimension(1024, 768));
        try {
            /*
             * The BasicMapPanel automatically creates many default
components,
             * including the MapBean and the MapHandler. You can extend the
             * BasicMapPanel class if you like to add different
functionality or
             * different types of objects.
             */
            m_mapPanel = new BasicMapPanel();

            // Get the default MapHandler the BasicMapPanel created.
            m_mapHandler = m_mapPanel.getMapHandler();
            m_mapHandler.add(this);

            // Get the default MapBean that the BasicMapPanel created.
            m_mapBean = m_mapPanel.getMapBean();

            // Set the map's center : S-W Of France
            m_mapBean.setCenter(new LatLonPoint(43.0f, 1.5f));

            // Set the map's scale 1:750000
            m_mapBean.setScale(7500000f);
           
            /*
             * Create and add a LayerHandler to the MapHandler. The
LayerHandler
             * manages Layers, whether they are part of the map or not.
             * layer.setVisible(true) will add it to the map. The
LayerHandler
             * has methods to do this, too. The LayerHandler will find the
             * MapBean in the MapHandler.
             */
            m_mapHandler.add(new LayerHandler());
                     
            MovingPlotLayer layer = new MovingPlotLayer();
            layer.setVisible(true);
            m_mapHandler.add(layer);
           
            // Create the directional and zoom control tool
            final OMToolSet omts = new OMToolSet();
            // Create an OpenMap toolbar
            final ToolPanel toolBar = new ToolPanel();
           
            /*
             * Add the ToolPanel and the OMToolSet to the MapHandler. The
             * OpenMapFrame will find the ToolPanel and attach it to the top
             * part of its content pane, and the ToolPanel will find the
             * OMToolSet and add it to itself.
             */
            m_mapHandler.add(omts);
            m_mapHandler.add(toolBar);
           
            // Menus
            createMenuItems();

        } catch (final MultipleSoloMapComponentException msmce) {
            // The MapHandler is only allowed to have one of certain
            // items. These items implement the SoloMapComponent
            // interface. The MapHandler can have a policy that
            // determines what to do when duplicate instances of the
            // same type of object are added - replace or ignore.

            // In this example, this will never happen, since we are
            // controlling that one MapBean, LayerHandler,
            // MouseDelegator, etc is being added to the MapHandler.
        }
    }


    /**
     * Create frame's menus
     */
    private void createMenuItems() {
        // Create main menu bar
        final JMenuBar menuBar = new JMenuBar();

        // First menu
        final JMenu menu = new JMenu("Move");
        menuBar.add(menu);

        // Submenu
        JMenuItem menuItem = new JMenuItem("East");
        menuItem.setMnemonic(KeyEvent.VK_E);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
ActionEvent.CTRL_MASK));
        menuItem.setActionCommand("mnuEast");
        menuItem.addActionListener(this);
        menu.add(menuItem);

        // Submenu
        menuItem = new JMenuItem("West");
        menuItem.setMnemonic(KeyEvent.VK_W);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,
ActionEvent.CTRL_MASK));
        menuItem.setActionCommand("mnuWest");
        menuItem.addActionListener(this);
        menu.add(menuItem);

        // Submenu
        menuItem = new JMenuItem("North");
        menuItem.setMnemonic(KeyEvent.VK_N);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
ActionEvent.CTRL_MASK));
        menuItem.setActionCommand("mnuNorth");
        menuItem.addActionListener(this);
        menu.add(menuItem);
       
        // Submenu
        menuItem = new JMenuItem("South");
        menuItem.setMnemonic(KeyEvent.VK_S);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
ActionEvent.CTRL_MASK));
        menuItem.setActionCommand("mnuSouth");
        menuItem.addActionListener(this);
        menu.add(menuItem);
        setJMenuBar(menuBar);
    }

2) My layer extended class :

package layer;

import java.util.ArrayList;

import com.bbn.openmap.Layer;
import com.bbn.openmap.event.ProjectionEvent;
import com.bbn.openmap.omGraphics.OMGraphic;
import com.bbn.openmap.omGraphics.OMGraphicList;
import com.bbn.openmap.proj.Projection;

import data.PlotItem;

public class MovingPlotLayer extends Layer {

    //
------------------------------------------------------------------------
    // Attributes
    //
------------------------------------------------------------------------

    /** A list of graphics to be painted on the map. */
    private OMGraphicList m_omgraphics = new OMGraphicList();

    /** Only a single instance for this layer */
    private static MovingPlotLayer m_thisLayer = null;

    /** Plots to draw */
    private ArrayList<PlotItem> m_list = new ArrayList<PlotItem>();

    /** Projection used */
    private Projection m_projection = null;

    //
------------------------------------------------------------------------
    // Methods
    //
------------------------------------------------------------------------

    /**
     * Constructor
     */
    public MovingPlotLayer() {
        m_thisLayer = this;
        m_omgraphics = new OMGraphicList();
    }

    /**
     * Gets a reference to this layer object.
     */
    public static MovingPlotLayer getLayer() {
        return m_thisLayer;
    }

    /**
     * Clears and then fills the given OMGraphicList.
     *
     * _at_return the graphics list, after being cleared and filled
     */
    public OMGraphicList createGraphics() {
        m_omgraphics.clear();
        drawElements();
        return m_omgraphics;
    }

    /**
     * Draw plots on layer.
     */
    public void drawElements() {
        for (PlotItem plot : m_list) {
            for (OMGraphic shapeItem : plot.getShapes()) {
                m_omgraphics.addOMGraphic(shapeItem);
            }
        }
    }

    /**
     * Update layer
     */
    public void update() {
        m_omgraphics.generate(m_projection);
    }

    /**
     * Add a new plot
     *
     * _at_param plot
     */
    public void addPlot(PlotItem plot) {
        m_list.add(plot);
    }

    /**
     * Get current projection
     */
    public Projection getProjection() {
        return m_projection;
    }

    //
----------------------------------------------------------------------
    // Layer overrides
    //
----------------------------------------------------------------------

    /**
     * Renders the graphics list. It is important to make this routine
as fast
     * as possible since it is called frequently by Swing, and the User
     * Interface blocks while painting is done.
     */
    public void paint(java.awt.Graphics g) {
        m_omgraphics.render(g);
    }

    //
----------------------------------------------------------------------
    // ProjectionListener interface implementation
    //
----------------------------------------------------------------------

    /**
     * Handler for <code>ProjectionEvent</code>s. This function is invoked
     * when the <code>MapBean</code> projection changes. The graphics are
     * reprojected and then the Layer is repainted.
     * <p>
     *
     * _at_param e
     * the projection event
     */
    public void projectionChanged(ProjectionEvent e) {
        m_projection = e.getProjection();
        m_omgraphics.project(e.getProjection(), true);
        repaint();
    }
}


3) and my displayed object

package data;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import layer.MovingPlotLayer;

import com.bbn.openmap.LatLonPoint;
import com.bbn.openmap.omGraphics.OMCircle;
import com.bbn.openmap.omGraphics.OMGraphic;

public class PlotItem {

    /** Current position in lat,lon */
    private LatLonPoint m_point = null;

    /**
     * List of graphical items for drawing. For now I've only one OMCircle
     */
    private ArrayList<OMGraphic> m_graphicItems = new
ArrayList<OMGraphic>();

    /** Displayed object on map */
    private OMCircle m_grafxPos = null;

    /**
     * Constructor
     *
     * _at_param point
     * lat,lon position
     */
    public PlotItem(LatLonPoint point) {
        m_point = point;
        m_grafxPos = new OMCircle(m_point.getLatitude(),
m_point.getLongitude(), 0.02f);
        m_grafxPos.setFillPaint(Color.black);
        m_graphicItems.add(m_grafxPos);
    }

    /**
     * Draw plot
     */
    public List<OMGraphic> getShapes() {
        return m_graphicItems;
    }

    public void moveNorth() {
        float Y = m_point.getLatitude();
        m_point.setLatitude(Y + 0.05f);
        updateGrafx();
    }

    public void moveSouth() {
        float Y = m_point.getLatitude();
        m_point.setLatitude(Y - 0.05f);
        updateGrafx();
    }

    public void moveEast() {
        float X = m_point.getLongitude();
        m_point.setLatitude(X + 0.05f);
        updateGrafx();
    }

    public void moveWest() {
        float X = m_point.getLongitude();
        m_point.setLatitude(X + 0.05f);
        updateGrafx();
    }

    /**
     * Change coordinates
     */
    private void updateGrafx() {
        m_grafxPos.setCenter(m_point);
        m_grafxPos.regenerate(MovingPlotLayer.getLayer().getProjection());
        MovingPlotLayer.getLayer().update();
    }
}


BR,

Christophe



Don Dietrick a écrit :
> Hi Christophe,
>
> There isn't enough code here to see what your problem is. I can't see
> what gets painted in paint(), or when repaint() is called after
> updateGrafix() is called.
>
> - Don
>
>
>
>
> On Jun 1, 2007, at 9:05 AM, Christophe Ganivet wrote:
>
>> regenerate doesn't seems to be enough too.
>> Here is a part of my code :
>>
>> /* My moving plot item */
>> public class PlotItem {
>>
>> /** Constructor */
>> public PlotItem(LatLonPoint point) {
>> m_point = point;
>> m_grafxPos = new OMCircle (m_point.getLatitude(),
>> m_point.getLongitude(), 0.02f);
>> m_grafxPos.setFillPaint(Color.black);
>> }
>>
>> /** Current position in lat,lon */
>> private LatLonPoint m_point = null;
>>
>> /** Displayed object on map */
>> private OMCircle m_grafxPos = null;
>>
>> /** Move plot to west */
>> public void moveWest() {
>> float X = m_point.getLongitude();
>> m_point.setLatitude(X + 0.05f);
>> updateGrafx();
>> }
>>
>> /** Change coordinates */
>> private void updateGrafx() {
>> m_grafxPos.setCenter(m_point);
>>
>> m_grafxPos.regenerate(MovingPlotLayer.getLayer().getProjection());
>> }
>>
>> [...]
>> }
>>
>> When I make a CTRL-W in my frame, I call plot.moveWest();
>> And the "plot" object need to be redraw onto a Layer.
>>
>> public class MovingPlotLayer extends Layer {
>>
>> /** list of graphics to be painted on the map. */
>> private OMGraphicList m_omgraphics = new OMGraphicList();
>>
>> /** Projection used */
>> private Projection m_projection = null;
>>
>> /** Draw plots on layer. */
>> public void drawElements(OMGraphicList pGraphicList) {
>> m_omgraphics.clear();
>> for (PlotItem plot : m_list) {
>> for (OMGraphic shapeItem : plot.getShapes()) {
>> m_omgraphics.addOMGraphic(shapeItem);
>> shapeItem.generate(m_projection);
>> }
>> }
>> }
>> public void projectionChanged(ProjectionEvent e) {
>> m_projection = e.getProjection();
>> m_omgraphics.project(e.getProjection(), true);
>> repaint();
>> }
>>
>> [...]
>> }
>>
>> Did i miss anything ?
>> Thanx for your help.
>>
>> Christophe
>>
>> Stéphane WASSERHARDT a écrit :
>>> Hi Christophe,
>>>
>>> I think your problem is simply that "repaint" is not enough to
>>> redraw de
>>> circle correctly.
>>> You'll have to regenerate your OMCircle before repainting, by calling
>>> circle.generate(projection).
>>>
>>> If your layer only handles an OMGraphicList, you may want to extend
>>> OMGraphicHandlerLayer instead of Layer.
>>> An OMGraphicHandlerLayer manages an OMGraphicList internally, so
>>> you'll only
>>> have to call the "prepare" method to have all your OMGraphics
>>> regenerated.
>>>
>>> I hope this will help!
>>> Stephane
>>>
>>> -----Message d'origine-----
>>> De : owner-openmap-users_at_bbn.com
>>> [mailto:owner-openmap-users_at_bbn.com] De la
>>> part de Christophe Ganivet
>>> Envoyé : vendredi 1 juin 2007 10:28
>>> À : openmap-users_at_bbn.com
>>> Objet : [OpenMap Users] How to animate OMGraphic and/or make
>>> AnimationTester
>>> class running
>>>
>>> Hi all,
>>>
>>> I'm learning OpenMap for a few weeks and for some "basics" I've no
>>> trouble (displaying static data for instance).
>>> But now I'm trying to move a simple OMCircle (north, south, east and
>>> west) using keyboard and i fail.
>>> My OMcircle doesn't be "refresh" into the layer.
>>> I'm extending the com.bbn.openmap.Layer class with an OMGraphicList
>>> member (i'm copying what i've seen in provided example (Simple2.java
>>> and RouteLayer.java).
>>> When my OMCircle need to be moved, i only update its coordinates and
>>> then call repaint() on the layer.
>>> But this doesn't make my circle to be updated. If i zoom or span the
>>> map it is refresh at the good new coordinates.
>>> I've also seen in mailing list archive that some people are using a
>>> GraphicLoader. I've try to see the AnimationTester class but i don't
>>> succeed make it run in a short sample program.
>>> Can anyone help me ?
>>> Thanx
>>>
>>> Christophe
>>>
>>> --
>>> [To unsubscribe to this list send an email to "majdart_at_bbn.com"
>>> with the following text in the BODY of the message "unsubscribe
>>> openmap-users"]
>>>
>>>
>>>
>>
>> --
>> [To unsubscribe to this list send an email to "majdart_at_bbn.com"
>> with the following text in the BODY of the message "unsubscribe
>> openmap-users"]
>
>
>
> =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> Don Dietrick, dietrick_at_bbn.com
> BBN Technologies, Cambridge, MA
> =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
>
>
> --
> [To unsubscribe to this list send an email to "majdart_at_bbn.com"
> with the following text in the BODY of the message "unsubscribe
> openmap-users"]
>

--
[To unsubscribe to this list send an email to "majdart_at_bbn.com"
with the following text in the BODY of the message "unsubscribe openmap-users"]
Received on Fri Jun 01 2007 - 09:34:50 EDT

This archive was generated by hypermail 2.3.0 : Tue Mar 28 2017 - 23:25:08 EDT