Graphical view of an array
Hi, I am pretty new to Java so I will try my best to describe my problem. Recently I have been assigned to do an exercise where I am to draw all elements in a random generated array with the "drawRect()" method. I would be very glad if someone could also explain how to put some space (5-10 pixels) between the drawn rectangles.
I will include a part of my code where I need the help:
Code:
[B]public class PaintEx extends JPanel{
//declares a new array with 50 spaces
int [] array = new int[50];
public PaintEx(){
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
//loop for filling the array with random elements from 0 to 300, and
//also draw them as rectangles
for (int i = 0; i<array.length; i ++){
Random a = new Random();
array [i] = a.nextInt(300);
g.drawRect(array[i], 300-array[i], 10, array[i]);
}[/B]
This code allows me to draw the graphics somewhat correct. The problem is that they appear in growing order starting from lower values and end with higher values. As I understand, the random utility should take care of a new random value every time the loop draws a rectangle. This does not happen, and the rectangles are mostly overlapping each other. My question: where did I do it wrong at the drawing bit? Or maybe even the array bit?
The reason for "300-array[i]" is because I want the rectangles to appear in a normal coordinate system and not the inverted y which Java uses. All help/pinpoints are gladly appreciated :).