[currently viewing: /java/PrimalSpire5source.javasrc]
import javax.swing.*;
//import java.awt.event.*;
//import java.awt.geom.*;
public class PrimalSpire5 extends JApplet {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(Color.blue);
int width = getSize().width-1;
int height = getSize().height-1;
int midX = width / 2;
int midY = height / 2;
int x = midX;
int y = midY;
int q = 0;
int count = 1;
int j = -1;
int dir = 3; //direction
/* you need to add 1 to whatever number you want the
spiral to go to and put it where "i <" */
for (int i = 1; i < 1000001; i++) {
if (dir == 0) { // go east
x+=1;
} else if (dir == 1) { // go north
y-=1;
} else if (dir == 2) { // go west
x-=1;
} else if (dir == 3) { // go south
y+=1;
}
if (i < 4) {
dir++;
}
if (i == 4) {
dir = 2;
q = 5; //just a randomly (ok, badly) named variable. I didn't know what to call it.
}
if (i == q) {
dir++;
if (i > 9) {
j++;
}
if (j % 2 != 0) {
count++;
}
q+= count;
}
if (dir > 3) {
dir = 0;
}
if (is_prime(i)) {
g2d.setPaint(Color.black);
g2d.fillRect(x,y,1,1);
} else {
g2d.setPaint(Color.blue);
g2d.fillRect(x,y,1,1);
}
}
}
public static boolean is_prime (int n) {
boolean prime = true;
int limit = (int) Math.sqrt (n);
for (int i = 2; i <= limit; i++) {
if (n % i == 0) {
prime = false;
break;
}
}
if (n == 0 || n == 1) {
return false;
}
return prime;
}
public static void main(String args[]) {
JFrame frame = new JFrame("Primal Spire");
JApplet applet = new PrimalSpire5();
frame.getContentPane().add("Center", applet);
frame.setSize(300, 250);
frame.setVisible(true);
}
}
