Please add a line(s) of code to the java code below. And comment the java code. Refer to image below and show output of the sierpenski triangle import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; public class SierpinskiTriangle extends JFrame { public SierpinskiTriangle() { setTitle("Sierpinski Triangle"); setSize(1000,1000); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } public void paint(Graphics g) { g.setColor(Color.BLUE); sierpinski(g, 500, 250, 500); } public void sierpinski(Graphics g, int x1, int y1, int side) { int x2 = x1 + side; int x3 = x1 + side / 2; int y2 = y1 - (int) (Math.sqrt(3.0) / 2.0 * side); int y3 = y1 + (int) (Math.sqrt(3.0) / 2.0 * side); if (side < 10) { g.drawLine(x1, y1, x2, y1); g.drawLine(x2, y1, x3, y2); g.drawLine(x3, y2, x1, y1); } else { sierpinski(g, x1, y1, side / 2); sierpinski(g, x2, y1, side / 2); sierpinski(g, x3, y2, side / 2); } } public static void main(String[] args) { SierpinskiTriangle st = new SierpinskiTriangle(); } }
Please add a line(s) of code to the java code below. And comment the java code. Refer to image below and show output of the sierpenski triangle
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class SierpinskiTriangle extends JFrame {
public SierpinskiTriangle() {
setTitle("Sierpinski Triangle");
setSize(1000,1000);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
g.setColor(Color.BLUE);
sierpinski(g, 500, 250, 500);
}
public void sierpinski(Graphics g, int x1, int y1, int side) {
int x2 = x1 + side;
int x3 = x1 + side / 2;
int y2 = y1 - (int) (Math.sqrt(3.0) / 2.0 * side);
int y3 = y1 + (int) (Math.sqrt(3.0) / 2.0 * side);
if (side < 10) {
g.drawLine(x1, y1, x2, y1);
g.drawLine(x2, y1, x3, y2);
g.drawLine(x3, y2, x1, y1);
} else {
sierpinski(g, x1, y1, side / 2);
sierpinski(g, x2, y1, side / 2);
sierpinski(g, x3, y2, side / 2);
}
}
public static void main(String[] args) {
SierpinskiTriangle st = new SierpinskiTriangle();
}
}
Step by step
Solved in 4 steps with 3 images