无法在JPanel中绘制线条

时间:2014-04-24 17:29:04

标签: java sorting user-interface paint repaint

我正在进行选择排序测试计划。它需要一组随机数,20-100并绘制它们,当你运行程序时,框架会显示根据随机数绘制的线条,当你点击面板时,线条按选择排序排序。这是点击之前和点击之后的样子。

enter image description here

我以为我已经弄清楚了,但是我根本没有显示任何线条,我的油漆是否有问题?这是我到目前为止所拥有的,我有两个单独的类,我的AnimatedSelectSortUI,我的JFrame,以及我的AnimatedSelectionSortPanel。

感谢任何帮助,谢谢。

的JFrame

public class AnimatedSelectionSortUI extends javax.swing.JFrame {

/**
 * Creates new form AnimatedSelectionSortUI
 */
public AnimatedSelectionSortUI() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 500, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 150, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(AnimatedSelectionSortUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(AnimatedSelectionSortUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(AnimatedSelectionSortUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(AnimatedSelectionSortUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new AnimatedSelectionSortUI().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
// End of variables declaration                   
}

的JPanel

import java.awt.Graphics;
import java.util.Random;
/**
 *
 * @author matthewtingle
 */
public class AnimatedSelectionSortPanel extends javax.swing.JPanel {
private static final int NUMBER_OF_INDEXES = 50;
private static int[] number = new int[NUMBER_OF_INDEXES];

/**
 * Creates new form AnimatedSelectionSortPanel
 */
public AnimatedSelectionSortPanel() {
    initComponents();
    loadArray();
    for(int i = 0; i < NUMBER_OF_INDEXES; i++){
        if(i%10==0){
            System.out.println("");
            System.out.print("" + number[i]+", ");
        }else{
            System.out.print("" + number[i]+", ");
        }
    }
}
@Override
public void paintComponent(Graphics g) {
    System.out.println("");
    selectionSort();
    for (int i = 0; i < NUMBER_OF_INDEXES; i++){
        if(i%10==0){
            System.out.println("");
            System.out.print(""+ number[i]+ ", ");
        }else{
            System.out.print(""+ number[i]+ ", ");
        }
    }
}

private void loadArray() {
    Random rnd = new Random();
    for (int i = 0; i < NUMBER_OF_INDEXES; i++) {
        number[i] = rnd.nextInt((100 - 20) + 1) + 20;
    }
}

private void drawPass(Graphics g) {
    int xBasePosition = 10;
    int yBasePosition = 100;
    for (int i = 0; i < NUMBER_OF_INDEXES; i++) {
        g.drawLine(xBasePosition,yBasePosition+20, xBasePosition, yBasePosition - number[i]);
        xBasePosition+=10;
    }
}
public void selectionSort(){
    for(int top = 0; top <= number.length - 2; top++){
        int minIndex = top;
        for (int i = top + 1; i <= number.length - 1; i++) {
            if (number[i] < number[minIndex]) {
                minIndex = i;
            }
        }swapElements(top,minIndex);
    }
}
private void swapElements(int index1, int index2){
    int tmp = number[index1];
    number[index1] = number[index2];
    number[index2] = tmp;
}


/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 500, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 150, Short.MAX_VALUE)
    );
}// </editor-fold>                        


// Variables declaration - do not modify                     
// End of variables declaration                   
}

1 个答案:

答案 0 :(得分:0)

你不能在paintComponents()中调用drawPass()。永远不会画出这些线条。