Question

In: Computer Science

please use java swing and recursion and the suggested method hints listed Objective: Write a program...

please use java swing and recursion and the suggested method hints listed

Objective:

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:

I

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:

Solutions

Expert Solution

please please thumbs up!!!

hope it will help uh out!!

Code::

(IN JAVA PROGRAMMING LANGUAGE)
--------------------------------------------------------

import java.awt.*;
import javax.swing.JApplet;
class Fractals extends JApplet {
private static final int SIZE = 400; // height/width of applet
public void init() {
setPreferredSize(new Dimension(SIZE, SIZE)); // settin up window
}
public void paint(Graphics g) {
super.paint(g);
// finding the coordinates of top center, bottom left and bottom right
Point p1 = new Point(getWidth() / 2, 0);
Point p2 = new Point(0, getHeight());
Point p3 = new Point(getWidth(), getHeight());
// using white color
g.setColor(Color.WHITE);
// drawing a triangle with p1,p2,p3 as vertices
drawTriangle(p1, p2, p3, g);
// using black color
g.setColor(Color.BLACK);
// drawing fractals with n=half width
drawFractals(p1, p2, p3, g, getWidth() / 2);
}
public static void drawTriangle(Point p1, Point p2, Point p3, Graphics g) {
// creating a polygon with the points and filling it
Polygon p = new Polygon();
p.addPoint(p1.x, p1.y);
p.addPoint(p2.x, p2.y);
p.addPoint(p3.x, p3.y);
g.fillPolygon(p);

}
public static void drawFractals(Point p1, Point p2, Point p3, Graphics g,
int n) {
// checking if n reached the 4px limit
if (n <= 4) {
// base case: simple triangle
drawTriangle(p1, p2, p3, g);
} else {
// get the three middle points between p1 and p2, p1 and p3, p2 and
// p3
Point p4 = new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
Point p5 = new Point((p2.x + p3.x) / 2, (p2.y + p3.y) / 2);
Point p6 = new Point((p1.x + p3.x) / 2, (p1.y + p3.y) / 2);
// using recursive call to draw three sub-triangles, with half
drawFractals(p1, p4, p6, g, n / 2);
drawFractals(p4, p2, p5, g, n / 2);
drawFractals(p6, p5, p3, g, n / 2);
}
}

}


Related Solutions

java program: Please save the program with the name ‘Palindrome.java’ Write a method which return true...
java program: Please save the program with the name ‘Palindrome.java’ Write a method which return true if the string parameter is a palindrome. This is the output example: Lets check if a string is a palindrom Please give me a string: abc No, its not a Palindrom Lets check if a string is a palindrom Please give me a string: noon Yes, its a Palindrom
Recursion practice Write a Java program with functions for each of the following problems. Each problem...
Recursion practice Write a Java program with functions for each of the following problems. Each problem should be solved by writing a recursive function. Your final program should not have any loops in it. All of your solutions should be in a single .java file. The main function of the file should demonstrate each of your solutions, by running several tests and producing the corresponding outputs. Write a recursive method to 1. calculate power of a give number 2. multiply...
This is for Java programming. Please use ( else if,) when writing the program) Write a...
This is for Java programming. Please use ( else if,) when writing the program) Write a java program to calculate the circumference for the triangle and the square shapes: The circumference for: Triangle = Side1 + Side2 +Sid3 Square = 4 X Side When the program executes, the user is to be presented with 2 choices. Choice 1 to calculate the circumference for the triangle Choice 2 to calculate the circumference for the square Based on the user's selection the...
Using Java (Swing) language(please hard code)... Create a program that has a textfield for the user...
Using Java (Swing) language(please hard code)... Create a program that has a textfield for the user to type in a set of input. Below that textfield have the following controls to show string manipulations: (1) A button that will change the entire textfield’s current text to uppercase. (2) A button with its own textfield for a search value, that will tell the position the search value appears in the textfield above. (3) A button that reports the current number of...
Write a java program with the following classes: Class Player Method Explanation: play : will use...
Write a java program with the following classes: Class Player Method Explanation: play : will use a loop to generate a series of random numbers and add them to a total, which will be assigned to the variable score. decideRank: will set the instance variable rank to “Level 1”, “Level 2”, “Level 3”, “Level 4” based on the value of score, and return that string. getScore : will return score. toString: will return a string of name, score and rank....
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print all elements of a linked list in order. B. Method to print all elements of a linked list in reverse order. C. Method to print all elements of a linked list in order starting from specific position. D. Method to join two linked lists into the first list in the parameters. E. Method to clone a linked list. The copy list has to be...
In Java please. Thank you! Recursion For this assignment you are going to write six different...
In Java please. Thank you! Recursion For this assignment you are going to write six different methods. Each method is to be written recursively. Any method that is written iteratively will not receive any credit, even if it is correct and produces the same results or output. You will be given a starter file. You are not allowed to change the signatures of any of the given methods. You are not allowed to add any methods to your solutions. Write...
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use recursion "...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use recursion " /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public boolean isSymmetric(TreeNode...
using Dr java Objective: Write a program that takes a phrase and then counts the number...
using Dr java Objective: Write a program that takes a phrase and then counts the number of vowels (case does not matter) in the phrase. It then should display all of the vowels in sorted ascending order according to their count. Only consider {AEIOU} as the vowels. Hint: It may be a good idea to keep track and sort two arrays: Has the vowels in alphabetic order Has the number of said vowels Whenever one would swap then it swaps...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT