Forums Register Login

disappearing square

+Pie Number of slices to send: Send
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.

Code:
// 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!
+Pie Number of slices to send: Send
perhaps you should be using drawRect() instead of fillRect()??

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

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

3)
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:
1)
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
2)
create a rectangle based on the arguments supplied to drawSquare()
3)
add the rectangle to the arraylist
4)
call repaint(), so the rectangles will be drawn
5)
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
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
> I redited my code,

did you really?
http://forum.java.sun.com/thread.jspa?threadID=712083&tstart=0

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.
Look! I laid an egg! Why does it smell like that? Tiny ad, does this smell weird to you?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2459 times.
Similar Threads
array of array help
Class to draw a circle to Jframe
A Quadrilateral extend program
help with oval
Simple Programming exercise
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 20:32:30.