Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Run the Viewer of the Sierpinski Triangles and give a description of what you observed as it relates to recursion. Write a short summary (2-3

Run the Viewer of the Sierpinski Triangles and give a description of what you observed as it relates to recursion. Write a short summary (2-3 sentences is enough) of your observations. Feel free to explain what the recursive solution is doing by checking out its ControlPanel. This is a picture when I run the code.

image text in transcribed

import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.awt.Polygon; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; public class ControlPanel extends JPanel { public static final long serialVersionUID = 1L; public static final int WIDTH = 500; public static final int HEIGHT = 420; public Color color = new Color(200, 50, 50); private int level = 1; public JButton plusButton = new JButton("+"); public JButton minusButton = new JButton("-"); public JLabel label; public JPanel subPanel = new JPanel(); private Point p1; private Point p2; private Point p3; public ControlPanel() { this.label = new JLabel("n: " + this.level); this.subPanel.setBackground(Color.GRAY); this.plusButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { ++ControlPanel.this.level; ControlPanel.this.label.setText("n: " + ControlPanel.this.level); } }); this.minusButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if (ControlPanel.this.level != 1) { --ControlPanel.this.level; ControlPanel.this.label.setText("n: " + ControlPanel.this.level); } } }); this.subPanel.add(this.plusButton); this.subPanel.add(this.minusButton); this.subPanel.add(this.label); this.setPoints(); } public void drawFigure(Graphics graphic, int level, Point p1, Point p2, Point p3) { if (level == 1) { Polygon triangle = new Polygon(); triangle.addPoint(p1.x, p1.y); triangle.addPoint(p2.x, p2.y); triangle.addPoint(p3.x, p3.y); graphic.setColor(this.color); graphic.fillPolygon(triangle); } else { Point p4 = this.midPoint(p1, p2); Point p5 = this.midPoint(p2, p3); Point p6 = this.midPoint(p1, p3); this.drawFigure(graphic, level - 1, p1, p4, p6); this.drawFigure(graphic, level - 1, p4, p2, p5); this.drawFigure(graphic, level - 1, p6, p5, p3); } this.repaint(); } public Point midPoint(Point p1, Point p2) { return new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2); } public void paintComponent(Graphics graphic) { super.paintComponent(graphic); this.drawFigure(graphic, this.level, this.p1, this.p2, this.p3); } public void setPoints() { this.p1 = new Point(250, 10); this.p2 = new Point(25, 395); this.p3 = new Point(475, 395); } } 
Transcribed image text

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions