Question

So I'm creating a recursive H-structure in Java as an exercise and can't figure how to...

So I'm creating a recursive H-structure in Java as an exercise and can't figure how to print my points after I've declared them and the recursive process.

This is what I have so far:

package exercise18_35;

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.util.LinkedList;
import java.util.TreeSet;

/**
*
* @author
*/
public class Exercise18_35 {

    public static void drawH(double x, double y, double size) {
//points
        double x0 = x - size/2;
        double x1 = x + size/2;
        double y0 = y - size/2;
        double y1 = y + size/2;

//Drawing the middle H
         StdDraw.line(x0, y0, x0, y1);    // left vertical segment of the H
         StdDraw.line(x1, y0, x1, y1);    // right vertical segment of the H
         StdDraw.line(x0, y, x1, y);    // crossbar segment of the H
     }

     // plot an order n H-tree, centered on (x, y) of the given side length
     public static void draw(int n, double x, double y, double size) {
         if (n == 0) return;             // base case
         drawH(x, y, size);

         // compute x- and y-coordinates of the 4 half-size H-trees
         double x0 = x - size/2;
         double x1 = x + size/2;
         double y0 = y - size/2;
         double y1 = y + size/2;

         // recursively draw 4 half-size H-trees of order n-1
         draw(n-1, x0, y0, size/2);    // lower left H-tree
         draw(n-1, x0, y1, size/2);    // upper left H-tree
         draw(n-1, x1, y0, size/2);    // lower right H-tree
         draw(n-1, x1, y1, size/2);    // upper right H-tree
     }
// read in a command line argument N and plot an order N H-tree
     public static void main(String[] args, double size) {
         int N = Integer.parseInt(args[0]);

         double x = 0.5, y = 0.5;     // center of H-tree         double size = 0.5;           // side length of H-tree
         draw(N, x, y, size);
       
     }
      
       

0 0
Add a comment Improve this question Transcribed image text
Answer #1


import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class Exercise18_35 extends Application {

    @Override
    public void start(Stage primaryStage) {
        HTreePane hTreePane = new HTreePane();
        TextField tfOrder = new TextField();
        tfOrder.setOnAction(
                e -> hTreePane.setOrder(Integer.parseInt(tfOrder.getText())));
        tfOrder.setPrefColumnCount(4);
        tfOrder.setAlignment(Pos.BOTTOM_RIGHT);

        // Pane to hold label and a text field
        HBox hBox = new HBox(10);
        hBox.getChildren().addAll(new Label("Enter an order: "), tfOrder);
        hBox.setAlignment(Pos.CENTER);

        BorderPane borderPane = new BorderPane();
        borderPane.setCenter(hTreePane);
        borderPane.setBottom(hBox);

        // Create a scene and place it in the stage
        Scene scene = new Scene(borderPane, 200, 210);
        primaryStage.setTitle("Exercise18_35: H-tree fractal");
        primaryStage.setScene(scene);
        primaryStage.show();
      
        // Resize fractal upon change
        scene.widthProperty().addListener(ov -> hTreePane.paint());
        scene.heightProperty().addListener(ov -> hTreePane.paint());
    }

    /**
     * Pane for displaying H-tree
     */
    static class HTreePane extends Pane {
        private int order = 0;
      
        /**
         * Set a new order
         */
        public void setOrder(int order) {
                this.order = order;
                paint();
        }

        HTreePane() {
        }

        protected void paint() {
            // Find center in proportion to the pane size
            Point2D paneCenter = new Point2D(getWidth() / 2, getHeight() / 2);
            double lineLength = Math.min(getWidth() / 3, getHeight() / 3); // Make lines roughly a third of the window

            this.getChildren().clear(); // Clear the pane before redisplay

            displayHTree(order, paneCenter, lineLength);
        }
      
      
        private void displayHTree(int order, Point2D center, double lineLength) {
            double paneXCenter = center.getX();
            double paneYCenter = center.getY();

            Point2D p1 = new Point2D(paneXCenter - lineLength / 2, paneYCenter - lineLength / 2);
            Point2D p2 = new Point2D(paneXCenter - lineLength / 2, paneYCenter + lineLength / 2);
            Point2D p3 = new Point2D(paneXCenter + lineLength / 2, paneYCenter - lineLength / 2);
            Point2D p4 = new Point2D(paneXCenter + lineLength / 2, paneYCenter + lineLength / 2);
          
            // Draw three lines in an H-shape
            getChildren().add(new Line(p1.getX(), p1.getY(), p2.getX(), p2.getY()));
            getChildren().add(new Line(p3.getX(), p3.getY(), p4.getX(), p4.getY()));
            getChildren().add(new Line(paneXCenter - lineLength / 2, paneYCenter, paneXCenter + lineLength / 2, paneYCenter));

            if (order > 0) {
                displayHTree(order - 1, p1, lineLength / 2);
                displayHTree(order - 1, p2, lineLength / 2);
                displayHTree(order - 1, p3, lineLength / 2);
                displayHTree(order - 1, p4, lineLength / 2);
            }
        }
    }
  
    public static void main(String[] args) {
        launch(args);
    }
}

Add a comment
Know the answer?
Add Answer to:
So I'm creating a recursive H-structure in Java as an exercise and can't figure how to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at Art.main(Art.java:29) What...

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at Art.main(Art.java:29) What does this mean/How do I fix this? 1 public class Art { 2 // plot a square, centered on (x, y) of the given side length 3 // with a light gray background and black border 4 public static void drawSquare(double x, double y, double size) { 5 PennDraw.setPenColor(PennDraw.LIGHT_GRAY); 6 PennDraw.filledSquare(x, y, size/2); 7 PennDraw.setPenColor(PennDraw.BLACK); 8 PennDraw.square(x, y, size/2); 9 } 10 // plot...

  • Recursion Tree Goal: Predict the output of a recursive method call using a recursion tree. Draw...

    Recursion Tree Goal: Predict the output of a recursive method call using a recursion tree. Draw the recursion tree of the following source code, showing all method calls and outputs: public class Main { public static void main(String[] args) { f(3, 4); } public static void f(int x, int y) { if(x + y > 1) { f(x - 2, y - 1); System.out.print(x + " "); f(y, x - 2); System.out.print(2 * x + y + " "); }...

  • **JAVA** Hi! Can't figure this out, but if you could give it a shot, I'd appreciate...

    **JAVA** Hi! Can't figure this out, but if you could give it a shot, I'd appreciate it! Everything is explained in the program via comments, and I just need help implementing the methods. Instructions for the methods are commented on top of said method. It's supposedly straightforward. Methods that need to be solved are in bold. Some might already be implemented. Just based off what's in this class, is supposed to lead to the outcome. This is all the info...

  • In java need help with the TODO sections creating recursive methods public class CountUpDown { /**...

    In java need help with the TODO sections creating recursive methods public class CountUpDown { /** * countUp - a recursive function that counts up from 1 to n * * @param n the integer value to count up to */ private static void countUp(int n) { // TODO PRELAB // IMPLEMENT THIS RECURSIVE METHOD } /** * countDown - a recursive function that counts down from n to 1 * * @param n the integer value to count down...

  • The following code computes the radius of a circle. Using static methods from the Math class...

    The following code computes the radius of a circle. Using static methods from the Math class complete the method that computes the radius of a circle using the formula  r2=(x-h)2 +(y-k)2 , given the (x,y) coordinates of one point on its circumference and the (h,k) coordinates of its center. public class Circle {    public static void main(String[] C)    {        double x1 =14.25;        double y1 =13.68;        double xCenter = 25.678;        double yCenter...

  • JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers:...

    JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...

  • Have to write the tree into a text file? JAVA CODE Binary search tree This is...

    Have to write the tree into a text file? JAVA CODE Binary search tree This is the tree public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary search tree public Buildbst(int data) { this.data = data; this.left = null; this.right =null; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Buildbst getLeft() { return left; } public void setLeft(Buildbst left) { this.left = left;...

  • What is wrong with this code please. I need to draw CIRCLES in JFrame JAVA.  recursively Draw...

    What is wrong with this code please. I need to draw CIRCLES in JFrame JAVA.  recursively Draw a circle centered near the top of the JPanel After drawing a circle, your program should draw two more circles half way to the left and right of the circle just drawn and located "one level" lower in the JPanel. Do this drawing as long as the drawing would not be below the bottom of the JPanel Each newly drawn circle then follows the...

  • Java Questions When creating a for loop, which statement will correctly initialize more than one variable?...

    Java Questions When creating a for loop, which statement will correctly initialize more than one variable? a. for a=1, b=2 c. for(a=1, b=2) b. for(a=1; b=2) d. for(a = 1&& b = 2) A method employee() is returning a double value. Which of the following is the correct way of defining this method? public double employee()                                    c. public int employee() public double employee(int t)                  d. public void employee() The ____ statement is useful when you need to test a...

  • JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to...

    JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to maintain a list of shapes drawn as follows: Replace and upgrade all the AWT components used in the programs to the corresponding Swing components, including Frame, Button, Label, Choice, and Panel. Add a JList to the left of the canvas to record and display the list of shapes that have been drawn on the canvas. Each entry in the list should contain the name...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT