Я работаю над программой выбора сортировки. Он принимает массив случайных чисел, 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
}
Вы не называете drawPass() в paintComponents(). Линии никогда не будут нарисованы.
AnimatedSelectionSortPanel
в свойAnimatedSelectionSortUI
.