簡體   English   中英

無法在JPanel中畫線

[英]Unable to draw lines in JPanel

我正在研究選擇分類測試程序。 它需要一個20-100的隨機數數組並將其繪制,當您運行該程序時,將顯示一個框架,其中根據隨機數繪制了線條,當您在面板上單擊時,這些線條將按選擇排序。 單擊前和單擊后的外觀如下。

在此處輸入圖片說明

我以為我已經弄明白了,但是我什么也沒顯示出來,我的油漆有毛病嗎? 到目前為止,這是我擁有的兩個類,分別是我的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                   
}

您不會在paintComponents()中調用drawPass()。 永遠不會畫線。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM