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 2D array / recursion explain every method You are requested to write a Java program...
java 2D array / recursion explain every method You are requested to write a Java program of a simple Memory Management Unit. The program should allow the following: 1. The user can create a process asking for memory. The program will return a process ID if the requested memory can be allocated. It will also print the allocated Base and Limit. 2. The user can delete a process by specifying a process ID. The program should do that and free...
USE JAVA PLEASE Use recursion to implement a method public static int indexOf(String text, String str)...
USE JAVA PLEASE Use recursion to implement a method public static int indexOf(String text, String str) that returns the starting position of the first substring of the text that matches str. Return –1 if str is not a substring of the text. For example, s.indexOf("Mississippi", "sip") returns 6. Hint: You must keep track of how far the match is from the beginning of the text. Make that value a parameter variable of a helper method.
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
Java Recursion (Timelimit: 3 seconds) Problem Description Write a Java program to solve the following problem....
Java Recursion (Timelimit: 3 seconds) Problem Description Write a Java program to solve the following problem. Recursion may appear in various contexts and in different forms. For fast implementation, we should always aim at transforming recursions into a simpler form of computation. In this assignment, the task is to evaluate X(·), which is defined as follows:               |0,if m = 0 or n = 0               | X(m,n−1),if n is odd and m is even X(m,n) = | X(m−1,n),if m...
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...
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...
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...
Please use JAVA to do this: Write a method that takes four strings as parameter. The...
Please use JAVA to do this: Write a method that takes four strings as parameter. The first string should be a pokemon name, the second a pokemon type(either fire, water, or leaf, where water beats fire, fire beats leaf and leaf beats water), the third a pokemon name, and the fourth a pokemon type. The method should print out which pokemon has the advantage over the other based on their type. Example: Pokemon X(which is the fire type) has the...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT