Skip to content

Instantly share code, notes, and snippets.

@memish
Created October 1, 2018 01:41
Show Gist options
  • Select an option

  • Save memish/c806c96b855883e4da9392567b822a46 to your computer and use it in GitHub Desktop.

Select an option

Save memish/c806c96b855883e4da9392567b822a46 to your computer and use it in GitHub Desktop.
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
public class DrawStuff {
private JFrame frame;
public DrawStuff() {
frame = new JFrame("Sierpinski Triangle");
frame.setSize(1024, 768);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setPreferredSize(frame.getSize());
frame.add(new DrawShapes(frame.getSize()));
frame.pack();
frame.setVisible(true);
}
public static void main(String... argv) {
new DrawStuff();
}
public static class DrawShapes extends JPanel {
int startX = 100;
int startY = 400;
int startSide = 300;
public DrawShapes(Dimension dimension) {
setSize(dimension);
setPreferredSize(dimension);
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
Dimension d = getSize();
drawTriangle(g2, startX, startY, startSide);
}
private void drawTriangle(Graphics2D g, int x, int y, int side) {
g.drawLine(x, y, x + (side/2), y - side);
g.drawLine(x + (side/2), y - side, x + side, y);
g.drawLine(x, y, x + side, y);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment