Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Drawing rectangle using stars in java

Sat Jun 20, 2009 10:01 am

Drawing rectangle using stars * in java
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.


Here is the code.

Code:
/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");
       
 
      
   [color=#FF0000]//printing the rectangle
   for (int hcount = 1; hcount <= height; hcount++)
           
   {
      System.out.println("");
       
      for(int wcount = 1; wcount <= width; wcount++)
           
      System.out.print("*");
   }[/color]
     
       
    } // 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.



Post a reply
  Related Posts  to : Drawing rectangle using stars in java
 Java Rectangle Class     -  
 Triangle of stars (C++)     -  
 Add voting by stars images to your website content     -  
 Ellipse in Rectangle     -  
 Draw Fill Rectangle     -  
 Draw rectangle on image     -  
 How to Calculate Volume of a Rectangle     -  
 ellipse, circle, rectangle, square, or parallelogram     -  
 Draw Oval,Arc,Polygon,string,Line,Round and 3D Rectangle     -  
 Drawing Lines in php     -