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

*only*the heights for each. Leave everything else in place.

Also, you may find this link interesting.

"I'm not back." - Bill Harding, *Twister*

I will check out the link you suggested to see if it will bring about some clarity.

Regards, Michael

Originally posted by Jim Yingst:

When you swap two elements, you're swapping their positions in the array. However, each object still retains its old values of x, y, width, and height, which is what determines where they are drawn onscreen. I recommend that when you swap two elements, swaponlythe heights for each. Leave everything else in place.

Also, you may find this link interesting.

Sheriff

Draw a point at (1, 5)

Draw a point at (2, 10)

Draw a point at (3, 15)

Draw a point at (4, 20)

I can read these in order, and draw a set of points forming a nice straight line. Now imagine that the statements were shuffled:

Draw a point at (2, 10)

Draw a point at (1, 5)

Draw a point at (4, 20)

Draw a point at (3, 15)

If I read each line in order and draw points accordingly, I will end up with the exact same graph. I may have drawn the points in a different order, but the final result is the same - because each statement had enough data to allow me to reconstruct the original picture, even with the statements scrambled. If I wanted to scramble the points up so they weren't in a line, I'd have to break apart the current x, y value pairs - e.g.

Draw a point at (1, 10)

Draw a point at (2, 5)

Draw a point at (3, 20)

Draw a point at (4, 15)

This is analogous to your situation. The final appearance of your graph has nothing to do with the order of elements in your array - it depends on the correlations between x values and height values in those elements. Hence, scramble the heights, not the whole objects.

"I'm not back." - Bill Harding, *Twister*

Thank you very much, Michael

Originally posted by Jim Yingst:

Consider the folllowing set of statements:

Draw a point at (1, 5)

Draw a point at (2, 10)

Draw a point at (3, 15)

Draw a point at (4, 20)

I can read these in order, and draw a set of points forming a nice straight line. Now imagine that the statements were shuffled:

Draw a point at (2, 10)

Draw a point at (1, 5)

Draw a point at (4, 20)

Draw a point at (3, 15)

If I read each line in order and draw points accordingly, I will end up with the exact same graph. I may have drawn the points in a different order, but the final result is the same - because each statement had enough data to allow me to reconstruct the original picture, even with the statements scrambled. If I wanted to scramble the points up so they weren't in a line, I'd have to break apart the current x, y value pairs - e.g.

Draw a point at (1, 10)

Draw a point at (2, 5)

Draw a point at (3, 20)

Draw a point at (4, 15)

This is analogous to your situation. The final appearance of your graph has nothing to do with the order of elements in your array - it depends on the correlations between x values and height values in those elements. Hence, scramble the heights, not the whole objects.