Java program that draws a Sierpinski Gasket
A Sierpinski Gasket or Triangle is a type of fractal named after the Polish
mathematician
Waclaw Sierpinski who described some of its interesting properties
in 1916. It is a nice example of how an orderly structure can be created as a result
of random, chaotic behavior.
One way to create the fractal is to start with an equilateral triangle. Let us say that
the corners are labeled X, Y, and Z.
1. Set current equal to point X.
2. Repeat many times (you can try 10,000).
a. Randomly pick target as one of the three X, Y, or Z.
b. Calculate the point halfway between current and target.
c. Set current to this halfway point.
d. Draw a pixel at location current. One way to do this is to fill or draw a tiny
rectangle at this coordinate.
Write a java program that draws a Sierpinski Gasket. You can pick the coordinates for
the corners of the triangle. It may seem like you should get a random mess of dots
but instead you get a very orderly picture!
To draw a single pixel at coordinate (X,Y), use the drawLine method where the
start and endpoints are both (X,Y).
Answer:
/**
* QuestionGasket.java
* This program uses the Graphics drawLine method to create a
* single pixel and plot a Sierpinski gasket.
*/
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class QuestionGasket extends JFrame
{
private static final int NUMPOINTS = 3;
private int vertex_x[] = new int[NUMPOINTS]; // Holds x,y coordinates
private int vertex_y[] = new int[NUMPOINTS]; // of the three triangle corners
private JPanel mainPanel; // All drawing in this panel
// In the constructor, set our size, title, add a JPanel,
// and set up coordinates of where the vertices are for the triangle
public QuestionGasket()
{
super();
this.setSize(800, 600);
this.setTitle("Sierpinski Gasket");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
mainPanel = new JPanel(); // The panel we will draw on
mainPanel.setBackground(Color.PINK);
add(mainPanel, BorderLayout.CENTER);
vertex_x[0] = 400; vertex_y[0] = 50; // Top of triangle
vertex_x[1] = 20; vertex_y[1] = 550; // Bottom left corner
vertex_x[2] = 780; vertex_y[2] = 550; // Bottom right corner
}
/**
* paint draws pixels according to the Sierpinksi gasket algorithm.
* It loops 50,000 times and randomly selects a target point, either
* point 0, 1, or 2. Compute the midpoint between the current point and
* a randomly selected point, draw a pixel there, and repeat.
*/
public void paint(Graphics g)
{
int targetVertex;
int target_x, target_y;
super.paint(g);
// Initialize our current coordinates to the top of the triangle
int current_x = vertex_x[0];
int current_y = vertex_y[0];
g.setColor(Color.black);
// Loop many times to draw the gasket, plotting a single point
// each time
for (int i = 0; i < 50000; i++) {
// Select a random vertex as our target, either 0,1, or 2
targetVertex = (int) (Math.random() * NUMPOINTS);
target_x = vertex_x[targetVertex];
target_y = vertex_y[targetVertex];
current_x = (current_x + target_x)/2;
current_y = (current_y + target_y)/2;
g.drawLine(current_x, current_y, current_x, current_y);
}
}
// Our main method just displays the window
public static void main(String[] args)
{
QuestionGasket sierpinski = new QuestionGasket();
sierpinski.setVisible(true);
}
}
Leave a reply