Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A Sierpinski Triangle is a fractal formed by drawing a triangle, and then using

ID: 3701141 • 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 origi- nal 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 pos sible based on the size of the program window. The maximum slider value should change as appropriate when the window is resized. PP 12.11

Explanation / Answer

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SierpinskiTriangle extends JApplet {
private JTextField jtfOrder = new JTextField("0", 5); // Order
private SierpinskiTrianglePanel trianglePanel =
new SierpinskiTrianglePanel(); // To display the pattern
  
public SierpinskiTriangle() {
// Panel to hold label, text field, and a button
JPanel panel = new JPanel();
panel.add(new JLabel("Enter an order: "));
panel.add(jtfOrder);
jtfOrder.setHorizontalAlignment(SwingConstants.RIGHT);
  
// Add a Sierpinski Triangle panel to the applet
add(trianglePanel);
add(panel, BorderLayout.SOUTH);
  
// Register a listener
jtfOrder.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
trianglePanel.setOrder(Integer.parseInt(jtfOrder.getText()));
}
});
}
  
static class SierpinskiTrianglePanel extends JPanel {
private int order = 0;
  
/** Set a new order */
public void setOrder(int order) {
this.order = order;
repaint();
}
  
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
  
// Select three points in proportion to the panel size
Point p1 = new Point(getWidth() / 2, 10);
Point p2 = new Point(10, getHeight() - 10);
Point p3 = new Point(getWidth() - 10, getHeight() - 10);
  
displayTriangles(g, order, p1, p2, p3);
}
  
private static void displayTriangles(Graphics g, int order,
Point p1, Point p2, Point p3) {
if (order == 0) {
// Draw a triangle to connect three points
g.drawLine(p1.x, p1.y, p2.x, p2.y);
g.drawLine(p1.x, p1.y, p3.x, p3.y);
g.drawLine(p2.x, p2.y, p3.x, p3.y);
}
else {
// Get the midpoint on each edge in the triangle
Point p12 = midpoint(p1, p2);
Point p23 = midpoint(p2, p3);
Point p31 = midpoint(p3, p1);

// Recursively display three triangles
displayTriangles(g, order - 1, p1, p12, p31);
displayTriangles(g, order - 1, p12, p2, p23);
displayTriangles(g, order - 1, p31, p23, p3);
}
}
  
private static Point midpoint(Point p1, Point p2) {
return new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
}
}

public static void main(String[] args) {
JFrame frame = new JFrame("SierpinskiTriangle");
SierpinskiTriangle applet = new SierpinskiTriangle();
frame.add(applet);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote