• Post Reply Bookmark Topic Watch Topic
  • New Topic

No compilation error, but not the required output  RSS feed

 
kanaka tam
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you see only one bar? Is x across from the left or down from the top? And how thick is each bar?

Why does the bar get longer when you resize the window? Because paint() gets called every time the window contents are redisplayed. If you resize from 300x300 to 800x600, paint might get called about 50 times -- more or less depending on how fast you drag and how fast your computer is. What happens to the values in hist each time paint() is called?

(edit: Oops, I take that back. I guess paint() only gets called when you let go of the corner at the new location, not every little increment along the way.)

Ryan
[ April 16, 2005: Message edited by: Ryan McGuire ]
 
kanaka tam
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ryan McGuire:
Why do you see only one bar? Is x across from the left or down from the top? And how thick is each bar?

Why does the bar get longer when you resize the window? Because paint() gets called every time the window contents are redisplayed. If you resize from 300x300 to 800x600, paint might get called about 50 times -- more or less depending on how fast you drag and how fast your computer is. What happens to the values in hist each time paint() is called?

Ryan
[ April 15, 2005: Message edited by: Ryan McGuire ]

Ryan, Thanx a lot for your help. Couldn't have gotten this far without those valuable hints of yours. Now the problem is i only see one thick rectangle from left to right that stretches with the applet window. I am a novice to Java - so i am going to ask this question even if it is not sensible. I thought paint would be called again and again only when method repaint() is used. But i haven't used it.
Forgive me if this is not a smart question..
Kt
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
paint() is called by the AWT toolkit to update newly exposed areas, usually in response to moving it out from behind another window or resizing it. It's been a while, but I believe repaint() is used by you to force it to redraw the entire visible area.
 
kanaka tam
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Harkness:
paint() is called by the AWT toolkit to update newly exposed areas, usually in response to moving it out from behind another window or resizing it. It's been a while, but I believe repaint() is used by you to force it to redraw the entire visible area.


Thanx David. Currently i have figured out to get a histogram but the problem is that it is drawn facing downwards. I couldn't figure out how to get it to draw facing upwards.
Any help appreciated. Here is the code.
kt
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get a histogram that grows up from the bottom, the only additional bits of computation you need is to figure out where the top of each bar should be and perhaps determining where the bottom of the whole 'gram should be.

Since you're placing your first drawString at 150,350, maybe y=300 would be a good place for the BOTTOM of each line. Now the trick is to figure out what to put as the second argument to the fillRect(), which determines the TOP of each bar. If height=0, you'd want the second argument to be 300, right? What if height=100 -- then what value would you want for the second fillRect() argument?

I hope this helps.

By the way, the bars still get longer every time you resize the window. Also, even the first time through, the second bar is twice as long as it should be, the third one is three times as long, and the fourth four time, etc. What one thing is causing both these problems?

Ryan
[ April 17, 2005: Message edited by: Ryan McGuire ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should add some System.out.println() calls to your paint() method in order to see what the values of the variables are. If you are using Internet Explorer to test your applet, you can open the Java console to see the output. There should be an icon in the Windows system tray that will let you open the Java console.

HTH

Layne
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!