【开发者笔记】冒泡排序过程呈现之java内置GUI表示

自己玩玩写写,排序的过程多么有趣,特别是把看着电脑吧一堆乱七八糟的数据排成有序组合的时候,看起来贼舒服,特别是强迫症患者。好了,话不多说上代码,也算是自己记录一下吧,没有什么技术含量但个人感觉比较有趣。

排序以及显示代码:

 1 package com.wyb.dyi.test;
 2 
 3 import java.awt.Toolkit;
 4 import java.util.Random;
 5 
 6 import com.wyb.dyi.test.*;
 7 
 8 public class showBubbleSort {
 9     public static int W = (int) Toolkit.getDefaultToolkit().getScreenSize()
10             .getWidth();/* 当前屏幕宽度 */
11     public static int H = (int) Toolkit.getDefaultToolkit().getScreenSize()
12             .getHeight();/* 当前屏幕高度 */
13 
14     static int[] array = getArray(W);
15     /*显示用的面板*/
16     static ShowArrayInLine show = new ShowArrayInLine(array);
17 
18     /* main */
19     public static void main(String[] args) {
20         try {
21             Thread.sleep(2000);
22         } catch (Exception e) {
23         }
24         System.out.println("start!");
25         bubble(array);
26         
27     }
28 
29     /* 冒泡排序 */
30     static void bubble(int[] array) {
31         for (int i = 0; i < array.length; i++) {
32             for (int j = 0; j < array.length - 1 - i; j++) {
33                 if (array[j] > array[j + 1]) {
34                     int temp = array[j];
35                     array[j] = array[j + 1];
36                     array[j + 1] = temp;
37                 }
38                 /*可以在这里刷新显示,但是刷新速度过慢,能观察到排序细节*/
39                 show.updateShowArray(array);
40             }
41             /*在这里刷新显示比较快*/
42             //show.updateShowArray(array);
43         }
44     }
45 
46 
47     /* 构造一个长度为length的高度为length/2的数组 */
48     public static int[] getArray(int length) {
49         /* 生成空数组 */
50         int[] re = new int[length];
51         /* 给数组附上高为length/2的升序数值 */
52         for (int i = 0; i < re.length; i++)
53             re[i] = i / 2;
54         /* 讲有序数组打乱 */
55         for (int i = 0; i < 20 * re.length; i++) {
56             int index1 = new Random().nextInt(length);
57             int index2 = new Random().nextInt(length);
58             int temp = re[index1];
59             re[index1] = re[index2];
60             re[index2] = temp;
61         }
62 
63         return re;
64     }
65 }
排序块

 

 1 package com.wyb.dyi.test;
 2 
 3 import java.awt.Color;
 4 import java.awt.Graphics;
 5 import java.awt.Toolkit;
 6 import java.util.Random;
 7 
 8 import javax.swing.JFrame;
 9 import javax.swing.JPanel;
10 
11 /* 显示主框架JFrame类 */
12 class ShowArrayInLine extends JFrame {
13     private static final long serialVersionUID = 1L;
14     ShowJPanel show;
15     int[] oldArray;
16     /* 构造函数,初始化显示框架 */
17     public ShowArrayInLine(int[] a) {
18         oldArray=a;
19         show = new ShowJPanel(a,a);
20         show.setBounds(0, 0, ShowTool.W, ShowTool.H);
21         this.setSize(ShowTool.W, ShowTool.H);
22         this.setTitle("归并排序");
23         this.setLocation(0, 0);
24         this.add(show);
25         this.setVisible(true);
26         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
27     }
28 
29     /**
30      * 更新画布
31      * 
32      * @param a
33      */
34     public void updateShowArray(int[] a) {
35         
36         this.remove(show);
37         show = new ShowJPanel(a,oldArray);
38         this.add(show);
39         this.setVisible(true);
40         oldArray=a;
41     }
42 }
43 
44 /* 画图面板 */
45 class ShowJPanel extends JPanel {
46     private static final long serialVersionUID = 1L;
47      int[] array;/* 用于接收构造方法传过来的数组,用于绘图 */
48      public int[] oldArray;
49 
50     public ShowJPanel(int[] a,int[] b) {
51         array = a;    
52         oldArray=b;
53     }
54     /* 绘图函数 */
55     public void paintComponent(final Graphics g) {
56         Random r=new Random();
57         
58         
59         /*画笔置白色*/ 
60         g.setColor(Color.WHITE);
61         /* 擦除上一次绘制的团*/ 
62         g.fillRect(0,0,Tool.W,Tool.H);
63         
64         /* 开始绘制当前数组图像,以(0,ShowTool.H)为原点,向右为x轴表数组下标,向上为y轴表当前下标所对应数组存置大小*/ 
65         for (int i = 0; i < array.length; i++) {
66             /* 画笔置黑色*/ 
67             if(oldArray[i]==array[i])
68                 g.setColor(Color.BLACK);
69             else
70                 g.setColor(Color.RED);
71             g.drawLine(i, ShowTool.H - 80, i, ShowTool.H - array[i] - 80);
72         }
73         
74         
75 
76     }
77 }
78 
79 /* 工具类,获取当前屏幕大小,用户初始化显示组件和new乱序数组 */
80 class ShowTool {
81     public static int W = (int) Toolkit.getDefaultToolkit().getScreenSize()
82             .getWidth();/* 当前屏幕宽度 */
83     public static int H = (int) Toolkit.getDefaultToolkit().getScreenSize()
84             .getHeight();/* 当前屏幕高度 */
85 }
显示面板块

下面给出效果图以及连接: 冒泡排序过程呈现之java内置GUI表示视频

 

posted @ 2016-10-26 13:36  stagebo  阅读(430)  评论(0编辑  收藏  举报