在鼠标单击时对选择排序行进行排序

时间:2014-04-25 04:02:12

标签: java swing sorting paint repaint

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

enter image description here

我已经把它带到了我可以随意打印的地方,比如第一张图片,或者像第二张图片那样排序。但我无法想象鼠标点击了。我以为我的代码在这里是正确的,但我猜不是。 感谢任何帮助,谢谢。

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

/**
 * Creates new form AnimatedSelectionSortPanel
 */
public AnimatedSelectionSortPanel() {
    initComponents();
    loadArray();
    for(int i = 0; i < NUMBER_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("");

    drawPass(g);
    for (int i = 0; i < NUMBER_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_INDEXES; i++) {
        number[i] = rnd.nextInt((100 - 20) + 1) + 20;
    }
}

public void drawPass(Graphics g) {
    int xBasePosition = 10;
    int yBasePosition = 100;
    for (int i = 0; i < NUMBER_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() {

    jPanel1 = new javax.swing.JPanel();

    jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            jPanel1MouseClicked(evt);
        }
    });

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

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
}// </editor-fold>                        

private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {                                     
   selectionSort();
   repaint();
}                                    


// Variables declaration - do not modify                     
private javax.swing.JPanel jPanel1;
// End of variables declaration                   

}

1 个答案:

答案 0 :(得分:1)

我试图将这个面板添加到一个框架中,但它甚至没有显示出来,除非我注释掉了&#34; initComponents&#34;方法。我猜错了,但这个自动生成的代码是不可读的。我假设jPanel1(添加鼠标监听器)未正确放置。您可以直接将鼠标侦听器添加到动画面板。但是,这是SSCCE:

import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
 *
 * @author matthewtingle
 */
public class AnimatedSelectionSortPanel extends javax.swing.JPanel {
    private static final int NUMBER_INDEXES = 50;
    private static int[] number = new int[NUMBER_INDEXES];

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.getContentPane().setLayout(new GridLayout(1,1));
                f.getContentPane().add(new AnimatedSelectionSortPanel());
                f.setSize(600,300);
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });
    }

    /**
     * Creates new form AnimatedSelectionSortPanel
     */
    public AnimatedSelectionSortPanel() {

        addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                selectionSort();
                repaint();
            }
        });

        loadArray();
        for(int i = 0; i < NUMBER_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) {
        super.paintComponent(g);

        drawPass(g);
        for (int i = 0; i < NUMBER_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_INDEXES; i++) {
            number[i] = rnd.nextInt((100 - 20) + 1) + 20;
        }
    }

    public void drawPass(Graphics g) {
        int xBasePosition = 10;
        int yBasePosition = 100;
        for (int i = 0; i < NUMBER_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;
    }
}
BTW:如果打算将此作为动画排序流程,那么您必须重新考虑一些现有结构。预警警告: NOT 足以在您的&#34; swapElements&#34;中添加一些Thread.sleep。方法左右。您必须废弃排序过程和绘制过程(即,排序必须在自己的线程中进行),并且您必须能够显示排序过程的中间状态