Skip to content

Demonstrate Sierpinski Gasket with Applet

September 11, 2013


–> demonstrate recursion.

–> know what is recursion and how its used in a program.

Simply put, recursion is when a function calls itself. That is, in the course of the function definition there is a call to that very same function. At first this may seem like a never ending loop, or like a dog chasing its tail. It can never catch it. So too it seems our method will never finish. This might be true is some cases, but in practise we can check to see if a certain condition is true and in that case exit (return from) our method. click here for more info.

We demonstrate recursion with Sierpinski Gasket. Sierpinski Gasket is a fractal based on a triangle with four equal triangles inscribed in it. The central triangle is removed and each of the other three treated as the original was, and so on, creating an infinite regression in a finite space. click here for more info.


// import the packages
import java.awt.*;
import javax.swing.*;

public class SierpinskiGasket extends JApplet
	int level = 0;

	// when the app fires off
	// a dialog will pop up and ask to enter the depth
	// of recurstion
	public void init()
		String levelStr = JOptionPane.showInputDialog("Enter the recursion depth:");
		level = Integer.parseInt(levelStr);


	// this paint method initializes the 3 point objs
	public void paint(Graphics g)
		Point pointOne = new Point(60, 160);
		Point pointTwo = new Point(220, 160);
		Point pointThree= new Point(140, 20);

		// this fires off the recusion
		drawSierpinski(g, level, pointOne, pointTwo, pointThree);

	private void drawSierpinski(Graphics g, int lev, Point p1, Point p2, Point p3)
		Point midP1P2;	
		Point midP2P3;
		Point midP3P1;

		// the condition stops the recursion 
		// and preventing it to go infinity.
		// although, that's the theory, our computers has limits 
		// in its memory so this recursion will stop at one point.
		if(lev > 0)
			// draws the line from the method x and y of class Point
			g.drawLine(p1.x,p1.y, p2.x, p2.y);
			g.drawLine(p2.x,p2.y, p3.x, p3.y);
			g.drawLine(p3.x,p3.y, p1.x, p1.y);

			// gets the mid point for every line
			midP1P2 = midPoint(p1, p2);
			midP2P3 = midPoint(p2, p3);
			midP3P1 = midPoint(p3, p1);			// start the recurstion for every triangle made in the first recurstion
			// kind of a loop
			drawSierpinski(g, lev - 1, p1, midP1P2, midP3P1);
			drawSierpinski(g, lev - 1, p2, midP2P3, midP1P2);
			drawSierpinski(g, lev - 1, p3, midP3P1, midP2P3);
	private Point midPoint(Point pOne, Point pTwo)
		Point mid = new Point((pOne.x + pTwo.x) / 2, (pOne.y + pTwo.y) / 2);
		return mid;

click here to learn how to embed applets in browser. or google it. Below are some snapshots of the app.

Simple, Free Image and File Hosting at MediaFire

Simple, Free Image and File Hosting at MediaFire



Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Children's Book Reviews

Malav Shah

official Web Site

My Road to Programming

The journey of how I taught myself to program.


Look at what's brewing up guys! we all are work in progress. So don't meander, lets micromesh everything and have candid conversation about every problem related to C, C++ and JAVA. We can rejuvenate the crinkled faces.


A simple guide to programming...


Information Techonology

Rahul's Work

My Work Tips and Tutorial on Various Tech.

Turnip Farmers

Kingdom Scans


Technology news, trends and analysis covering mobile, big data, cloud, science, energy and media


News About Tech, Money and Innovation


The Diary of a Retiree

50 Year Project

My challenge to visit 192 countries, read 1,001 books, and watch the top 100 movies

[BTW] : Ben Trube, Writer

Writer, Programmer, Singer

My Arch Walkthrough

Arch linux made easy

Don Charisma

because anything is possible with Charisma

SutoCom Solutions

Success & Satisfaction with the Cloud

Waterlooville Designs

A Professional Graphic Designer In Waterlooville.

%d bloggers like this: