I'm working with DirectLayer to try and create a tool that I can use to draw boxes on a map that will be used to highlight features.
I'm looking for a way to make features that will resize and move with zoom and pan operations and not cause issues with other layers when they are created.
Right now the biggest issue is that when the DirectLayer draw function is called other layers in my map will render incorrectly or not render at all.
public class drawer extends DirectLayer{
private int x,y,w,h;
private Point center;
private boolean oval;
private Color main;
private Color shade;
private float diameter;
/**
* Empty constructor because there should be no persistent information to this class
* Defaults to a circle with diameter 20 and blue outline and shade
*/
public drawer()
{
diameter = 20;
shade = Color.BLUE;
main = Color.BLUE;
}
/**
* The actual drawing function, should not call this from the client, but has to be public to extend DirectLayer
*/
@Override
public void draw(Graphics2D graphics, MapContent map, MapViewport vp) {
graphics.setColor(main);
if(center == null)
{
if(!oval)
{
graphics.drawRect(x, y, w,h);
graphics.setColor(shade);
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float) .5));
graphics.fillRect(x, y, w,h);
}
else
{
graphics.drawOval(x, y, w, h);
graphics.setColor(shade);
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float) .5));
graphics.fillOval(x, y, w,h);
}
}
else
{
Shape theCircle = new Ellipse2D.Double(center.x - diameter/2, center.y - diameter/2, diameter, diameter);
graphics.draw(theCircle);
graphics.setColor(shade);
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float) .5));
graphics.fill(theCircle);
}
map.addLayer(this);
}
/**
* Draws a circle on the given map using the given graphics option
* @param graphics - graphics object from parentComponent
* @param center - center of circle (diameter is set using setDiameter)
* @param map - map to draw circle on
*/
public void drawCircle(Graphics2D graphics,Point center, MapContent map)
{
this.center = center;
draw(graphics,map,map.getViewport());
}
/**
* Draws a rectangle on the map with the given x and y coordinates and the given width and height on the Map
* @param graphics - graphics object from the parentcomponent
* @param x - Upper left x coordinate
* @param y - upper left y coordinate
* @param w - width
* @param h - height
* @param map - Map to draw on
*/
public void drawRectangle(Graphics2D graphics,int x, int y, int w, int h, MapContent map)
{
center = null;
oval = false;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
draw(graphics,map,map.getViewport());
}
/**
* Draws an oval on the map with the given x and y coordinates and the given width and height on the Map
* @param graphics - graphics object from the parentcomponent
* @param x - Upper left x coordinate
* @param y - upper left y coordinate
* @param w - width
* @param h - height
* @param map - Map to draw on
*/
public void drawOval(Graphics2D graphics,int x, int y, int w, int h, MapContent map)
{
oval = true;
center = null;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
draw(graphics,map,map.getViewport());
}
/**
* used to set outline color for shapes
* @param color - color to outline
*/
public void setGraphicsColor(Color color)
{
main = color;
}
/**
* Used to set color of shading
* @param color - color to shade
*/
public void setShaderColor(Color color)
{
shade = color;
}
/**
* Sets diameter of circles that are drawn using the drawCircle Method
* @param d - Diameter
*/
public void setDiameter(float d)
{
diameter = d;
}
/**
* removes all drawn shapes from the given map
* @param map - the map to remove shapes from
*/
public static void removeDrawings(MapContent map)
{
for(Layer l : map.layers())
{
if(l instanceof drawer)
{
l.preDispose();
map.removeLayer(l);
}
}
}
@Override
public ReferencedEnvelope getBounds() {
// TODO Auto-generated method stub
return null;
}
}
The issue here is that when I call DrawREctangle, when the rectangle is drawn on the screen other layers render incorrectly on the repaint. Also the rectangle does not zoom or pan with the screen as it has no featuresource.
I'm wondering what can I do about these two issues? Is DirectLayer actually what I am looking for or should I be trying to make a Drawable version of FeatureLayer, which just feels like the wrong way to go about this.