【发布时间】:2018-10-20 03:47:18
【问题描述】:
我显然在这里遗漏了一个重要概念。我已经编写了使用鼠标事件在现有 BufferedImage 上绘制边界(多边形)的代码。以下是相关部分:
public void paintComponent(Graphics g)
{
super.paintComponent(g); //Paint parent's background
//G3 displays the BufferedImage "Drawing" with each paint
Graphics2D G3 = (Graphics2D)g;
G3.drawImage(this.Drawing, 0, 0, null);
G3.dispose();
}
public void updateDrawing()
{
int x0, y0, x1, y1; // Vertex coordinates
Line2D.Float seg;
// grafix is painting the mouse drawing to the BufferedImage "Drawing"
if(this.pts.size() > 0)
{
for(int ip = 0; ip < pts.size(); ip++)
{
x0 = (int)this.pts.get(ip).x;
y0 = (int)this.pts.get(ip).y;
this.grafix.drawRect(x0 - this.sqw/2, y0 - this.sqh/2, + this.sqw, this.sqh);
if (ip > 0)
{
x1 = (int)this.pts.get(ip-1).x;
y1 = (int)this.pts.get(ip-1).y;
this.grafix.drawLine(x1, y1, x0, y0);
seg = new Line2D.Float(x1, y1, x0, y0);
this.segments.add(seg);
}
}
}
repaint();
}
接下来的两个例程由鼠标事件调用:左键单击获取下一个点,右键单击关闭该区域。
public void getNextPoint(Point2D p)
{
this.isDrawing = true;
Point2D.Float next = new Point2D.Float();
next.x = (float) p.getX();
next.y = (float) p.getY();
this.pts.add(next);
updateDrawing();
}
public void closeBoundary()
{
//Connects the last point to the first point to close the loop
Point2D.Float next = new Point2D.Float(this.pts.get(0).x, this.pts.get(0).y);
this.pts.add(next);
this.isDrawing = false;
updateDrawing();
}
一切正常,我可以保存带有我的绘图的图像: image with drawing 顶点(pts)列表和线段(segments)都是描述区域/形状/多边形的。 我希望仅从原始图像中提取边界内的区域。也就是说,我计划通过移动所有像素来创建一个新的 BufferedImage,测试它们是否落入图中,如果有则保留它们。 所以我想根据我在绘制形状时收集的点和线段创建一个区域。一切都在说:创建一个 AREA 变量和“getPathIterator”。但在什么形状上?我的 AREA 变量将为空。路径迭代器如何访问我列表中的点?
我也浏览过文献和这个网站。 我错过了一些东西。
【问题讨论】:
-
在绘画时存储所有点,然后从点创建一个
Polygon。如果需要,从它创建一个Area(new Area(polygon))。但您也可以直接从Polygon创建一个PathIterator。很难确切地说出您为什么要这样做...也许有关您尝试做的一些伪代码会有所帮助。
标签: java bufferedimage shapes area