Got a couple of suggestions, here's a reworked version:
import com.bbn.openmap.event.ProjectionEvent;
import com.bbn.openmap.layer.OMGraphicHandlerLayer;
import com.bbn.openmap.layer.policy.BufferedImageRenderPolicy;
import com.bbn.openmap.omGraphics.OMGraphic;
import com.bbn.openmap.omGraphics.OMGraphicList;
import com.bbn.openmap.omGraphics.OMRect;
import com.bbn.openmap.proj.Projection;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
// import static com.bbn.openmap.omGraphics.OMGraphicConstants.*;
// import com.bbn.openmap.omGraphics.OMGraphicConstants.*;
public class OverlayLayer extends OMGraphicHandlerLayer {
private OMGraphicList omgraphics;
public OverlayLayer() {
//////////// Should move this to another method, let them get
created when they are needed.
// int scale = 5;
// omgraphics = new OMGraphicList();
// //Create 5 degree by 5 degree rectangles
// for(int i=-90; i<90; i+=scale)
// for(int j=-180; j<180; j+=scale)
// {
// OMRect r = new
OMRect((float)i+scale,(float)j,(float)i,(float)j+scale,
OMGraphic.LINETYPE_RHUMB);
// r.setFillPaint(new Color(1.0f, 0.5f, 0.0f,
0.5f));
// omgraphics.add(r);
// }
//Generate all rectangles
// DFD- as a side note, generate is meaningless in the
// constructor, the layer isn't added to a map, doesn't have a
// projection yet. the projection gets added then the layer
// becomes part of the map.
// omgraphics.generate(super.getProjection());
///////////////////////// End of move
// This is OK...
setRenderPolicy(new BufferedImageRenderPolicy());
// DFD- repaint is meaningless here, the layer isn't added to
anything yet.
// repaint();
}
/**
* New method for creating all rects if they are needed the first
* time the method is called, otherwise just return the one you
* have. Let the caller worry about generating the list.
*/
private OMGraphicList getAllRectList() {
if (omgraphics == null) {
int scale = 5;
omgraphics = new OMGraphicList();
//Create 5 degree by 5 degree rectangles
for(int i=-90; i<90; i+=scale)
for(int j=-180; j<180; j+=scale)
{
OMRect r = new
OMRect((float)i+scale,(float)j,(float)i,(float)j+scale,
OMGraphic.LINETYPE_RHUMB);
r.setFillPaint(new Color(1.0f, 0.5f, 0.0f,
0.5f));
omgraphics.add(r);
}
//Generate all rectangles - DFD let the caller deal with it
// omgraphics.generate(super.getProjection());
}
return omgraphics;
}
private OMGraphicList getVisibleRectList()
//This function returns only OMRects that have at least one corner
visible
//on the screen.
{
OMGraphicList toRender = new OMGraphicList();
Projection p = super.getProjection();
if(p==null) return null;
OMGraphicList allRects = getAllRectList();
for(int a=0; a<omgraphics.size(); a++)
{
OMRect omr = (OMRect)omgraphics.getOMGraphicAt(a);
float s = omr.getSouthLat();
float w = omr.getWestLon();
float n = omr.getNorthLat();
float e = omr.getEastLon();
if( isVisible(p,n,e) ||
isVisible(p,n,w) ||
isVisible(p,s,e) ||
isVisible(p,s,w))
{
// DFD - generate it here, while you have it
// with the current projection and prepare is
// calling this method. No additional loop
// needed later.
omr.generate(p);
toRender.add(omr);
}
}
return toRender;
}
public synchronized OMGraphicList prepare()
{
return getVisibleRectList();
// DFD- Generally, you should generate here, but you don't need
to
// in your case - getVisibleList is a perfect alternative to
// generate the OMGraphics as you find them, saving another
// loop through the list.
// ogl.generate(getProjection());
// DFD- The list will get set as a result of returning it from
this method, repaint
// will be called by the super class...
// setList(ogl);
// return ogl;
}
private boolean isVisible(Projection proj, float lat, float lon)
{
//returns true if the point is visible
//From the openmap-users archive
Point p = proj.forward(lat, lon);
return
p.getX() >=0 &&
p.getX() < proj.getWidth() &&
p.getY() >= 0 &&
p.getY() < proj.getHeight();
}
// DFD - Notice you don't need renderDataForProjection(), paint(),
// projectionChanged(). Those are being handled by
// OMGraphicHandlerLayer superclass...
}
On Mar 24, 2004, at 8:47 AM, Sperlongano Brian M NPRI wrote:
>
> Hello,
>
> I'm attempting to use OMGraphicHandlerLayer, and I used the simple
> example of wallpapering the map with 5 degree by 5 degree OMRects. I
> modeled the class after the GraticuleLayer, but I can't seem to get my
> class to display. GraticuleLayer displays, and my class does not.
> I've attached the code below. Any ideas?
>
> Thanks,
> Brian
>
>
> //BEGIN CODE
>
> import com.bbn.openmap.event.ProjectionEvent;
> import com.bbn.openmap.layer.OMGraphicHandlerLayer;
> import com.bbn.openmap.layer.policy.BufferedImageRenderPolicy;
> import com.bbn.openmap.omGraphics.OMGraphicList;
> import com.bbn.openmap.omGraphics.OMRect;
> import com.bbn.openmap.proj.Projection;
> import java.awt.Color;
> import java.awt.Graphics;
> import java.awt.Point;
>
> import static com.bbn.openmap.omGraphics.OMGraphicConstants.*;
>
> public class OverlayLayer extends OMGraphicHandlerLayer
> {
> private OMGraphicList omgraphics;
>
> public OverlayLayer()
> {
> int scale = 5;
> omgraphics = new OMGraphicList();
>
> //Create 5 degree by 5 degree rectangles
> for(int i=-90; i<90; i+=scale)
> for(int j=-180; j<180; j+=scale)
> {
> OMRect r = new
> OMRect((float)i+scale,(float)j,(float)i,(float)j+scale,
> LINETYPE_RHUMB);
> r.setFillPaint(new Color(1.0f, 0.5f, 0.0f, 0.5f));
> omgraphics.add(r);
> }
>
> //Generate all rectangles
> omgraphics.generate(super.getProjection());
> setRenderPolicy(new BufferedImageRenderPolicy());
> repaint();
> }
> public synchronized void renderDataForProjection(Projection proj,
> Graphics g)
> {
> if (proj == null) return;
>
> if (!proj.equals(getProjection()))
> {
> setProjection(proj.makeClone());
> setList(getVisibleRectList());
> }
> paint(g);
> }
>
> private OMGraphicList getVisibleRectList()
> //This function returns only OMRects that have at least one corner
> visible
> //on the screen.
> {
> OMGraphicList toRender = new OMGraphicList();
> Projection p = super.getProjection();
> if(p==null) return null;
>
> for(int a=0; a<omgraphics.size(); a++)
> {
> OMRect omr = (OMRect)omgraphics.getOMGraphicAt(a);
>
> float s = omr.getSouthLat();
> float w = omr.getWestLon();
> float n = omr.getNorthLat();
> float e = omr.getEastLon();
>
> if( isVisible(p,n,e) ||
> isVisible(p,n,w) ||
> isVisible(p,s,e) ||
> isVisible(p,s,w))
> {
> toRender.add(omr);
> }
> }
> return toRender;
> }
>
> public synchronized OMGraphicList prepare()
> {
> OMGraphicList ogl = getVisibleRectList();
> setList(ogl);
> return ogl;
> }
>
> private boolean isVisible(Projection proj, float lat, float lon)
> {
> //returns true if the point is visible
> //From the openmap-users archive
> Point p = proj.forward(lat, lon);
> return
> p.getX() >=0 &&
> p.getX() < proj.getWidth() &&
> p.getY() >= 0 &&
> p.getY() < proj.getHeight();
> }
>
> public void projectionChanged(ProjectionEvent p1)
> {
> Projection proj = setProjection(p1);
> if (proj == null)
> {
> repaint();
> return;
> }
>
> setList(null);
> doPrepare();
> }
> }
> --
> [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"]
>
-- [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 Wed Mar 24 14:46:02 2004
This archive was generated by hypermail 2.1.8 : Thu May 12 2005 - 07:18:38 EDT