import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class Difficult extends Canvas{
protected static int[] buffer;
protected static final int width = 70;
protected static final int height = 70;
public Difficult() {
setSize(700,725);
setBackground(Color.white);
setForeground(Color.black);
do_spiral();
}
private void draw_line(int x, int y, int xx, int yy) {
for(int sy = y; sy <= yy; sy++) {
for(int sx = x; sx <= xx; sx++) {
buffer[sy * height + sx] = 1;
}
}
}
private void do_spiral() {
int speed = 1;
int x = width / 2;
int y = height / 2;
buffer = new int[width*height];
for(int i = 0; i < height-4; i++) {
int dir = i % 4;
switch(dir) {
// up
case 0:
draw_line(x, y-speed, x, y);
y -= speed;
break;
// left
case 1:
speed += 2;
draw_line(x-speed, y, x, y);
x -= speed;
break;
// down
case 2:
draw_line(x, y, x, y+speed);
y += speed;
break;
// right
case 3:
speed += 2;
draw_line(x, y, x+speed, y);
x += speed;
break;
}
}
}
public void paint(Graphics g) {
for(int sy = 0; sy < height; sy++) {
for(int sx = 0; sx < width; sx++) {
if (buffer[sy*height+sx] == 1)
g.fillRect(sx*10, sy*10, 10, 10);
}
}
}
public static void main(String [] args) {
Difficult d = new Difficult();
Difficult dtwo = new Difficult();
JFrame f = new JFrame("Challenge #18");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(700,725);
f.add(d);
f.setVisible(true);
}
}