A Sierpinski triangle is a recursive structure of triangles within triangles. The order of the Sierpinski triangle refers to how many levels of nested triangles to draw. There is one outer triangle that points upward. Then we can draw a downward facing triangle within it. This then creates three new upward facing triangles (above, left, and right of the downward one), in which we can repeat this process. For example:
Write a program that takes the order as a command-line argument (e.g., java Sierpinski 2) and draws the corresponding Sierpinski triangle. You can use Sierpinski.java as a starting point. Your program should use recursion. Due to integer rounding, your images might look slightly different than those above.
To draw a triangle, you may want to use the g.drawPolygon(xs, ys, n) method, where xs is a list of x-coordinates, ys a list of y-coordinates, and n the number of vertices.
OUTPUT
Javac Sierpinski.java
java Sierpinski 2 ---> should give order 2 figure
//Start of the code using this import java.awt.*; import javax.swing.*; public class Sierpinski { public int n; public int size; public void paint(Graphics g) { // TODO } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Sierpinski drawing = new Sierpinski(); drawing.n = Integer.parseInt(args[0]); drawing.size = 700; drawing.setSize(drawing.size, drawing.size); frame.add(drawing); frame.pack(); frame.setVisible(true); } }