All pastes #911302 Raw Edit

G2DTest

public java v1 · immutable
·published 2008-02-20 16:59 UTC
rendered paste body
import java.awt.Color;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Point;import java.util.Random;public class G2DTest extends javax.swing.JFrame {        private Point[] points;    private int skip=1;    private int red=1; private int green=2; private int blue=3;    private int rdir=1; private int gdir=1; private int bdir=1;    private int xt=0; private int yt=0;    private double theta=0.0;    public int rot=0;    public int rotdir=1;    int speed=30;    Random random;    /** Creates new form G2DTest */    public G2DTest() {        random=new Random();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        setSize(1000,1000);        initPoints();    }        public void go(){        int i=5;        while(true){            i%=60;            if (i<10) i=10;            if (i>50) i=10;            //System.out.println("i="+i);            skip=i;            paint(this.getGraphics());            //System.out.println("painting...skip is: "+skip);            i++;            try {                Thread.sleep(speed);            } catch (InterruptedException ex) {                ex.printStackTrace();            }            initPoints();        }    }            public void initPoints(){        // each point represents 1/60th of a circle, or 6 degrees        if(points==null)            points=new Point[60];        int xoff=450;        int yoff=450;        int radius=400;        for (int i=0; i<60; i++){            int x, y;            double angle=(Math.PI/180.0)*((i*6)+(rot%6));            double xr=Math.cos(angle);            double yr=Math.sin(angle);            x=(int)((xr*radius)+xoff);            y=(int)((yr*radius)+yoff);            points[i]=new Point(x, y);            //System.out.println("i="+i+"\tx="+xr+"\ty="+yr+"");        }        if(rotdir==1)            rot++;        else            rot--;        if(rot%180==0){            rotdir*=-1;            speed=random.nextInt(40)+20;        }    }            public void paint(Graphics g){        Graphics2D g2=(Graphics2D) g;        g2.clearRect(0,0,1000,1000);       /* g2.setColor(new Color(red, green, blue));        red+=1; blue+=2; green+=3;        red=red%256;green=green%256;blue=blue%256; */        nPoints(g2,1);        nPoints(g2, skip);    }        public void nPoints(Graphics2D g2, int n){        for(int i=0; i<60; i++){            g2.drawLine(points[i%60].x, points[i%60].y, points[(i+n)%60].x, points[(i+n)%60].y);        }    }        public void onePoint(Graphics2D g2){        for(int i=0; i<59; i++){            g2.drawLine(points[i].x, points[i].y, points[i+1].x, points[i+1].y);        }        g2.drawLine(points[59].x, points[59].y, points[0].x, points[0].y);    }        public void twoPoint(Graphics2D g2){        for(int i=0; i<59; i+=2){            g2.drawLine(points[i%60].x, points[i%60].y, points[(i+2)%60].x, points[(i+2)%60].y);        }        for(int i=1; i<60; i+=2){            g2.drawLine(points[i%60].x, points[i%60].y, points[(i+1)%60].x, points[(i+1)%60].y);        }//        g2.drawLine(points[59].x, points[59].y, points[0].x, points[0].y);    }            /**     * @param args the command line arguments     */    public static void main(String args[]) {        java.awt.EventQueue.invokeLater(new Runnable() {            public void run() {                G2DTest g2dt=new G2DTest();                g2dt.setVisible(true);                g2dt.go();            }        });    }        }