开发者

Draw Pixels in a square from inside out - Java

Draw Pixels in a square from inside out - Java

I want to draw a square of pixels pending on how many items are in an array. the square represents the array amount so small squares represent small arrays and large squares represent large arrays. I am finding it difficult to conceptualize how I go about this?

EDIT: I am using Java 2D.

The spiral starts at 1 and then adv开发者_运维百科ances anti-clockwise towards the outside of the square (i.e. 2,3,4,5 etc). Each square can be represented by the amount of data that square represents.


public class Test {

    enum Direction {
        Right,
        Up,
        Left,
        Down
    }

    public static void main(String... args) throws IOException {

        BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);

        int rgb = Color.BLACK.getRGB();

        Point p = new Point(50, 50);
        Direction d = Direction.Right;
        int currentSegmentLength = 1;


        for (int i = 0; i < 100; i += 2) {

            paintSegment(image, rgb, p, d, currentSegmentLength);
            d = nextSegmentDirection(d);

            paintSegment(image, rgb, p, d, currentSegmentLength);
            d = nextSegmentDirection(d);

            currentSegmentLength++;
        }


        ImageIO.write(image, "png", new File("test.png"));
    }

    private static void paintSegment(BufferedImage image, int rgb, Point p,
            Direction d, int currentSegmentLength) {

        for (int s = 0; s < currentSegmentLength; s++) {
            image.setRGB(p.x, p.y, rgb);

            switch (d) {
            case Right: p.x++; break;
            case Up:    p.y--; break;
            case Left:  p.x--; break;
            case Down:  p.y++; break;
            }
        }
    }

    private static Direction nextSegmentDirection(Direction d) {
        switch (d) {
        case Right: return Direction.Up;
        case Up:    return Direction.Left;
        case Left:  return Direction.Down;
        case Down:  return Direction.Right;

        default: throw new RuntimeException("never here");
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜