1 Reply Latest reply on May 21, 2013 8:21 PM by morgalr

    help in recursive tree , java , netbeans

    1001815
      hello I've been trying to draw simple recursive tree

      we must change this code to something that can draw the same thing but from depth 4 to 10 and constant angle , using delay (500) each time , so that we can see that tree being drawing

      also there's a bonus if we can erase everything (flush) when it ends drawing the tree, then draw it and erase it and so on , until someone exit ( using x at the corner )
       
      import java.awt.*;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.util.Random;
       
      import javax.swing.*;
      import javax.swing.event.ChangeEvent;
      import javax.swing.event.ChangeListener;
       
      public class TestBranches extends JPanel {
       
          private Random random;
          private long seed = System.currentTimeMillis();
          private int depth = 8;
          private int angle = 40;
          private boolean singleLine = false;
          
          public void setDepth(int depth) {
              this.depth = depth;
              this.seed = System.currentTimeMillis();
              repaint();
          }
       
          public void setAngle(int angle) {
              this.angle = angle;
              this.seed = System.currentTimeMillis();
              repaint();
          }
          
          public void setSingleLine(boolean singleLine) {
              this.singleLine = singleLine;
              repaint();
          }
          
          @Override
          protected void paintComponent(Graphics g) {
              super.paintComponent(g);
       
              Graphics2D gg = (Graphics2D) g;
              gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                      RenderingHints.VALUE_ANTIALIAS_ON);
              random = new Random(seed);
              drawBranch(gg, depth, getWidth() / 2,
                      getHeight() + getHeight() / 4, getHeight() / 2);
          }
       
          private void drawBranch(Graphics2D g, int depth, int x, int y, int length) {
              if(depth == 0 || length < 2) {
                  return;
              }
              int green = (10 - depth - (10 - this.depth) / 2) * 25;
              int red = random.nextInt(Math.max(green, 1));
              int blue = random.nextInt(Math.max(green, 1));
              g.setColor(new Color(red, green, blue));
              int childY = y - length;
              if(singleLine) {
                  g.drawLine(x, y, x, childY);
              }
              else {
                  int half = Math.max(depth / 2, 1);
                  g.fillRect(x - half, childY, half, y - childY);
              }
       
              int childDepth = depth - 1;
              int halfLength = length / 2;
              int quarterLength = (int) Math.max(halfLength / 4.0, 1);
              int halfAngle = angle / 2;
              int branchAngle = -90 + halfAngle + random.nextInt(halfAngle);
              int totalAngle = branchAngle;
              while(totalAngle < (90 - halfAngle)) {
                  g.rotate(Math.toRadians(branchAngle), x, childY);
                  int childLength = halfLength + random.nextInt(quarterLength);
                  drawBranch(g, childDepth, x, childY, childLength);
                  branchAngle = halfAngle + random.nextInt(halfAngle);
                  totalAngle += branchAngle;
              }
              g.rotate(Math.toRadians(-(totalAngle - branchAngle)), x, childY);
          }
       
          public static void main(String[] args) {
              EventQueue.invokeLater(new Runnable() {
                  public void run() {
                      final TestBranches tree = new TestBranches();
                      tree.setBorder(BorderFactory.createEmptyBorder(
                              10, 10, 10, 10));
                      tree.setPreferredSize(new Dimension(800, 600));
       
                      final JSpinner depthSpinner = new JSpinner(
                              new SpinnerNumberModel(tree.depth, 1, 10, 1));
                      depthSpinner.addChangeListener(new ChangeListener() {
                          @Override
                          public void stateChanged(ChangeEvent e) {
                              tree.setDepth((Integer) depthSpinner.getValue());
                          }
                      });
                      
                      final JSpinner angleSpinner = new JSpinner(
                              new SpinnerNumberModel(tree.angle, 30, 90, 5));
                      angleSpinner.addChangeListener(new ChangeListener() {
                          @Override
                          public void stateChanged(ChangeEvent e) {
                              tree.setAngle((Integer) angleSpinner.getValue());
                          }
                      });
                      
                      final JCheckBox lineCheck = new JCheckBox(
                              "Single line", tree.singleLine);
                      lineCheck.addActionListener(new ActionListener() {
                          @Override
                          public void actionPerformed(ActionEvent e) {
                              tree.setSingleLine(lineCheck.isSelected());
                          }
                      });
                      JPanel options = new JPanel();
                      options.add(new JLabel("Depth"));
                      options.add(depthSpinner);
                      options.add(new JLabel("Angle"));
                      options.add(angleSpinner);
                      options.add(lineCheck);
                      
                      JFrame frame = new JFrame("Recursive branches");
                      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                      frame.getContentPane().add(options, BorderLayout.PAGE_START);
                      frame.getContentPane().add(tree);
                      frame.pack();
                      frame.setLocationRelativeTo(null);
                      frame.setVisible(true);
                  }
              });
          }
      }
        • 1. Re: help in recursive tree , java , netbeans
          morgalr
          So what is the bonus?

          And how do you suppose that it's going to make any difference to me?

          Since I don't get any part of the grade you'll get in class and I've already done my course work and have 20+ years in the programming industy... I'm well motivated by money and not helping your cheat for a grade, but if you have enough money, perhaps we could come to a happy medium.

          BTW: my minimum fee is 500 USD to consult on any project and goes to an additional hourly rate after the first 2 hours.