Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Topic: Java Programming A Sierpinski Triangle is a fractal formed by drawing a triangle, and then using the midpoints of each side of triangle to

Topic: Java Programming

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 3^N triangles, each of which is similar to the original triangle.

Task: Modify the following program so the maximum slider value changes, as appropriate, when the window is resized.

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

public class Driver extends JFrame implements ChangeListener{ private JSlider slider = new JSlider(JSlider.HORIZONTAL); private SierTriangle triangle = null; public Driver() { triangle = new SierTriangle(); triangle.setBackground(Color.WHITE); this.getContentPane().add(triangle, BorderLayout.CENTER); JPanel orderPanel = new JPanel(); orderPanel.add(new JLabel("Enter a Level: ")); orderPanel.add(slider); orderPanel.setBackground(Color.WHITE);

slider.setMaximum(10); slider.setMinimum(0); slider.setValue(0); Hashtable labels = new Hashtable(); labels.put(0, new JLabel("0")); labels.put(2, new JLabel("2")); labels.put(4, new JLabel("4")); labels.put(6, new JLabel("6")); labels.put(8, new JLabel("8")); labels.put(10, new JLabel("10")); slider.setMajorTickSpacing(1); slider.setLabelTable(labels); slider.setPaintTicks(true); slider.setSnapToTicks(true); slider.setPaintLabels(true); slider.addChangeListener(this); this.getContentPane().add(orderPanel, BorderLayout.SOUTH); this.setPreferredSize(new Dimension(750,750)); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.pack(); this.setVisible(true); } public void stateChanged(ChangeEvent e) { triangle.setLevel(slider.getValue()); } @SuppressWarnings("unused") public static void main(String[] args) { Driver fr = new Driver(); } private class SierTriangle extends JPanel { private int level = 0; public void setLevel(int v) { level = v; repaint(); } public void paintComponent(Graphics g){ super.paintComponent(g); Point p1 = new Point(this.getWidth() / 2, 10); Point p2 = new Point(10, this.getHeight() - 10); Point p3 = new Point(this.getWidth() - 10, this.getHeight() - 10); displayTriangle(g, level, p1, p2, p3); } public Point midpoint (Point a, Point b){ Point mid = new Point(); mid.x = (a.x + b.x) / 2; mid.y = (a.y + b.y) / 2; return mid; } public void displayTriangle(Graphics g, int level, Point p1, Point p2, Point p3) { if (level == 0){ 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{ Point p1_2 = midpoint(p1, p2); Point p2_3 = midpoint(p2, p3); Point p3_1 = midpoint(p3, p1);

displayTriangle(g, level - 1, p1, p1_2, p3_1); displayTriangle(g, level - 1, p1_2, p2, p2_3); displayTriangle(g, level - 1, p3_1, p2_3, p3); } } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

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

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

C++ Database Development

Authors: Al Stevens

1st Edition

1558283579, 978-1558283572

More Books

Students also viewed these Databases questions

Question

4 of 4 Answered: 1 week ago

Answered: 1 week ago