| | |
Seirpinski Triangle through Chaos Game
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
This is a simple implementation of the Chaos Game to produce a Seirpinski Triangle.
import java.awt.*; import javax.swing.*; /** * Main Frame of the Sierpinski Triangle frame. * @author SciWizEH * @version 1.0 */ public class Sierpinski extends JFrame { /**make a new Sierpinski*/ public Sierpinski() { //add STPane to frame this.add(new STPane()); //pack this.pack(); //set visible this.setVisible(true); } /**main*/ public static void main(String[] args){ //make a new frame new Sierpinski(); } } /** * Point class holds a 2 dimentional integer point. */ class Point { //two portions of the point int x,y; //default constructor does nothing public Point(){} //constructor that takes two arguments public Point(int nx,int ny){ x=nx; y=ny; } } /** * class that computes and displays the Sierpinski Triangle */ class STPane extends JComponent { //holds the initial triangle for the sierpenski Point[] triangle = new Point[3]; //first point Point fp; //current point Point cp=new Point(); //default constructor simply sets the original size public STPane(){ this.setPreferredSize(new Dimension(500,500)); } //simple method to ge a 0-2 range random number to select a new point public int rand0to2(){ return (int)(Math.random()*100)%3; } //paint where the magic of the chaos happens public void paint(Graphics g){ //get the current size of the component Dimension d = super.getSize(); //make an isocilese triangle from the size of the //component for the original points triangle[0] = new Point((int)d.getWidth()/2,0); triangle[1] = new Point(0,(int)d.getHeight()); triangle[2] = new Point((int)d.getWidth(),(int)d.getHeight()); //get an arbitrary point in the middle of the screen somewhere fp=new Point((int)(Math.random()*d.getWidth()),(int)(Math.random()*d.getHeight()/2)); //get a random triangle point Point p = triangle[rand0to2()]; //get the first current point by going half way from the first //point to the chosen triangle point cp.x=(p.x+fp.x)/2; cp.y=(p.y+fp.y)/2; //draw the first point g.drawRect(cp.x,cp.y,0,0); //compute 40000 points in the same fashion but use the current //point rather than the very original point for(int i = 0; i<40000;i++){ p = triangle[rand0to2()]; cp.x=(p.x+cp.x)/2; cp.y=(p.y+cp.y)/2; g.drawRect(cp.x,cp.y,0,0); } } }
0
•
•
•
•
There's no need to write
(int)(Math.random()*100)%3 . Just write (int)(Math.random() * 3) . Similar Threads
- Table Chaos (MS SQL)
- Fractals and the Chaos Game help. (Java)
- I don't want a right triangle I want a equilateral triangle!! help (C)
- Triangle (C++)
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary bluetooth character chat class classes client code component consumer csv database desktop draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie objects online oracle oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion robot rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time tree ubuntu windows working



