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());
        //set visible
    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){
 * 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
        //draw the first point
        //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()];
About the Author

There's no need to write (int)(Math.random()*100)%3 . Just write (int)(Math.random() * 3) .

I know, but, i've tested out some random stuff, and my way makes a better spread.