MapMarkerCircle::paint
调用MapMarkerCircle::paintText
,调用Graphics::drawString
,对控制字符或标记没有特殊含义。从这个例子开始,下面的实现paintText()
在第一条线下面画了第二条线。
我已经更新了示例以建议一种将标记的名称和值关联的方法。它使用 a Map<String, Integer>
,将 aMap.Entry<String, Integer>
作为参数传递给RateCircle
构造函数。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFrame;
import org.openstreetmap.gui.jmapviewer.Coordinate;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.MapMarkerCircle;
import org.openstreetmap.gui.jmapviewer.Style;
/**
* @see https://stackoverflow.com/a/38265252/230513
* @see https://stackoverflow.com/a/33857113/230513
*/
public class RateCircleTest {
private void display() {
JFrame f = new JFrame("London");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMapViewer map = new JMapViewer() {
@Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
};
Coordinate london = new Coordinate(51.5072, -0.1275);
map.setDisplayPosition(london, 16);
Map<String, Integer> rates = new HashMap<>();
rates.put("London", 42);
for (Map.Entry<String, Integer> entry : rates.entrySet()) {
map.addMapMarker(new RateCircle(entry, london));
}
f.add(map);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static class RateCircle extends MapMarkerCircle {
private static final int R = 12;
private Map.Entry<String, Integer> entry;
public RateCircle(Map.Entry<String, Integer> entry, Coordinate coord) {
super(null, "", coord, R, STYLE.FIXED, getDefaultStyle());
this.entry = entry;
Style style = getStyle();
style.setBackColor(Color.cyan);
style.setColor(Color.red);
}
@Override
public void paintText(Graphics g, Point position) {
super.paintText(g, position);
g.drawString(entry.getKey(), position.x + R + 2, position.y + R);
g.drawString(entry.getValue() + " kb/s", position.x + R + 2,
position.y + R + g.getFontMetrics().getHeight());
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new RateCircleTest()::display);
}
}