I'm trying to get my code to compare the height of my randomly generated objects and sort them in ascending order. I'm currently having trouble understanding some basic concepts and as much as I read over the java docs I'm not finding an answer that works for me. I know that I don't have anything in my sorting listener, but I'm sort of at a loss as to where to go from here.
Edit: I've fixed portions of the code, but now there's a problem with it only redrawing a few of the bars and occasionally they are out of order.
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class Lab7 extends JPanel
{
Random gen = new Random();
boolean tick = true;
private static bars[] bar = new bars[20];
private static Integer[] height = new Integer[20], heightTemp = new Integer[1];
private final int WIDTH = 500, HEIGHT = 300;
private final int DELAY = 900;
private int size = 15;
int[] y = new int[20], yTemp = new int[1];
int [] x = new int [20];
Timer timer;
public Lab7()
{
if(tick == true)
{
for(int count = 0; count < bar.length; count++)
{
height[count] = gen.nextInt(250) + 5;
y[count] = (HEIGHT - height[count]);
x[count] = (count * size +(count*5));
bar[count] = new bars(x[count], y[count], size, height[count]);
}
tick = false;
}
timer = new Timer(DELAY, new SortingListener());
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.white);
timer.start();
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
for(int count = 0; count < bar.length; count++)
{
bar[count].draw(page);
}
}
private class SortingListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
int min = 0;
for(int count = 0; count < height.length; count++)
{
for(int index = count + 1; index < height.length; index ++)
{
if(height[count].compareTo(height[index]) > 0)
{
heightTemp[0] = height[count];
yTemp[0] = y[count];
height[count] = height[index];
y[count] = y[index];
height[index] = heightTemp[0];
y[index] = yTemp[0];
}
System.out.println(count + ":" + min);
if(index == 19 && height[count].compareTo(height[index]) != 0)
{
bar[count] = new bars(x[count], y[count], size, height[count]);
bar[index] = new bars(x[index], y[index], size, height[index]);
}
}
repaint();
}
timer.stop();
}
}
public static void main(String[]args)
{
JFrame frame = new JFrame("Lab7");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Lab7());
frame.pack();
frame.setVisible(true);
}
}
Here's the Bars class as well.
public class bars{
Random gen = new Random();
private int width = 20, height, x, y;
private int R = gen.nextInt(256);
private int G = gen.nextInt(256);
private int B = gen.nextInt(256);
public Color RandomColor;
//creates a rectangle
public bars(int x, int y, int width, int height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void draw(Graphics page)
{
Color RandomColor = new Color(R, G, B);
page.setColor(RandomColor);
page.fillRect(x, y, width, height);
Polygon p = new Polygon();
page.setColor(RandomColor);
page.fillPolygon(p);
}
public void setWidth(int width)
{this.width = width;}
public int getWidth()
{return width;}
public void setHeight(int height)
{this.height = height;}
public int getHeight()
{return height;}
public void setX(int x)
{this.x = x;}
public int getX()
{return x;}
public void setY(int y)
{this.y = y;}
public int getY()
{return y;}
public void setColor(Color RandomColor)
{this.RandomColor = RandomColor;}
public Color getColor()
{return RandomColor;}}
If you're doing a selection sort, the inner loop needs to start at the value of the outer loop. Change
for(int index = 0;
to
for(int index = count;
The error you're getting is that the list is unsorted right? It looks like only the first element of the "sorted" list will be in the correct spot.