2013-05-21 36 views
0

我想讓我的代碼比較我的隨機生成的對象的高度,並按升序對它們進行排序。我目前在理解一些基本概念時遇到了困難,就像我讀過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;}} 
+0

發生了什麼事不對?排序不正確?分享你的觀察。 – JustinDanielson

回答

0

如果您正在進行選擇排序,則內部循環需要從外部循環的值開始。更改

for(int index = 0; 

for(int index = count; 

你得到的錯誤是,名單未排序嗎?它看起來只有「排序」列表的第一個元素將在正確的位置。

+0

這絕對是一個問題,謝謝你,但我仍然得到這些錯誤。 //異常在線程 「AWT-EventQueue的-0」 顯示java.lang.NullPointerException // \t在Lab7 $ SortingListener.actionPerformed(Lab7.java:65)// \t在javax.swing.Timer.fireActionPerformed(來源不明)// 以及一些其他未知來源和本地方法錯誤。 – BranJamin

+0

看看你正在排序的列表。你只是做list = new ... [40]但你永遠不會初始化40個元素中的每一個。他們都是空的。我不知道你打算如何初始化它們,但是你需要在選擇排序前進行。第一次使用該列表時,你會崩潰。可能在這一行上,列出[scan] .compareTo(list [min])。 (Lab7.java:65 < - 你在第65行崩潰了。那是什麼行? – JustinDanielson

+0

唉,再次感謝你。我仔細檢查了一下代碼。除此之外不重繪所有的條只有其中的一部分是爲了它正在做我想做的事 – BranJamin