0

我正在使用 DirectLayer 尝试创建一个工具,我可以使用该工具在地图上绘制用于突出显示功能的框。

我正在寻找一种方法来制作可以通过缩放和平移操作调整大小和移动的功能,并且在创建其他图层时不会导致其他图层出现问题。

现在最大的问题是,当调用 DirectLayer 绘制函数时,我的地图中的其他图层将无法正确渲染或根本不渲染。

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;
}

}

这里的问题是,当我调用 DrawREctangle 时,当在屏幕上绘制矩形时,其他图层在重绘时渲染不正确。此外,矩形不会随屏幕缩放或平移,因为它没有特征源。

我想知道这两个问题我能做些什么?DirectLayer 真的是我正在寻找的东西,还是我应该尝试制作 FeatureLayer 的 Drawable 版本,这感觉就像是错误的处理方式。

4

0 回答 0