填充椭圆[Java]

时间:2016-02-28 20:08:29

标签: java drawing ellipse

我正在尝试填充我的Ellipse,虽然我想知道是否有更有效的方法,但代码仍有效。

这将填充给定百分比的圆圈的前半部分。然后它将填补圆圈的后半部分。

如果您想查看任何其他功能,请告诉我。我主要关注填充它。

public void drawOrb() {
    this.icon.drawSprite(this.xPos - this.icon.getWidth() / 2, 29 - this.icon.getHeight() / 2);
    int radius = 19;
    fillCircleAlpha(this.xPos, this.yPos, radius, 0, 35); // Draws a filled circle given a radius and alpha value.
    Ellipse2D.Double circleToAvoid = drawCircle(this.xPos - radius, this.yPos - radius, radius * 2, 0, //The inner circle.
            125);
    Ellipse2D.Double circleToStart = drawCircle(this.xPos - (radius + 4), this.yPos - (radius + 4),
            radius * 2 + 8, 0, 150); // The outer circle.
    radius = 23;
    int r2 = radius * radius;
    int area = r2 << 2;
    int rr = radius << 1;
    for (int area2 = (int) (area * progress * 2.0), i = 0; i < area2; ++i) { //
        int tx = i % rr;
        int ty = i / rr;
        if (!circleToAvoid.contains(circleToStart.getCenterX() + tx, circleToStart.getY() + ty) //If the index is inside the circle.
                && circleToStart.contains(circleToStart.getCenterX() + tx, circleToStart.getY() + ty)) {
            drawPixelsWithOpacity(16777215, this.yPos + ty - radius, 1, 1, 75, this.xPos + tx); // Used to fill each pixel within the circle.
        }
    }
    if (progress > 0.5) {
        for (int area3 = (int) (area * (progress - 0.5) * 2.0), j = 0; j < area3; ++j) {
            int tx2 = j % rr;
            int ty2 = j / rr;
            if (!circleToAvoid.contains(circleToStart.getCenterX() - tx2, circleToStart.getMaxY() - ty2)
                    && circleToStart.contains(circleToStart.getCenterX() - tx2 - 1.0,
                            circleToStart.getMaxY() - ty2)) {
                drawPixelsWithOpacity(16777215, (int) circleToStart.getMaxY() - ty2, 1, 1, 75,
                        (int) circleToStart.getCenterX() - tx2 - 1);
            }
        }
    }
    radius = 19;
    drawCircle(this.xPos - (radius + 4), this.yPos - (radius + 4), radius * 2 + 8, 0, 150);
}
public static void drawPixelsWithOpacity(int color, int yPos, int pixelWidth, int pixelHeight, int opacityLevel, int xPos) {
    if (xPos < topX) {
        pixelWidth -= topX - xPos;
        xPos = topX;
    }
    if (yPos < topY) {
        pixelHeight -= topY - yPos;
        yPos = topY;
    }
    if (xPos + pixelWidth > bottomX)
        pixelWidth = bottomX - xPos;
    if (yPos + pixelHeight > bottomY)
        pixelHeight = bottomY - yPos;
    int l1 = 256 - opacityLevel;
    int i2 = (color >> 16 & 0xff) * opacityLevel;
    int j2 = (color >> 8 & 0xff) * opacityLevel;
    int k2 = (color & 0xff) * opacityLevel;
    int k3 = width - pixelWidth;
    int l3 = xPos + yPos * width;
    if (l3 > pixels.length - 1) {
        l3 = pixels.length - 1;
    }
    for (int i4 = 0; i4 < pixelHeight; i4++) {
        for (int j4 = -pixelWidth; j4 < 0; j4++) {
            int l2 = (pixels[l3] >> 16 & 0xff) * l1;
            int i3 = (pixels[l3] >> 8 & 0xff) * l1;
            int j3 = (pixels[l3] & 0xff) * l1;
            int k4 = ((i2 + l2 >> 8) << 16) + ((j2 + i3 >> 8) << 8) + (k2 + j3 >> 8);
            pixels[l3++] = k4;
        }
        l3 += k3;
    }
}
public static Ellipse2D.Double drawCircle(final int x, final int y, final int diameter, final int color, final int opacity) {
    final Ellipse2D.Double circle = new Ellipse2D.Double(x, y, diameter, diameter);
    for (int i = 0; i < diameter; ++i) {
        for (int i2 = 0; i2 < diameter; ++i2) {
            if (circle.contains(i + x, i2 + y) && (!circle.contains(i + x - 1, i2 + y - 1) || !circle.contains(i + x + 1, i2 + y + 1) || !circle.contains(i + x - 1, i2 + y + 1) || !circle.contains(i + x + 1, i2 + y - 1))) {
                drawPixelsWithOpacity(color, i2 + y, 1, 1, opacity, i + x);
            }
        }
    }
    return circle;
}

0 个答案:

没有答案