这个问题是GPS coordinates to pixel的继续。
我需要画几个多边形。我可以单独绘制每个多边形,但不能将所有多边形都放在正确的位置。

我从此文件加载有关多边形的信息:
GPS

我有Kraj类,它代表每个多边形。

public class Kraj {

    String name;
    Point2D.Double points[];
    Point2D.Double transPoints[];

    Point2D.Double max;
    Point2D.Double min;

    // polygon
    Path2D.Double polygon;

    ArrayList<Kraj> kraje;

    public Kraj(String name, Point2D.Double body[])
    {
        this.name = name;
        this.body = Arrays.copyOf(body, body.length);
        // calculate a bounding box
        zjistiLimity();
        this.transPoints = new Point2D.Double[points.length];
    }

    private void transformToWindow(int width, int height)
    {
        // convert to window
        double convertX = width / (max.x - min.x);
        double convertY = height / (max.y - min.y);

        // calculate polygon to fit in window with right aspect ratio
        double convert =  convertX > convertY ? convertY : convertX;
        // min = 0, convert to interval <0: infinity> and multiply by convert,
        for (int j = 0; j < points.length; j++) {
            double transX = (points[j].x - min.x) * convert;
            double transY = height - (points[j].y - min.y)  * convert;
            transPoints[j] = new Point2D.Double(transX, transY);
        }

        this.polygon = new Path2D.Double();
        this.polygon.moveTo(transBody[0].x, transBody[0].y);
        for (int i = 1; i < body.length; i++)
            this.polygon.lineTo(transPoints[i].x, transPoints[i].y);
        this.polygon.closePath();
    }

    private void drawKraj(Graphics2D g2, int width, int height) {

        g2.setStroke(new BasicStroke(2));
        g2.fill(polygon);
        // vykreslime obrys
        g2.setColor(Color.black);
        g2.draw(polygon);
    }

    public void draw(Graphics2D g2,
            int contextWidth, int contextHeight)
    {
        // fit to window size
        int sirkaSOdsazenim = contextWidth;
        int vyskaSOdsazenim = contextHeight;

        this.transformujToWindow(sirkaSOdsazenim, vyskaSOdsazenim);

        this.drawKraj(g2, sirkaSOdsazenim, vyskaSOdsazenim);

    }


    /**
     * Set min and max
     */
    private void zjistiLimity() {
        max = new Point2D.Double(-Double.MAX_VALUE, -Double.MAX_VALUE);
        min = new Point2D.Double(Double.MAX_VALUE, Double.MAX_VALUE);
        for(int j = 0; j < 10; j++)
        {
            for (int i = 0; i < body.length; i++)
            {
                if (points[i].getX() < min.getX()) min.x = points[i].getX();
                if (points[i].getY() < min.getY()) min.y = points[i].getY();
                if (points[i].getX() > max.getX())max.x = points[i].getX();
                if (points[i].getY() > max.getY()) max.y = points[i].getY();
            }
        }
    }


使用此代码,我可以绘制适合窗口的多边形。但是我需要绘制所有多边形以适合窗口(计算坐标以创建此地图):



我需要编辑或添加什么?感谢所有答案。

最佳答案

您可以使用以下方法转换整个多边形:

g2.translate(x, y);
g2.draw(polygon);
g2.translate(-x, -y)


为每个多边形确定适当的x / y平移是您需要做的事情。

关于java - 在正确位置绘制多边形,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16040543/

10-12 04:06
查看更多