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);
    }
}

Create a Random object right before the loop. Inside of the loop, you have your normal thing and then the last 'sensor' parameter would look something of the sort:

sensor[random_name.nextInt(10)+1];

That would give you a random number from 1 to 10.

Create a Random object right before the loop. Inside of the loop, you have your normal thing and then the last 'sensor' parameter would look something of the sort:

sensor[random_name.nextInt(10)+1];

That would give you a random number from 1 to 10.

Thank you for your reply!

Thats actually not really the problem. instead of sensor in that stence at the end, i can put everyting i want. For example 100.

The problem is, how this is being plotted out on the screen.

See screenshot number 1 where the rects are fine:

[IMG]http://img139.imageshack.us/img139/2262/javafillrects2mi.th.jpg[/IMG]


See screenshot number 2 where they are not fine:

[IMG]http://img305.imageshack.us/img305/6248/javafillrects29wk.th.jpg[/IMG]


See de difference between these 2 screenshot. Look at the space between the numbers and the rects.

In the second screenshot i replaced the last sensor with '30'.

And the rects are going downwards which should be upwards...

So the question is, how can i make them go up instead of down?


Thnx in advance!

Member Avatar for iamthwee

What are those pictures supposed to show?

Can you post all the rest of your code please?

Hi iamthwee,

And sure iamthwee.

There ya go:


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);
    }
}

Draw them from the same base position and you should be fine. It looks like you are drawing from top to bottom instead of bottom to top.

Draw them from the same base position and you should be fine. It looks like you are drawing from top to bottom instead of bottom to top.

Hi server_crash,

Thank you for your reply!

In what line does this happen?

Member Avatar for iamthwee

Hi iamthwee,

And sure iamthwee.

There ya go:

Is that meant to be a joke. :sad:

Can you post the public static void main()

Is that meant to be a joke. :sad:

Can you post the public static void main()

Nope ;)

I quoted it because its translated from another language which i dont have anymore...

but this is the main method:

public class HistoGram {
    
    public static void main(String[] args) {
        // Create application frame.
        HistoGramFrame frame = new HistoGramFrame();
        
        // Show frame
        frame.setVisible(true);
    }
}
Member Avatar for iamthwee

And the rects are going downwards which should be upwards...

So the question is, how can i make them go up instead of down?

I've got it!

Turn your monitor upside down. Or stand on your head. Whichever takes your fancy?

I've got it!

Turn your monitor upside down. Or stand on your head. Whichever takes your fancy?

What about the numbers?

They would be upside down then...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.