首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在地图上绘制线条的Graphics2D

在地图上绘制线条的Graphics2D
EN

Stack Overflow用户
提问于 2011-01-28 13:28:19
回答 1查看 1.9K关注 0票数 0

我正在尝试使用坐标在地图上绘制一条路径。我正在尝试使用GeneralPath,但它没有创建一条线,只是在经度/经度坐标处创建了一堆点。我如何连接它们,或者是否有其他我可以使用的东西?不太熟悉Graphics2D..

代码语言:javascript
复制
region.add(new GeoPosition(47.2971, -122.3822));
region.add(new GeoPosition(47.2975, -122.3701));
region.add(new GeoPosition(47.3006, -122.3535));
region.add(new GeoPosition(47.2899, -122.3356));
region.add(new GeoPosition(47.2895, -122.3111));
region.add(new GeoPosition(47.2903, -122.2989));
region.add(new GeoPosition(47.2929, -122.2921));
region.add(new GeoPosition(47.2914, -122.2920));
region.add(new GeoPosition(47.2934, -122.2883));


Painter<JXMapViewer> overlay = new Painter<JXMapViewer>() {
    public void paint(Graphics2D g, JXMapViewer map, int w, int h) {
        g = (Graphics2D) g.create();
        //convert from viewport to world bitmap
        Rectangle rect = map.getViewportBounds();
        g.translate(-rect.x, -rect.y);          
        GeneralPath path = new GeneralPath();
        //Polygon poly = new Polygon();
        for(GeoPosition gp : region) {
            //convert geo to world bitmap pixel
            mapViewer.setZoom(60);
            Point2D pt = map.getTileFactory().geoToPixel(gp, map.getZoom());
            //poly.addPoint((int)pt.getX(),(int)pt.getY());
            path.moveTo((int)pt.getX(),(int)pt.getY());
            path.lineTo((int)pt.getX(),(int)pt.getY());                                                                         
        }           
        //do the drawing
        g.setColor(new Color(255,0,0,100));
        g.fill(path);           
        g.setColor(Color.RED);
        g.draw(path);           
        g.dispose();
    }
EN

回答 1

Stack Overflow用户

发布于 2011-01-28 14:13:28

我相信你只需要一个moveTo(...)方法开始绘制线条。然后执行多个lineTo(...)方法来绘制实际的线。

这是我很久以前在网上找到的一个例子:

代码语言:javascript
复制
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class GraphicPane extends JFrame
{
    public GraphicPane()
    {
        super("polygon");

        this.setSize(600,600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MyPolygon mypolygon = new MyPolygon();
        getContentPane().add(mypolygon);

        this.setVisible(true);

        try
        {
            Thread.sleep(2000);
        }
        catch (InterruptedException e){}

        mypolygon.move();
    }

    class MyPolygon extends JPanel
    {
        Point[] p;
        GeneralPath shape;

        public MyPolygon()
        {
            p=new Point[4];
            p[0]=new Point(10,10);
            p[1]=new Point(100,10);
            p[2]=new Point(170,170);
            p[3]=new Point(80,180);
            shape =new GeneralPath();
        }

        public void move()
        {
            int x=300;
            int y=200;
            p[0].setLocation(x, y);
            shape = new GeneralPath();
            repaint();
        }

        public void paintComponent(Graphics grap)
        {
            super.paintComponent(grap);

            Graphics2D grap2D=(Graphics2D)grap;

            shape.moveTo((float)p[0].getX(),(float)p[0].getY());

            for(int i=1;i<p.length;i++)
            {
                shape.lineTo((float)p[i].getX(),(float)p[i].getY());
            }

            shape.closePath();
            grap2D.setColor(Color.red);
            grap2D.draw(shape);
            grap2D.setColor(Color.blue);
            grap2D.fill(shape);
        }
    }

    public static void main(String[] args)
    {
          GraphicPane graph = new GraphicPane();
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4825149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档