Jump to content

Disappearing square

Featured Replies

Write a program which start by drawing a 200 X 200 square at the top-left corner of the monitor.

Continue drawing squares that become steadily smaller as they are displayed to right side of each preceding square.

The bottom of the square stays at the same level. Each square is 25 percent smaller than the previously drawn square.

Start with the student version, shown below.

 

In the student version you are provided with a completed main method. This method is designed to execute graphics application, which done by completing the Windows class.

Make no changes to the main method.

 

Inside the Windows class, you will need to use a paint method that controls the graphics output with an object of the Graphics class.

You will also need to create a method, called drawSquare, which actually draws the squares and calls itself recursively until the base case is reached.

 

The recursive process exits when it is true that either the square size becomes smaller than four pixels or the border of the monitor is reached.

You must be aware that only full-sized squares must be displayed. Keep this in mind as you determine if you are reaching the border of the monitor.

 

 

 

80 Point Version

 

The 80 point version draws one set of squares from left to right, starting with a 200X200 square

that is drawn with a top-left corner at coordinate (0,100). Each square is drawn with a 10 pixel space between the squares.

 

100 Point Version

 

The 100 point version draws a second set of squares in the same manner as the 80 point version,

but this time the large square start on the right side and successive squares are drawn moving to the left side of the screen.

 

 
// Lab19bst.java 
// The student version of the Lab19b assignment. 


import java.awt.*; 
import java.awt.event.*; 

public class lab19b 
{ 
public static void main(String args[]) 
{ 
Windows win = new Windows(); 
win.setSize(1000,750); 
win.addWindowListener(new WindowAdapter() {public void 
windowClosing(WindowEvent e) {System.exit(0);}}); 
win.show(); 
} 
} 

class Windows extends Frame 
{ 

public void paint(Graphics g) 
{ 
g.fillRect(0,100,200,200); 
} 

public static int drawSquare(int width, int height) 
{ 
if (height < 0 && width < 0) 
return drawSquare(width - 5, height - 5);//How would you tell it to decrease by 25% and also move over? 
} 

} 

 

 

This is what I have so far. Could somebody tell me if I am on the right track? Also, how is the Graphics method supposed to display the recursive square. Do I need to put the Graphics inside the recursive method? Thank you for your help!

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.