Created
October 1, 2018 01:41
-
-
Save memish/c806c96b855883e4da9392567b822a46 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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