Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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<Line2D> 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<lines.size(); a++){
- bufferImage.draw(lines.get(a));
- }
- image.clearRect(0, 0, width, height);
- image.drawImage(bufferBase, null, 0, 0);
- }
- public void setSize(int width, int height){
- super.setSize(width, height);
- this.width=width;
- this.height=height;
- spiral.setCoord(width/2, height/2);
- }
- public void step(){
- spiral.rotate((Math.PI)/300);
- //spiral.step();
- }
- }
- package spiroGraphic;
- import java.awt.geom.Line2D;
- import java.util.ArrayList;
- public class Spirograph {
- private int size; // Size
- private double rotation; // Rotation from standard position
- private double scaleStage;
- private int step;
- private boolean growing=true;
- private double a,b;
- private double x,y;
- Spirograph(int scale){
- size=scale;
- rotation=0;
- scaleStage=0;
- x=0;
- y=0;
- step=0;
- a=10;
- b=7;
- }
- Spirograph(int scale, double x, double y){
- size=scale;
- rotation=0;
- this.x=x;
- this.y=y;
- step=0;
- a=10;
- b=7;
- }
- public ArrayList<Line2D> getLines(){
- ArrayList<Line2D> lines=new ArrayList<Line2D>();
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement