我想创建一个多边形onMouseMove()
事件。
这是我尝试过的:
public DrawingListners(JMapPane mappane) {
this._Pane = mappane;
this._Pane.addMouseListener(new MapMouseAdapter() {
@Override
public void onMouseClicked(MapMouseEvent ev) {
if (ev.getClickCount() == 2 && _Drawing == Drawings.Polygon) {
CreateFeatureCollection();
addPolygonToFC(poly);
for (int i = 0; i < nNumbers; i++) {
System.out.println(poly.xPoints[i] + ":" + poly.yPoints[i]);
}
notifyParent.firePropertyChange(CallType.Drawing.toString(), null, null);
dragged = false;
graphics.dispose();
graphics = null;
startPos = null;
poly = null;
nNumbers = 0;
}
}
@Override
public void onMousePressed(MapMouseEvent ev) {
if (enabled) {
startPos = new Point(ev.getPoint());
if (_Drawing != Drawings.Marker)
dragged = true;
if (poly == null) {
poly = new pMapPolygon();
}
poly.addPoint(startPos.x, startPos.y, nNumbers);
nNumbers++;
}
}
@Override
public void onMouseMoved(MapMouseEvent ev) {
if (enabled && poly != null) {
ensureGraphics();
if (_Drawing == Drawings.Polygon) {
poly.addPoint(ev.getPoint().x, ev.getPoint().y, nNumbers);
//graphics.drawPolygon(poly.xPoints, poly.yPoints, poly.xPoints.length);
graphics.setColor(Color.blue);
graphics.fillPolygon(poly.xPoints, poly.yPoints, poly.xPoints.length);
//graphics.drawPolygon(poly.xPoints, poly.yPoints, poly.xPoints.length);
}
}
}
@Override
public void onMouseDragged(MapMouseEvent ev) {
}
@Override
public void onMouseReleased(MapMouseEvent ev) {
}
private void ensureGraphics() {
if (graphics == null) {
graphics = (Graphics2D) _Pane.getGraphics();
graphics.setColor(Color.WHITE);
graphics.setXORMode(Color.RED);
}
}
});
}
}
class pMapPolygon {
int[] xPoints = new int[500];
int[] yPoints = new int[500];
int nNumber = 0;
public pMapPolygon() {
}
public void addPoint(int x1, int y1, int number) {
// System.out.println(x1 + ":" + y1);
if (number == 0) {
for (int i = number; i < xPoints.length; i++) {
xPoints[i] = x1;
yPoints[i] = y1;
}
} else {
xPoints[number] = x1;
yPoints[number] = y1;
}
nNumber = number;
}
}
我需要做些什么才能实现这一目标?我做错了什么?
我创建了一个多边形类pMapPolygon
,它具有所需的参数xPoints
和yPoints
以及一个向存储所有x
和{{的数组添加点的函数1}}的
我在y
和addPoint()
中调用了该特定函数onMousePressed()
。但是当我启动应用程序时,它不会生成正确的多边形...
答案 0 :(得分:0)
建议在地图控件中绘制的正确方法需要一段时间,您需要覆盖地图控件的paintComponent方法。
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (drawingLock.tryLock()) {
try {
Graphics2D g2 = (Graphics2D) g;
if (baseImage != null) {
g2.drawImage(baseImage, imageOrigin.x, imageOrigin.y, null);
}
if (this._Drawing != null) {
if (this.poly != null && this._Drawing == Drawings.Polygon) {
//g.setColor(Color.gray);
g2.fillPolygon(this.poly.xPoints, this.poly.yPoints, this.poly.xPoints.length);
}
if (this.rect != null && this._Drawing == Drawings.Rectangle) {
//g.setColor(Color.gray);
g2.fillRect(rect.x, rect.y, rect.width, rect.height);
}
if (this.rect != null && this._Drawing == Drawings.Oval) {
//g.setColor(Color.gray);
g2.fillOval(rect.x, rect.y, rect.width, rect.height);
}
if (this.ln != null && this._Drawing == Drawings.Line) {
//g.setColor(Color.black);
g2.drawLine(ln._x1, ln._y1, ln._x2, ln._y2);
}
}
} finally {
drawingLock.unlock();
}
}
}