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:

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") //
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 */ //
/* 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
Get step-by-step solutions from verified subject matter experts
