I'm not sure if this is a question better situated for the
applet board, however this section seems to get more action.
I've created an array of objects that draws bars of varying heights.
for (int i = 0; i < sortsBar.length; i++){
sortsBar[i] = new Bar (x, y, width, height);
}
sortsBar[0].addBar(1, 250, 5, 5);
for (int i = 1; i < sortsBar.length - 1; i++){
x+= width + gap;
height +=5;
sortsBar[i].addBar(x, y, width, height);
}
What I would like to be able to do is to randomize the array because later in the applet I will need to use two sort methods on the array. I've created code which I think should work, but doesn't.
public void paint (Graphics g){
for (int index = 0; index < 100; index++){
swap = (int) (rand.nextDouble() * 50);
swap2 = (int) (rand.nextDouble() *50);
if (swap != swap2){
myBar = sortsBar[swap];
sortsBar[swap] = sortsBar[swap2];
sortsBar[swap2] = myBar;
}
}
g.setColor (Color.white);
for (int index = 0; index <sortsBar.length; index++){
g.drawRect(sortsBar[index].x,sortsBar[index].y -sortsBar[index].height,sortsBar[index].width,sortsBar[index].height );
}
}
The output I get is a succession of bars from smallest to largest. For some reason my attempt to jumble the bars is not working even though it compiles and I have no problems running the applet.
Any help would be appreciated.
Regards, Michael