Click to See Complete Forum and Search --> : Java - Rectangle + Graph


Valachi
06-20-2009, 06:02 PM
Hey everyone. I need a bit of advise on my code.

Here is a sample image.


^
20+
|
|
|
|
15+
|
|
|
|
10+
|
|
|
|
5+
|
|
|
|
+----+----+----+----+----+----+----+----+>
0 5 10 15 20 25 30 35 40

Upper Left: (5,10) Width: 8, Height: 4

********
********
********

EDIT: noticed the horizontal and vertical numbers are off on the forum here, but fine in program console.




//program START


//create class for filename
public class DrawRectangle

{

//create main arugment to show string text
public static void main(String[] args)

{



//temporarly hard code values.
int height = 4;
int width = 8;
int x = 5;
int y = 10;


//draw the whole graph and rectangle
System.out.println("\n\n ^");

//draw the verticle axis
for (int m = 20; m > 0; m--)

{

if (m == 5)
System.out.println(" " + m + "+");

else if (m % 5 == 0)
System.out.println(m + "+");

else
System.out.println(" |");

}


System.out.print(" ");

//draw the horizontal axis
for (int n = 0; n <= 40; n++)

{
if (n % 5 == 0)

System.out.print("+");

else
System.out.print("-");
}

System.out.print(">");

System.out.println();

//create numbers for the horizontal axis
for (int p = 0; p <= 40; p++)

{
if (p % 5 == 0)

System.out.print(" " + p);
}


//draw the description
System.out.println("\n\n Upper Left: (" + x + "," + y + ") Width: " + width + ", Height: " + height + "\n\n");



//printing the rectangle
for (int hcount = 1; hcount <= height; hcount++)

{
System.out.println("");

for(int wcount = 1; wcount <= width; wcount++)

System.out.print("*");
}


} // main method end brace.

} // drawrectangle class end brace

//program END




So what is my problem?

Well I need the "*" rectangle to fit on the graph, on the PROPER coordinates.

Basically, I just need a starting point for the code highlighted in red to execute.

Now I just can't seem to figure it out.

I'm wondering... if x = n (the horizontal axis) and if y = m (the vertical axis). Then I'd assume the rectangle would execute at the correct coordinates?

I'm just a bit blank on how to execute the rectangle loop.

Thanks.

BTW, I know the rectangle code NEEDS to be moved up, I placed it at the bottom for now.

Totte_ch
07-06-2009, 04:07 PM
Here is one way to do it. (see attached file)
It took a while, but I made it! Some thinking is good for the brain! ;P

Output:

y
^
20 +
|
|
|
|
15 +
|
|
|
|
10 + ********
| ********
| ********
| ********
|
5 +
|
|
|
|
0 +----+----+----+----+----+----+----+----+> x
0 5 10 15 20 25 30 35 40

Rectangle: (5,10) Width: 8, Height: 4