java绘制连续折线_java-在JMapViewer中绘制折线

本文介绍了一种在JMapViewer中绘制连续折线的方法,通过创建自定义的MapPolyLine类,继承自MapPolygonImpl,并重写paint方法来实现线条绘制。示例代码展示了如何创建并显示一条包含多个坐标的折线。

您可以创建自己的折线实现.下面是一个基于现有MapPolygonImpl的示例.这很hacky,但JMapViewer中似乎没有添加行的方法.

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Point;

import java.awt.geom.Path2D;

import java.util.ArrayList;

import java.util.List;

import javax.swing.JFrame;

import javax.swing.SwingUtilities;

import org.openstreetmap.gui.jmapviewer.Coordinate;

import org.openstreetmap.gui.jmapviewer.JMapViewer;

import org.openstreetmap.gui.jmapviewer.MapPolygonImpl;

import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;

public class TestMap {

public static class MapPolyLine extends MapPolygonImpl {

public MapPolyLine(List extends ICoordinate> points) {

super(null, null, points);

}

@Override

public void paint(Graphics g, List points) {

Graphics2D g2d = (Graphics2D) g.create();

g2d.setColor(getColor());

g2d.setStroke(getStroke());

Path2D path = buildPath(points);

g2d.draw(path);

g2d.dispose();

}

private Path2D buildPath(List points) {

Path2D path = new Path2D.Double();

if (points != null && points.size() > 0) {

Point firstPoint = points.get(0);

path.moveTo(firstPoint.getX(), firstPoint.getY());

for (Point p : points) {

path.lineTo(p.getX(), p.getY());

}

}

return path;

}

}

private static void createAndShowUI() {

JFrame frame = new JFrame("Demo");

JMapViewer viewer = new JMapViewer();

List coordinates = new ArrayList();

coordinates.add(new Coordinate(50, 10));

coordinates.add(new Coordinate(52, 15));

coordinates.add(new Coordinate(55, 15));

MapPolyLine polyLine = new MapPolyLine(coordinates);

viewer.addMapPolygon(polyLine);

frame.add(viewer);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLocationByPlatform(true);

frame.pack();

frame.setVisible(true);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowUI();

}

});

}

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值