A Sierpinski Triangle is a fractal formed by drawing a triangle, and then using
ID: 664506 • Letter: A
Question
A Sierpinski Triangle is a fractal formed by drawing a triangle, and then using the midpoints of each side of triangle to form another triangle. This inner triangle is then removed. The result is three smaller triangles (one at the top and one in each corner) on which the process is repeated. After iteration N, the image will contain 3N triangles, each of which is similar to the original triangle.
Write a program that implements a recursive algorithm for drawing a Sierpinski Triangle. The user interface for the program should include a JSlider that allows the user to select a value for N. The slider should allow the user to pick a value for N between 0 and the maximum value of N possible based on the size of the program window. The maximum slider value should change as appropriate when the window is resized.
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KochSnowflake extends JApplet implements Sierpinski Triangle
{
private final int Triangle_Top;
private final int Triangle_midpoints;
private final int Triangle_Corner;
private JButton increase,decrease;
private JLabel titleLabel,orderLabel;
private kochPanel drawing;
private JPanel appletPanel,tools;
public void init()
{
tools = new JPanel ();
tools.setLayout (new BoxLayout (tools,BoxLayout.X_AXIS));
tools.setBackground(Color.yellow);
tools.setOpaque(true);
titleLabel = new JLabel ("The KockSnowflake");
titleLabel.setForeground(Color.black);
public void actionPerformed (ActionEvent event)
{
int order = drawing.getOrder();
if (event.getSource() == N)
order++;
else
order--;
if(order >= 0 && oreder <= N);
{
orderLabel.setText ("Order: " + order);
drawing.setOrder (order);
resize();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.