Hi,
Im having a lil problem which is kinda fustrating me.
This line of the code: g.fillRect( xPos, yPos - 20 - sensor, 20, sensor ); the last sensor besides how high the rect will be.
And in this case that would be 1, 2, 3, 4, 5, 6 and so on till 10. But i wanna randomize it. i tried to fill in a random number but then it doesn't display it.
Coz somehow its bounds out of its reach and i get a blank screen with just the numbers 1 ... 10.
So the question is, how can i change the last sensor into a randomized number given by math(random) and fill the rect by going up, instead of down?
Thnx in advance!
public class HistoGramFrame extends Frame
{
private int[] sensor = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
private Histogram histogram = new Histogram( sensor, 40 );
/**
* The constructor.
*/
public HistoGramFrame()
{
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
HistoGramFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("HistoGram");
setMenuBar(menuBar);
setSize(new Dimension(800, 800));
// Add window listener.
this.addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
HistoGramFrame.this.windowClosed();
}
}
);
}
public void paint( Graphics g )
{
histogram.print( g, 200, 500 );
}
class Histogram
{
private int[] sensor;
private int space;
public Histogram( int[] sensor, int space )
{
this.sensor = sensor;
this.space = space;
}
public void print( Graphics g, int x, int y )
{
int xPos = x;
int yPos = y;
for( int i = 0; i < sensor.length; i++ )
{
//g.setColor(Color.GREEN);
g.fillRect( xPos, yPos - 20 - sensor[i], 20, sensor[i] );
xPos += space;
}
xPos = x;
for( int i = 0; i < sensor.length; i++ )
{
String numberStr = " " + sensor[i];
g.drawString( numberStr, xPos, yPos );
xPos += space;
}
}
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed()
{
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
}