Question

I'm working on a selection sort testing program. It takes an array of random numbers, 20-100 and paints them, when you run the program, a frame is displayed with lines painted according to the random numbers, when you click on the panel the lines are ordered by the selection sort. Here is what it should look like before click and after click.

enter image description here

I thought I had it figured out, but I'm not getting any lines to show at all, is there a fault in my paint? Here's what I have so far, I have two seperate classes, my AnimatedSelectSortUI, which is my JFrame, and my AnimatedSelectionSortPanel.

Any help is appreciated, thank you.

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                   
}
Was it helpful?

Solution

You don't call drawPass() in your paintComponents(). The lines will never be drawn.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top