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 Drawing in a loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Drawing in a loop" Watch "Drawing in a loop" New topic

Drawing in a loop

Graham Robinson

Joined: Nov 23, 2005
Posts: 16
Hi, this code should allow the value entered by the user to set the height of a bar. Then allow the user to enter 12 values, which it does, but only prints one bar. Does it need it's own loop, such as a while? If so where would i put this in? I can't think what the conditions would be, seeming they're allready set.


import element.*;
import java.awt.Color;

* Prompt user for 12 values of rainful
* and produce the heaviest, average value and bar chart.
* @Author Graham Robinson
* @version 1 November 2005

public class AuchenWeather

public static void main (String[] args)

//consrtuct new console window
ConsoleWindow c = new ConsoleWindow();

// construct a drawing window object
DrawingWindow d = new DrawingWindow(370, 300);

int rain, heaviest = 0, entry = 0, x = 10, height;
final int y = 300, width = 20;
boolean valid;
float total = 0.0f, topEntry = 0.0f, average = 0.0f;

//for control
for(int count = 0; count < 12; count++)
c.out.println("Please input value of rainfull betweem 0 and 200mm.");
rain = c.input.readInt();//rainfall value entered by user
valid = (rain >= 0 && rain <= 200);
total += rain;//total of rainfall
entry++;//number of entries
x = x + 30;
height = rain;

d.move(x,y);// Draw rectangle
d.line(0, - height);
d.line(0, height);
d.line(- width, 0);

if (!valid)
c.out.println("The value entered isn't valid.");
}while (!valid);

if (rain > heaviest)
heaviest = rain;
average = total / entry;

}//end of for

c.out.println("Heaviest rainfall = " + heaviest);
c.out.println("Average rainfall = " + average);

}//end of class
}//end of main
Vijay Raj
Ranch Hand

Joined: Oct 10, 2005
Posts: 110
Don't you think that you should only draw the rectangle when the variable 'valid' is true. In the code above, it'll increment x even when 'valid' is false (and I guess x stands for the value along x-axis).

The loops see to be alright but I did not understand the code written to draw the rectangle.

I agree. Here's the link:
subject: Drawing in a loop
It's not a secret anymore!