Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / April 2005

Tip: Looking for answers? Try searching our database.

No compilation error but not the required output

Thread view: 
kt - 16 Apr 2005 17:21 GMT
This program is to input 20 numbers in the range 0 to 100 and display a
histogram that shows how many numbers are in the range 0 to 9, 10 to 19, 20
to 29..
public class Histogram extends Applet
{
private int [] num = {5, 7, 8, 19, 25, 30, 37, 41, 43, 50, 52, 55,
61, 63, 70, 76, 84, 91, 95, 98}; // input array

private int [] hist = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // histogram count

public void drawHist()
{
for( int i = 0; i <num.length; i++)
{
hist[num[i] / 10] += 1;
}

}

public void paint (Graphics g)
{
int x = 20;
int y = 20;
int height = 40;

for(int i = 0; i < hist.length; i++)
{
drawHist();
int width = hist[i];
g.fillRect(x, y, width * 10, height);
x = x + 10;
}

}

}

But i see a solid black rectangle that stretches when the applet window is
dragged. It doesn't look like histogram. I don't know what am i doing wrong.
Any help appreciated,
Kt
Frederik Coppens - 17 Apr 2005 18:50 GMT
You're drawing a number of horizontal lines that overlap.
That's why you see one big rectangle.
Also, you're calling drawHist 10 times. It's enough to call it once, before
the loop.

> This program is to input 20 numbers in the range 0 to 100 and display a
> histogram that shows how many numbers are in the range 0 to 9, 10 to 19,
[quoted text clipped - 39 lines]
> Any help appreciated,
> Kt


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.