1
我想繪製一個像素的正方形,等待數組中有多少項。正方形表示數組量,所以小方塊表示小數組,大方塊表示大數組。我很難概念化我如何去做這件事?從內向外繪製一個正方形的像素 - Java
編輯:我正在使用Java 2D。
螺旋從1開始,然後逆時針向正方形外側(即2,3,4,5等)前進。每個正方形可以用正方形表示的數據量表示。
我想繪製一個像素的正方形,等待數組中有多少項。正方形表示數組量,所以小方塊表示小數組,大方塊表示大數組。我很難概念化我如何去做這件事?從內向外繪製一個正方形的像素 - Java
編輯:我正在使用Java 2D。
螺旋從1開始,然後逆時針向正方形外側(即2,3,4,5等)前進。每個正方形可以用正方形表示的數據量表示。
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");
}
}
}
你使用了什麼圖形庫/工具包/系統? – 2010-12-01 18:41:34
...從內到外? – 2010-12-01 18:48:06