I'm trying to figure out how to draw Sierpinksi's triangle with a pixel limit of 4. Here is my code so far.
import java.awt.*
import java.swing.*
public class SierpTriangle extends Canvas{
public static void main(String[] args) {
JFrame frame = new JFrame("Sierp Triangle");
frame.setSize(900, 900);
SierpTriangle st = new SierpTriangle();
frame.add(st);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
drawSTriangle(0, this.getSize().height, this.getSize().height, g);
}
public void drawSTriangle(int x, int y, int size, Graphics g) {
int sub = size / 2;
int[] xPoints = {x, x + size / 2, x + size};
int[] yPoints = {y, y - size, y};
g.setColor(Color.BLACK);
g.fillPolygon(xPoints, yPoints, 3);
if (sub >= 4) {
drawSTriangle(x, y, sub, g);//LEFT
drawSTriangle(x + sub / 2, y - sub, sub, g);//TOP
drawSTriangle(x + sub, y, sub, g);//RIGHT
}
}
}
It seems to show a large grey triangle instead of a Sierpenki's triangle with 4 layers. Can anyone help?