绘制不规则的形状,并在awt java中填充颜色

时间:2018-05-31 12:10:48

标签: java awt shape curve

我正在开发一种应用程序,它在画布上绘制各种形状并用颜色填充它们;只是一个使用java的绘图应用程序我已经实现了矩形选择并填充了形状。我想要的更多的是,绘制不规则的形状,即具有一些弯曲表面的形状,并用颜色填充它。

绘制形状的基本流程是;首先,使用鼠标绘制轮廓,一旦路径关闭,可以通过拖动端点来调整形状的大小。然后,点击“完成”后,可以使用默认颜色(GRAY)填充它。按钮。

关于曲面,我成功绘制了曲面轮廓,尽管有一些错误。现在,一旦我点击“完成”#39;按钮,不需要的形状被绘制。实际上,无论轮廓如何,我都希望用颜色填充轮廓的内部部分。

Example image here

借助控制点绘制曲线的代码。

public static Path2D.Double createPathWithCurve(ArrayList<EndPointRect> endPoints, ArrayList<MidPointRect> midPoints, MidPointRect midPoint, Point curPoint) {
    Path2D.Double path = new Path2D.Double();

    Point start = midPoint.getStart();
    Point end = midPoint.getEnd();
    int x[] = Utility.fetchArrayOfXFromPoint(endPoints);
    int y[] = Utility.fetchArrayOfYFromPoint(endPoints);

    for(int i = 0; i < x.length; i++) {
        MidPointRect mp = hasCurve(new Point(x[i], y[i]), midPoints);

        if(i == 0) {
            path.moveTo(x[i], y[i]); // Moves drawer to first point
            if(x[i] == start.getX() && y[i] == start.getY()) {
                path.curveTo(start.getX(), start.getY(),
                        curPoint.getX(), curPoint.getY(),
                        end.getX(), end.getY());
                midPoint.setXy(curPoint);
                midPoint.setControlPoint(curPoint);
            }
            else if(mp != null)
                path.curveTo(mp.getStart().getX(), mp.getStart().getY(),
                        mp.getControlPoint().getX(), mp.getControlPoint().getY(),
                        mp.getEnd().getX(), mp.getEnd().getY());
        }
        else if(x[i] == start.getX() && y[i] == start.getY()) {
            path.lineTo(x[i], y[i]);
            path.moveTo(start.getX(), start.getY());
            path.curveTo(start.getX(), start.getY(),
                    curPoint.getX(), curPoint.getY(),
                    end.getX(), end.getY());
            path.moveTo(end.getX(), end.getY());
            midPoint.setXy(curPoint);
            midPoint.setControlPoint(curPoint);
        }
        else {
            if(mp != null) {
                path.lineTo(x[i], y[i]);
                path.moveTo(mp.getStart().getX(), mp.getStart().getY());
                path.curveTo(mp.getStart().getX(), mp.getStart().getY(),
                        mp.getControlPoint().getX(), mp.getControlPoint().getY(),
                        mp.getEnd().getX(), mp.getEnd().getY());
                path.moveTo(mp.getEnd().getX(), mp.getEnd().getY());
            }
            else
                path.lineTo(x[i], y[i]);
        }

        if((i+1) == x.length) {
            path.lineTo(x[0], y[0]);  // Draws line from last point to first point
            //path.closePath();
        }
    }

    return path;
}

&#39;完成&#39;按钮单击方法和自定义曲线类:

public void doneButtonCalled() {
if (!tempRectList.isEmpty()) {
    CustomCurve area = createCurveFromPoints(getCurrentShape(), getTempRectList());
    repaint();
} }

public CustomCurve createCurveFromPoints(Path2D.Double shape, ArrayList<EndPointRect> points) {
int x[] = Utility.fetchArrayOfXFromPoint(points);
int y[] = Utility.fetchArrayOfYFromPoint(points);

return new CustomCurve(shape, x, y, x.length); }

public class CustomCurve extends Area implements ShapeModel, Serializable {
private Color color;
private int[] xPoints;
private int[] yPoints;
private boolean visible;

public CustomCurve(Path2D.Double shape, int[] xPoints, int[] yPoints, int nPoints) {
    super(shape);
    this.xPoints = xPoints;
    this.yPoints = yPoints;
    this.color = Color.darkGray;
    visible = true;
}

/* setters and getters */  }

0 个答案:

没有答案