我试图让我的代码比较随机生成的对象的高度并按升序对它们进行排序。我目前在理解一些基本概念时遇到了困难,并且在阅读 java 文档时,我没有找到适合我的答案。我知道我的排序侦听器中没有任何东西,但我有点不知从何下手。
编辑:我已经修复了部分代码,但现在有一个问题,它只重绘了一些条,有时它们是乱序的。
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);
}
}
这里也是 Bars 类。
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;}}