Advertisement
Guest User

Untitled

a guest
Nov 8th, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 KB | None | 0 0
  1. package spiroGraphic;
  2.  
  3. import javax.swing.JFrame;
  4.  
  5. public class Main {
  6.  
  7.    
  8.    
  9.    
  10.     public static void main(String[] args) {
  11.        
  12.         boolean persist=true;
  13.        
  14.         SpiroFrame window=new SpiroFrame(8);
  15.        
  16.         window.setSize(600, 400);
  17.         window.setLocation(640, 140);
  18.        
  19.         window.setResizable(false);
  20.         window.setTitle("Spirograph");
  21.         window.setUndecorated(false);
  22.         window.setOpacity(1F);
  23.        
  24.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25.        
  26.        
  27.         window.setVisible(true);
  28.        
  29.        
  30.         while(persist){
  31.             window.repaint();
  32.             window.step();
  33.             window.setAlwaysOnTop(true);
  34.            
  35.             try {
  36.                 Thread.sleep(15);
  37.             } catch (InterruptedException e) {
  38.                 e.printStackTrace();
  39.             }
  40.            
  41.         }
  42.        
  43.  
  44.     }
  45.  
  46. }
  47.  
  48.  
  49.  
  50.  
  51.  
  52. package spiroGraphic;
  53.  
  54. import java.awt.*;
  55. import java.awt.geom.*;
  56. import java.awt.image.*;
  57. import java.util.*;
  58.  
  59. import javax.swing.*;
  60.  
  61.  
  62.  
  63. public class SpiroFrame extends JFrame {
  64.    
  65.     private Spirograph spiral;
  66.    
  67.     private int width, height;
  68.    
  69.     SpiroFrame(int size){
  70.         spiral=new Spirograph(size);
  71.     }
  72.     SpiroFrame(int size, double x, double y){
  73.         spiral=new Spirograph(size, x, y);
  74.     }
  75.    
  76.     public void paint(Graphics g){
  77.         Graphics2D image=(Graphics2D) g;
  78.        
  79.         BufferedImage bufferBase=new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  80.         Graphics2D bufferImage=bufferBase.createGraphics();
  81.        
  82.         ArrayList<Line2D> lines=spiral.getLines();
  83.        
  84.         bufferImage.setBackground(Color.WHITE);
  85.         bufferImage.setColor(Color.BLACK);
  86.         bufferImage.fillRect(0, 0, width, height);
  87.         bufferImage.setColor(Color.GREEN);
  88.        
  89.         for(int a=0; a<lines.size(); a++){
  90.             bufferImage.draw(lines.get(a));
  91.         }
  92.  
  93.        
  94.         image.clearRect(0, 0, width, height);
  95.         image.drawImage(bufferBase, null, 0, 0);
  96.        
  97.     }
  98.    
  99.    
  100.    
  101.     public void setSize(int width, int height){
  102.         super.setSize(width, height);
  103.         this.width=width;
  104.         this.height=height;
  105.        
  106.         spiral.setCoord(width/2, height/2);
  107.        
  108.     }
  109.    
  110.     public void step(){
  111.         spiral.rotate((Math.PI)/300);
  112.         //spiral.step();
  113.     }
  114.  
  115. }
  116.  
  117.  
  118.  
  119.  
  120.  
  121. package spiroGraphic;
  122.  
  123. import java.awt.geom.Line2D;
  124. import java.util.ArrayList;
  125.  
  126. public class Spirograph {
  127.    
  128.    
  129.     private int size; // Size
  130.    
  131.     private double rotation; // Rotation from standard position
  132.     private double scaleStage;
  133.     private int step;
  134.     private boolean growing=true;
  135.    
  136.     private double a,b;
  137.    
  138.     private double x,y;
  139.    
  140.  
  141.     Spirograph(int scale){
  142.         size=scale;
  143.         rotation=0;
  144.         scaleStage=0;
  145.         x=0;
  146.         y=0;
  147.         step=0;
  148.         a=10;
  149.         b=7;
  150.     }
  151.     Spirograph(int scale, double x, double y){
  152.         size=scale;
  153.         rotation=0;
  154.         this.x=x;
  155.         this.y=y;
  156.         step=0;
  157.         a=10;
  158.         b=7;
  159.     }
  160.    
  161.     public ArrayList<Line2D> getLines(){
  162.         ArrayList<Line2D> lines=new ArrayList<Line2D>();
  163.        
  164.         a=10;
  165.         b=7;
  166.        
  167.        
  168.        
  169.         double quality=Math.PI/30; // Theta range between checks
  170.        
  171.         for(double theta=0; theta<16*(Math.PI); theta+=quality){
  172.            
  173.             double x1,x2,y1,y2;
  174.            
  175.             x1=Math.cos(theta+rotation)*(a+b);
  176.             x2=Math.cos(theta+rotation+quality)*(a+b);
  177.             y1=Math.sin(theta+rotation)*(a+b);
  178.             y2=Math.sin(theta+rotation+quality)*(a+b);
  179.            
  180.             x1-=b*Math.cos( (theta)*        (a+b)/b );
  181.             x2-=b*Math.cos( (theta+quality)*(a+b)/b );
  182.             y1-=b*Math.sin( (theta)*        (a+b)/b );
  183.             y2-=b*Math.sin( (theta+quality)*(a+b)/b );
  184.            
  185.             x1*=size;
  186.             x2*=size;
  187.             y1*=size;
  188.             y2*=size;
  189.             double xmod=1, ymod=.4;
  190.            
  191.             x1*=xmod;
  192.             x2*=xmod;
  193.            
  194.             y1*=ymod;
  195.             y2*=ymod;
  196.            
  197.  
  198.            
  199.             x1+=x;
  200.             x2+=x;
  201.             y1+=y;
  202.             y2+=y;
  203.            
  204.            
  205.             lines.add(new Line2D.Double(x1, y1, x2, y2));
  206.            
  207.         }
  208.        
  209.         return lines;
  210.     }
  211.    
  212.    
  213.     public void step(){
  214.         step++;
  215.        
  216.         if(!growing){
  217.             a-=0.01;
  218.         }
  219.        
  220.         if(growing){
  221.             a+=0.01;
  222.             if(a>=12){
  223.                 a=12;
  224.                 growing=false;
  225.             }
  226.         }
  227.         if(a<=8){
  228.             a=8;
  229.             growing=true;
  230.         }
  231.        
  232.        
  233.        
  234.     }
  235.    
  236.    
  237.    
  238.     public void rotate(double angle){
  239.         rotation+=angle;
  240.     }
  241.    
  242.     public void setCoord(double x, double y){
  243.         this.x=x;
  244.         this.y=y;
  245.     }
  246.    
  247.  
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement