Question: Java recursive program Please help me fix the add code part A Sierpinski triangle is analogous to a Sierpinski carpet. To create one, you begin

Java recursive program

Please help me fix the "add code" part

A Sierpinski triangle is analogous to a Sierpinski carpet. To create one, you begin with an equilateral triangle. Connect the midpoints of the sides of the triangle to form four subtriangles, and remove the inner subtriangle. The result is a Level 1 Sierpinski triangle. Repeat the process on each of the remaining three subtriangles to get a Level 2 Sierpinski triangle, as follows:

Java recursive program Please help me fix the "add code" part A

Theoretically, you could continue up to any level, but in practice you will reach a point where you will not be able to see additional triangles.

Using NB_Grafix_Object_Thing, write a recursive program that draws a level n Sierpinski triangle for any integer n between 1 and 7, inclusive, that the user specifies. Your program can set named constants for the vertices and side length of the original outer triangle, or, alternatively, calculate these named constant values from the JPanel("canvas") dimensions (using the JPanel's getWidth() and getHeight() methods).

package nb_grafix_object_thing;

import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.Timer;

/** * Blank form for creating graphical objects * * @author nbashias1 */ public class NB_Grafix_Object_Thing extends javax.swing.JFrame implements ActionListener { private ArrayList Stuff; // generic list of objects private Timer timer; private static final int DELAY = 3000; // 3 seconds

/** * Creates new form Thing_Form */ public NB_Grafix_Object_Thing() { initComponents(); // create the generic stuff ArrayList Stuff = new ArrayList(); // create a timer for this Form // with the given delay timer = new Timer(DELAY, this); // start the timer timer.start(); } // end constructor

/** * method is called each time the form is repainted * * @param graphics the Form's Graphics object */ @Override public void paint(Graphics graphics) { // draw the window title, menu, buttons super.paint(graphics); // get the Graphics context for the canvas Graphics canvas = canvasPanel.getGraphics();

/*** ADD CODE TO DRAW ON THE canvas AFTER THIS LINE ***/

} // end paint method /** * method is called each time mouse is clicked in the form * * add code that does something with the click, like make things... * * @param xCoord x-coordinate of the click * @param yCoord y-coordinate of the click */ public void mouseClickedAt(int xCoord, int yCoord) {

/*** ADD CODE TO REACT TO USER MOUSE CLICK AFTER THIS LINE ***/

} // end mouseClickedAt method /*************************************************** * WARNING: DO NOT ADD ANY CODE BELOW THIS LINE!!! ***************************************************/

/** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // private void initComponents() {

canvasPanel = new javax.swing.JPanel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Graphical Objects");

canvasPanel.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { canvasPanelMouseClicked(evt); } });

javax.swing.GroupLayout canvasPanelLayout = new javax.swing.GroupLayout(canvasPanel); canvasPanel.setLayout(canvasPanelLayout); canvasPanelLayout.setHorizontalGroup( canvasPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 999, Short.MAX_VALUE) ); canvasPanelLayout.setVerticalGroup( canvasPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 737, Short.MAX_VALUE) );

getContentPane().add(canvasPanel, java.awt.BorderLayout.CENTER);

setSize(new java.awt.Dimension(1015, 775)); setLocationRelativeTo(null); }//

private void canvasPanelMouseClicked(java.awt.event.MouseEvent evt) { mouseClickedAt(evt.getX(), evt.getY()); }

/** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(NB_Grafix_Object_Thing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(NB_Grafix_Object_Thing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(NB_Grafix_Object_Thing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(NB_Grafix_Object_Thing.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // //

/* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NB_Grafix_Object_Thing().setVisible(true); } }); }

// Variables declaration - do not modify private javax.swing.JPanel canvasPanel; // End of variables declaration

@Override public void actionPerformed(ActionEvent ae) { repaint(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!