Home / Expert Answers / Computer Science / import-java-awt-color-import-java-awt-graphics-public-class-sierpinskicarpet-public-static-void-pa949

(Solved): import java.awt.Color;import java.awt.Graphics; public class SierpinskiCarpet { public static void ...



student submitted image, transcription available belowstudent submitted image, transcription available below

import java.awt.Color;

import java.awt.Graphics;

public class SierpinskiCarpet {

public static void main(String[] args) {

SierpinskiCarpet t = new SierpinskiCarpet(800, 800);

t.draw(4);

}

private Graphics g;

private int totalWidth, totalHeight;

public SierpinskiCarpet(int totalWidth, int totalHeight) {

this.totalWidth = totalWidth;

this.totalHeight = totalHeight;

DrawingPanel panel = new DrawingPanel(totalWidth, totalHeight);

g = panel.getGraphics();

g.setColor(Color.BLACK);

}

public void draw(int level) {

drawHelper(level, 0, 0, totalWidth, totalHeight);

}

private void drawHelper(int level, int startX, int startY, int width, int height) {

// TODO: implement the recursive call below

// an example to just draw a solid rectangle, feel free to modify to suit your need!

g.fillRect(startX + width/4, startY + height/4, width/2, height/2);

}

}

(40 points) "Recursion" For this project, you are going to draw a "Sierpinski Carpet" by writing a simple recursive function, and leveraging the drawing functions in the provided Drawing Panel. java. Sierpinski Carpet is an interesting recursive pattern like below. As you can see, you divide the carpet into a 3-by-3 grid, and then take out the central piece. Then the same process is applied recursively to the eight surrounding pieces. Level 0 Level 1 Level 2 Level 3 Note: to draw a solid rectangle, you can just use the method "g. fillRect (x, y, width, height)", where " " is the Graphic object (already constructed, provided by the DrawingPane 1). This method will fill up a rectangle with the given start point ( , width and height. Note that for the whole drawing area, the upper left corner has coordinates , the bottom left corner has coordinates ( 0 , totalHeight), the upper right corner has coordinates (totalWidth, 0 ), and the bottom right corner has coordinates (totalWidth, totalHeight). a. Download the attached "SierpinskiCarpet.java" and "DrawingPane . java" Note: When you start New Project in IDE, Make sure you use "Java Project with Existing Sources" and have both files in the SAME package (folder). i. To illustrate how to draw a solid rectangle, you can just run without any modification, you should see something like below: ii. All the other code is written for you already. Just focus on implementing the core recursive logic: private void drawHelper(int level, int startx, int starty, int width, int height) b. For this assignment you must use recursion method introduced in Chapter 12 of the textbook. In particular, do not use nested for loops. Also remember DO NOT change any method signature! c. Remember your program will be graded both on "external correctness" (whether your program compiles and produces exactly the expected outputs), and "internal design and style" (whether your source code follow the style guide). Remember, program that does not compile will be graded 0 in the Correctness part, which means you will lose a significant portion of your points.


We have an Answer from Expert

View Expert Answer

Expert Answer



To implement the recursive logic for drawing the Sierpinski Carpet, you need to modify the drawHelpe...
We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe