Java(8)GUI编程之排序算法可视化

此算法思想很简单,简而言之,选择排序:每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到全部待排序的数据元素排完。

核心代码如下:

public class SelectionSortData {

private int[] numbers;

public int orderedIndex = -1; // [0...orderedIndex) 是有序的
public int currentCompareIndex = -1; // 当前正在比较的元素索引
public int currentMinIndex = -1; // 当前找到的最小元素的索引

public SelectionSortData(int N, int randomBound){

numbers = new int[N];

for( int i = 0 ; i < N ; i ++)
numbers[i] = (int)(Math.random()*randomBound) + 1;
}

public int N(){
return numbers.length;
}

public int get(int index){
if( index < 0 || index >= numbers.length)
throw new IllegalArgumentException("Invalid index to access Sort Data.");

return numbers[index];
}

public void swap(int i, int j) {

if( i < 0 || i >= numbers.length || j < 0 || j >= numbers.length)
throw new IllegalArgumentException("Invalid index to access Sort Data.");

int t = numbers[i];
numbers[i] = numbers[j];
numbers[j] = t;
}
}
这是整个代码的核心,其他的均为图像处理,可视化
完整如下:
import java.awt.*;

public class AlgoVisualizer {

private static int DELAY = 20;

private SelectionSortData data;
private AlgoFrame frame;

public AlgoVisualizer(int sceneWidth, int sceneHeight, int N){

data = new SelectionSortData(N, sceneHeight);

// 初始化视图
EventQueue.invokeLater(() -> {
frame = new AlgoFrame("Selection Sort Visualization", sceneWidth, sceneHeight);
new Thread(() -> {
run();
}).start();
});
}

private void run(){

setData(0, -1, -1);

for( int i = 0 ; i < data.N() ; i ++ ){
// 寻找[i, n)区间里的最小值的索引
int minIndex = i;
setData(i, -1, minIndex);

for( int j = i + 1 ; j < data.N() ; j ++ ){
setData(i, j, minIndex);

if( data.get(j) < data.get(minIndex) ){
minIndex = j;
setData(i, j, minIndex);
}
}

data.swap(i , minIndex);
setData(i+1, -1, -1);
}

setData(data.N(),-1,-1);
}

private void setData(int orderedIndex, int currentCompareIndex, int currentMinIndex){
data.orderedIndex = orderedIndex;
data.currentCompareIndex = currentCompareIndex;
data.currentMinIndex = currentMinIndex;

frame.render(data);
AlgoVisHelper.pause(DELAY);
}

public static void main(String[] args) {

int sceneWidth = 800;
int sceneHeight = 800;
int N = 100;

AlgoVisualizer vis = new AlgoVisualizer(sceneWidth, sceneHeight, N);
}
}

import java.awt.*;
import javax.swing.*;

public class AlgoFrame extends JFrame{

private int canvasWidth;
private int canvasHeight;

public AlgoFrame(String title, int canvasWidth, int canvasHeight){

super(title);

this.canvasWidth = canvasWidth;
this.canvasHeight = canvasHeight;

AlgoCanvas canvas = new AlgoCanvas();
setContentPane(canvas);
pack();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);

setVisible(true);
}

public AlgoFrame(String title){

this(title, 1024, 768);
}

public int getCanvasWidth(){return canvasWidth;}
public int getCanvasHeight(){return canvasHeight;}

// data
private SelectionSortData data;
public void render(SelectionSortData data){
this.data = data;
repaint();
}

private class AlgoCanvas extends JPanel{

public AlgoCanvas(){
// 双缓存
super(true);
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2d = (Graphics2D)g;

// 抗锯齿
RenderingHints hints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.addRenderingHints(hints);

// 具体绘制
int w = canvasWidth/data.N();
for(int i = 0 ; i < data.N() ; i ++ ) {
if (i < data.orderedIndex)
AlgoVisHelper.setColor(g2d, AlgoVisHelper.Red);
else
AlgoVisHelper.setColor(g2d, AlgoVisHelper.Grey);

if(i == data.currentCompareIndex)
AlgoVisHelper.setColor(g2d, AlgoVisHelper.LightBlue);
if(i == data.currentMinIndex)
AlgoVisHelper.setColor(g2d, AlgoVisHelper.Indigo);
AlgoVisHelper.fillRectangle(g2d, i * w, canvasHeight - data.get(i), w - 1, data.get(i));
}
}

@Override
public Dimension getPreferredSize(){
return new Dimension(canvasWidth, canvasHeight);
}
}
}
import java.awt.*;
import java.awt.geom.Ellipse2D;

import java.awt.geom.Rectangle2D;
import java.lang.InterruptedException;

public class AlgoVisHelper {

private AlgoVisHelper(){}

public static final Color Red = new Color(0xF44336);
public static final Color Pink = new Color(0xE91E63);
public static final Color Purple = new Color(0x9C27B0);
public static final Color DeepPurple = new Color(0x673AB7);
public static final Color Indigo = new Color(0x3F51B5);
public static final Color Blue = new Color(0x2196F3);
public static final Color LightBlue = new Color(0x03A9F4);
public static final Color Cyan = new Color(0x00BCD4);
public static final Color Teal = new Color(0x009688);
public static final Color Green = new Color(0x4CAF50);
public static final Color LightGreen = new Color(0x8BC34A);
public static final Color Lime = new Color(0xCDDC39);
public static final Color Yellow = new Color(0xFFEB3B);
public static final Color Amber = new Color(0xFFC107);
public static final Color Orange = new Color(0xFF9800);
public static final Color DeepOrange = new Color(0xFF5722);
public static final Color Brown = new Color(0x795548);
public static final Color Grey = new Color(0x9E9E9E);
public static final Color BlueGrey = new Color(0x607D8B);
public static final Color Black = new Color(0x000000);
public static final Color White = new Color(0xFFFFFF);

public static void strokeCircle(Graphics2D g, int x, int y, int r){

Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
g.draw(circle);
}

public static void fillCircle(Graphics2D g, int x, int y, int r){

Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
g.fill(circle);
}

public static void strokeRectangle(Graphics2D g, int x, int y, int w, int h){

Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
g.draw(rectangle);
}

public static void fillRectangle(Graphics2D g, int x, int y, int w, int h){

Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
g.fill(rectangle);
}

public static void setColor(Graphics2D g, Color color){
g.setColor(color);
}

public static void setStrokeWidth(Graphics2D g, int w){
int strokeWidth = w;
g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
}

public static void pause(int t) {
try { Thread.sleep(t); } catch (InterruptedException e) { System.out.println("Error sleeping"); } }}
 
 
public class SelectionSortData {

private int[] numbers;

public int orderedIndex = -1; // [0...orderedIndex) 是有序的
public int currentCompareIndex = -1; // 当前正在比较的元素索引
public int currentMinIndex = -1; // 当前找到的最小元素的索引

public SelectionSortData(int N, int randomBound){

numbers = new int[N];

for( int i = 0 ; i < N ; i ++)
numbers[i] = (int)(Math.random()*randomBound) + 1;
}

public int N(){
return numbers.length;
}

public int get(int index){
if( index < 0 || index >= numbers.length)
throw new IllegalArgumentException("Invalid index to access Sort Data.");

return numbers[index];
}

public void swap(int i, int j) {

if( i < 0 || i >= numbers.length || j < 0 || j >= numbers.length)
throw new IllegalArgumentException("Invalid index to access Sort Data.");

int t = numbers[i];
numbers[i] = numbers[j];
numbers[j] = t;
}
}

 




还有,别再说用python更简单了,这话很LOW,谁教你的?速度慢不说,光是图形处理都未必能比java代码量少到哪去,注意看核心代码,算法!
 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值