我正在研究选择分类测试程序。它需要一个20-100的随机数数组并将其绘制,当您运行该程序时,会显示一个框架,并根据随机数绘制线条,当您在面板上单击时,这些线条将按选择排序。这是单击之前和单击之后的外观。
我以为我已经弄清楚了,但是我什么也没显示出来,我的油漆有毛病吗?到目前为止,这是我拥有的两个类,分别是我的AnimatedSelectSortUI(即我的JFrame)和我的AnimatedSelectionSortPanel。
任何帮助表示赞赏,谢谢。
J框架
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()。永远不会画线。