문제

I am currently making a painting application and am currently implementing the rubber tool. Due to the reason that the canvas (JPanel) I am using has a transparent background (intended), I could not just call g2d.fillOval(x,y,x,y) as it would draw a transparent circle, thus the drawing that was there before would remain. Conscquently, I have decided to use WritableRaster and it's setPixel method in two for loops that shape the overall 'shape' that the pixels draw into a circle (r is the same on every tangent). Yet, I am having problem with calling the setPixel method, as it does not draw anything. I have even set the color to black just to test it, and nothing. I have been tampering it and inserting print statetments and narrowed it down into the most inner for block, as the print method would not output into the console.

Here is my code

WritableRaster raster = (WritableRaster) bi.getData();
Integer[] arrayw = al.get(i);

Point p1 = new Point(arrayw[0], arrayw[1]);
Point p2 = new Point(arrayw[2], arrayw[3]);
int[] black = new int[]{0, 0, 0};
int r = tl.get(i);

for (int y = p1.y - r; y < r; y++) {
    int bound = (int) (Math.sqrt(r * r - y * y) + 0.5);
    for (int x = p1.x - bound; x < bound; x++) {
        raster.setPixel(x, y, black);
        gc.drawLine(x, y, x, y);
        System.out.println(y + "\t" + x + "\t" + tl.get(i));

    }
}
for (int y = p2.y - r; y < r; y++) {
    int bound = (int) (Math.sqrt(r * r - y * y) + 0.5);
    System.out.print("test");

    for (int x = p2.x - bound; x < bound; x++) {
        raster.setPixel(x, y, black);
        gc.drawLine(x, y, x, y);
        System.out.println(y + "\t" + x + "\t" + tl.get(i));
    }
}


System.out.print("raster");
bi.setData(raster);

As I said before, the code prints raster, but does not print inside the inner for block. I have no idea what I am doing wrong. Thanks for you help!

도움이 되었습니까?

해결책

Have you verified that the condition of the inner block is ever met?

For instance, is it ever true that x < bound if x is always greater than or equal to p1.x - bound? If p1.x > 2 * bound, your inner loop will never get called.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top