我想讓我的代碼比較我的隨機生成的對象的高度,並按升序對它們進行排序。我目前在理解一些基本概念時遇到了困難,就像我讀過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;}}
發生了什麼事不對?排序不正確?分享你的觀察。 – JustinDanielson