Created
October 3, 2018 00:00
-
-
Save feehe21/16cd1dc8677d516496a2a524d231607c to your computer and use it in GitHub Desktop.
This file contains 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; | |
import java.awt.Color; | |
public class fractal { | |
private JFrame frame; | |
public fractal() { | |
frame = new JFrame("Sierpinski Triangle"); | |
frame.setSize(1200, 700); | |
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 fractal(); | |
} | |
public static class DrawShapes extends JPanel { | |
//initialize variables | |
int startX = 350; | |
int startY = 350; | |
int startSide = 300; | |
int comp = startSide; | |
int compX = startX; | |
int compY = startY; | |
int length = 200; | |
int compY2 = compY - length; | |
int angle = 90; | |
int depth = 9; | |
int x1 = 800; | |
int y1 = 0; | |
int x = 300; | |
int y = 200; | |
int r = 200; | |
boolean shade = false; | |
int startL = startSide; | |
//boolean count = false; | |
public DrawShapes(Dimension dimension) { | |
setSize(dimension); | |
setPreferredSize(dimension); | |
} | |
@Override | |
public void paintComponent(Graphics g) {//call methods | |
Graphics2D g2 = (Graphics2D)g; | |
Dimension d = getSize(); | |
//g.drawOval(startX,startY,-30,-30); | |
drawOval(g2, startX, startY, startSide, comp, compX, compY, shade, startL); | |
drawFractalTree(g2, x1, y1, angle, depth); | |
} | |
private void drawTriangle(Graphics2D g, int x, int y, int side) {//draw sierpinsky triange | |
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); | |
side /= 2; | |
if(side>10){//base case | |
drawTriangle(g, x, y, side); | |
drawTriangle(g, x+side, y, side); | |
drawTriangle(g, x+(side/2), y-side, side); | |
} | |
} | |
private void drawOval(Graphics2D g, int x, int y, int r, int start, int startX, int startY, boolean shade, int startR){ | |
//g.setColor(new Color(255,0,0)); | |
//draw cone | |
if(r>0){//if on right give color | |
if(shade == true){ | |
g.setPaint(new Color(150, 15, 15)); | |
g.fillOval(x,y,Math.abs(r),Math.abs(r)); | |
shade = false; | |
}else{ | |
g.setPaint(new Color(0, 50, 100)); | |
g.fillOval(x,y,Math.abs(r),Math.abs(r)); | |
shade = true; | |
} | |
}else{//make black | |
g.setPaint(new Color(0, 0, 0)); | |
g.drawOval(x,y,Math.abs(r),Math.abs(r)); | |
} | |
r = r-5; | |
System.out.println(r); | |
if(r>=(-1*start)){//if within range | |
if(r<=0){//reverse if negative | |
y = startY-Math.abs(r); | |
x = startX-Math.abs(r); | |
}else{ | |
} | |
drawOval(g, x, y, r, start, startX, startY, shade, startR); | |
} | |
} | |
public void drawFractalTree(Graphics g, int x1, int y1, double angle, int depth) {//draw tree | |
if (depth == 0) { | |
} else {//calculate change | |
int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 10.0); | |
int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 10.0); | |
g.drawLine(x1, y1, x2, y2); | |
drawFractalTree(g, x2, y2, angle + 30, depth - 1); | |
drawFractalTree(g, x2, y2, angle - 30, depth - 1); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment