package spiroGraphic; import javax.swing.JFrame; public class Main { public static void main(String[] args) { boolean persist=true; SpiroFrame window=new SpiroFrame(8); window.setSize(600, 400); window.setLocation(640, 140); window.setResizable(false); window.setTitle("Spirograph"); window.setUndecorated(false); window.setOpacity(1F); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); while(persist){ window.repaint(); window.step(); window.setAlwaysOnTop(true); try { Thread.sleep(15); } catch (InterruptedException e) { e.printStackTrace(); } } } } package spiroGraphic; import java.awt.*; import java.awt.geom.*; import java.awt.image.*; import java.util.*; import javax.swing.*; public class SpiroFrame extends JFrame { private Spirograph spiral; private int width, height; SpiroFrame(int size){ spiral=new Spirograph(size); } SpiroFrame(int size, double x, double y){ spiral=new Spirograph(size, x, y); } public void paint(Graphics g){ Graphics2D image=(Graphics2D) g; BufferedImage bufferBase=new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D bufferImage=bufferBase.createGraphics(); ArrayList lines=spiral.getLines(); bufferImage.setBackground(Color.WHITE); bufferImage.setColor(Color.BLACK); bufferImage.fillRect(0, 0, width, height); bufferImage.setColor(Color.GREEN); for(int a=0; a getLines(){ ArrayList lines=new ArrayList(); a=10; b=7; double quality=Math.PI/30; // Theta range between checks for(double theta=0; theta<16*(Math.PI); theta+=quality){ double x1,x2,y1,y2; x1=Math.cos(theta+rotation)*(a+b); x2=Math.cos(theta+rotation+quality)*(a+b); y1=Math.sin(theta+rotation)*(a+b); y2=Math.sin(theta+rotation+quality)*(a+b); x1-=b*Math.cos( (theta)* (a+b)/b ); x2-=b*Math.cos( (theta+quality)*(a+b)/b ); y1-=b*Math.sin( (theta)* (a+b)/b ); y2-=b*Math.sin( (theta+quality)*(a+b)/b ); x1*=size; x2*=size; y1*=size; y2*=size; double xmod=1, ymod=.4; x1*=xmod; x2*=xmod; y1*=ymod; y2*=ymod; x1+=x; x2+=x; y1+=y; y2+=y; lines.add(new Line2D.Double(x1, y1, x2, y2)); } return lines; } public void step(){ step++; if(!growing){ a-=0.01; } if(growing){ a+=0.01; if(a>=12){ a=12; growing=false; } } if(a<=8){ a=8; growing=true; } } public void rotate(double angle){ rotation+=angle; } public void setCoord(double x, double y){ this.x=x; this.y=y; } }