Objective in Java: Write a program in which draws (yes it actually makes a pictu
ID: 3790024 • Letter: O
Question
Objective in Java:
Write a program in which draws (yes it actually makes a picture) a triangular fractal using recursion. This is best if done using a java applet.
Suggested Methodology
The idea for it is this
First draw a filled equilateral triangle
Next draw another filled equilateral triangle of a different color that’s upside down in the middle of that triangle
Using the other triangles formed repeat step 2 until a pixel limit of 4 is reached
HINTS:
It may be a good idea to look at the examples I gave 02/12/2015
The method fillPolygon(int[] xPoints, int[] yPoint, numberOfPoints) as called by the graphics device is important
The method setColor(Color aColor) is important for picking different colors to draw things.
Example Image of Results:
Explanation / Answer
import javax.swing.*; import java.awt.*; class FractalPanel extends JPanel { private String type; private Color foreground, background; public FractalPanel(String type, Color foreground, Color background) { super(); this.type = type; this.foreground = foreground; this.background = background; } private static Coord mid(Coord a, Coord b) { return new Coord((a.x+b.x)/2, (a.y+b.y)/2); } private static Coord third(Coord a, Coord b) { return new Coord((b.x-a.x)/3+a.x, (b.y-a.y)/3+a.y); } private static double dist(Coord a, Coord b) { return Math.sqrt(Math.pow(b.x-a.x,2)+Math.pow(b.y-a.y,2)); } private static void drawKochSide(Graphics2D g2, Coord a, Coord b) { //if lines are only 1 pixels in length if (dist(a,b) screen.width) frame.width = screen.width; app.setLocation((screen.width - frame.width) / 2, (screen.height - frame.height) / 2); app.setVisible(true); } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.