File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes disappearing square Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "disappearing square" Watch "disappearing square" New topic

disappearing square

avelin chen

Joined: Dec 11, 2005
Posts: 27
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.

// 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.addWindowListener(new WindowAdapter() {public void
windowClosing(WindowEvent e) {System.exit(0);}});;

class Windows extends Frame

public void paint(Graphics g)

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!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
perhaps you should be using drawRect() instead of fillRect()??

in paint(), because you are going to draw multiple objects, you need to:
use variables for the coordinates e.g
(in the end it won't actually look like this)

create a datastructure to hold the coordinates (at class level).
an ArrayList is a good candidate - the object/s it will hold are Rectangle()

in paint(), you need to loop through the arraylist, painting each rectangle
i.e. in the loop you create a rectangle object from the arraylist's current element
then you use drawRect() using that rectangle's coords

drawSquare() doesn't need to return anything, and it should have 4 parameters,
for each of the rectangle bounds
so it should be
public void drawSquare(int,int,int,int)

in drawSquare() you need:
a 'finished' condition, and because it's a square you only need to check
width or height
if (height <= 1) return;//reason for this, and not 0, is the calcs are percentages
create a rectangle based on the arguments supplied to drawSquare()
add the rectangle to the arraylist
call repaint(), so the rectangles will be drawn
recursively call drawSquare(), using a formula for the new coords.
each rectangle is to be displayed to the right, so its x-coord will be the
x-coord passed in as an argument + the width-coord passed in.
width and height coords are their values passed in *.75
these will need to be cast as int

I'll leave you to experiment with the y-coord, it is not as simple as the x-coord
because the squares need to be level at the bottom. Concentrate on getting
all the squares drawn, then modifying for position
avelin chen

Joined: Dec 11, 2005
Posts: 27
We haven't learned how to use an arrayList yet so I can't do that. I redited my code, and I was wondering if this woud be on the ight track.

Also, could someone please explain to me why I keep on gettin at comple error at my newWidth and newHeight, it says that I have a possible loss of precision. Wouldn't the round make it an int? Thanks!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I redited my code,

did you really?

why don't you have a go yourself, instead of waiting for someone to do it for you.

I won't waste any more of my time on this thread.
I agree. Here's the link:
subject: disappearing square