Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

Fix the code below so that there is a function that calculate the height of the triangle.

 

package recursion;

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

/**
 * Draw a Sierpinski Triangle of a given order on a JPanel.
 * 
 * 
 */
public class SierpinskiPanel extends JPanel {

    private static final int WIDTH = 810;
    private static final int HEIGHT = 830;
    
    private int order;

    /**
     * Construct a new SierpinskiPanel.
     */
    public SierpinskiPanel(int order) {
        
        this.order = order;
        
        this.setMinimumSize(new Dimension(WIDTH, HEIGHT));
        this.setMaximumSize(new Dimension(WIDTH, HEIGHT));
        this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    }
    
    public static double height(double size) {
        double h = (size * Math.sqrt(3)) / 2.0;
        return h;
    }
    /**
     * Draw an inverted triangle at the specified location on this JPanel.
     * 
     * @param x the x coordinate of the upper left corner of the triangle
     * @param y the y coordinate of the upper left corner of the triangle
     * @param size the width and height of the triangle
     * @param g the graphics object on which to draw the triangle
     */
    private void drawTriangle(int x, int y, int size, Graphics g) {
        Polygon triangle = new Polygon();
        double h = height(size);
        triangle.addPoint(x, y);
        triangle.addPoint(x - size /2, (int) (y + h));
        triangle.addPoint(x + size / 2, (int) (y + h));
        g.fillPolygon(triangle);
    }
    
    /**
     * Draw a Sierpinski triangle of the specified order at the specified location
     * on this JPanel.
     * 
     * @param order the order of the Sierpinski triangle to draw.
     * @param x the x coordinate of the upper left corner of the central order 1 triangle
     * @param y the y coordinate of the upper left corner of the central order 1 triangle
     * @param size the width and height of the triangle
     * @param g the graphics object on which to draw the triangle
     */
    private void drawSierpinski(int order, int x, int y, int size, Graphics g) {
        // fill in this method.  
        
        /*
         *  You'll want to make use of the drawTriangle method to do the
         *  actual drawing of the triangles.
         */
        
        drawTriangle(x, y, size, g);
        
        if (order == 1) {
            
        } else {
            drawSierpinski(order - 1, x + size / 4, y - size / 2, size / 2, g);
            drawSierpinski(order - 1, x - size / 4, y + size / 2, size / 2, g);
            drawSierpinski(order - 1, x + 3 * size / 4, y + size / 2, size / 2, g);
        }
    }

    /**
     * Override the paintComponent method so that each time the JPanel is painted on
     * the screen we can draw the Sierpinski Triangle on the JPanel.
     * 
     * @param g the graphics object on which to draw the triangle.
     */
    public void paintComponent(Graphics g) {
        g.setColor(Color.BLACK);
        drawSierpinski(order, 205, 425, 400, g);
    }

    /*
     * NOTE: Some code adopted/adapted from an earlier COMP132 lab created by
     * Tim Wahls who in turn borrowed some of the code from Robert Sedgewick.
     */
}

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education