├── MoneyProblem ├── MoneyProblem.iml ├── out │ └── production │ │ └── MoneyProblem │ │ ├── AlgoFrame$AlgoCanvas.class │ │ ├── AlgoFrame.class │ │ ├── AlgoVisHelper.class │ │ └── AlgoVisualizer.class └── src │ ├── AlgoFrame.java │ ├── AlgoVisHelper.java │ └── AlgoVisualizer.java └── README.md /MoneyProblem/MoneyProblem.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /MoneyProblem/out/production/MoneyProblem/AlgoFrame$AlgoCanvas.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterBooo/AmazingAlgo/b24f5ca5b10f945bb86148fb538bc588f174331a/MoneyProblem/out/production/MoneyProblem/AlgoFrame$AlgoCanvas.class -------------------------------------------------------------------------------- /MoneyProblem/out/production/MoneyProblem/AlgoFrame.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterBooo/AmazingAlgo/b24f5ca5b10f945bb86148fb538bc588f174331a/MoneyProblem/out/production/MoneyProblem/AlgoFrame.class -------------------------------------------------------------------------------- /MoneyProblem/out/production/MoneyProblem/AlgoVisHelper.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterBooo/AmazingAlgo/b24f5ca5b10f945bb86148fb538bc588f174331a/MoneyProblem/out/production/MoneyProblem/AlgoVisHelper.class -------------------------------------------------------------------------------- /MoneyProblem/out/production/MoneyProblem/AlgoVisualizer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MisterBooo/AmazingAlgo/b24f5ca5b10f945bb86148fb538bc588f174331a/MoneyProblem/out/production/MoneyProblem/AlgoVisualizer.class -------------------------------------------------------------------------------- /MoneyProblem/src/AlgoFrame.java: -------------------------------------------------------------------------------- 1 | import java.awt.*; 2 | 3 | import javax.swing.*; 4 | 5 | public class AlgoFrame extends JFrame{ 6 | 7 | private int canvasWidth; 8 | private int canvasHeight; 9 | 10 | public AlgoFrame(String title, int canvasWidth, int canvasHeight){ 11 | 12 | super(title); 13 | 14 | this.canvasWidth = canvasWidth; 15 | this.canvasHeight = canvasHeight; 16 | 17 | AlgoCanvas canvas = new AlgoCanvas(); 18 | setContentPane(canvas); 19 | 20 | setResizable(false); 21 | pack(); // 在setResizable(false)后进行pack(),防止在Windows下系统修改frame的尺寸 22 | // 具体参见:http://coding.imooc.com/learn/questiondetail/26420.html 23 | 24 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 25 | setVisible(true); 26 | } 27 | 28 | public AlgoFrame(String title){ 29 | 30 | this(title, 1024, 768); 31 | } 32 | 33 | public int getCanvasWidth(){return canvasWidth;} 34 | public int getCanvasHeight(){return canvasHeight;} 35 | 36 | private int[] money; 37 | private int count; 38 | public void render(int[] money,int count){ 39 | this.money = money; 40 | this.count = count; 41 | repaint(); 42 | } 43 | 44 | private class AlgoCanvas extends JPanel{ 45 | 46 | public AlgoCanvas(){ 47 | // 双缓存 48 | super(true); 49 | } 50 | 51 | @Override 52 | public void paintComponent(Graphics g) { 53 | super.paintComponent(g); 54 | 55 | Graphics2D g2d = (Graphics2D)g; 56 | 57 | // 抗锯齿 58 | RenderingHints hints = new RenderingHints( 59 | RenderingHints.KEY_ANTIALIASING, 60 | RenderingHints.VALUE_ANTIALIAS_ON); 61 | hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 62 | g2d.addRenderingHints(hints); 63 | // 64 | g2d.setFont(new Font("宋体", Font.BOLD,24)); 65 | g2d.drawString("经过了 "+String.valueOf(count) + " 次交换",500,80); 66 | 67 | // 具体绘制 68 | AlgoVisHelper.setColor(g2d,AlgoVisHelper.Blue); 69 | int w = canvasWidth / money.length; 70 | 71 | 72 | for (int i = 0 ; i < money.length; i++) 73 | AlgoVisHelper.fillRectangle(g2d,i*w,canvasHeight - money[i],w-1,money[i]); 74 | 75 | } 76 | 77 | @Override 78 | public Dimension getPreferredSize(){ 79 | return new Dimension(canvasWidth, canvasHeight); 80 | } 81 | } 82 | } 83 | 84 | 85 | -------------------------------------------------------------------------------- /MoneyProblem/src/AlgoVisHelper.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.*; 3 | import java.awt.geom.*; 4 | 5 | import java.lang.InterruptedException; 6 | 7 | public class AlgoVisHelper { 8 | 9 | private AlgoVisHelper(){} 10 | 11 | public static final Color Red = new Color(0xF44336); 12 | public static final Color Pink = new Color(0xE91E63); 13 | public static final Color Purple = new Color(0x9C27B0); 14 | public static final Color DeepPurple = new Color(0x673AB7); 15 | public static final Color Indigo = new Color(0x3F51B5); 16 | public static final Color Blue = new Color(0x2196F3); 17 | public static final Color LightBlue = new Color(0x03A9F4); 18 | public static final Color Cyan = new Color(0x00BCD4); 19 | public static final Color Teal = new Color(0x009688); 20 | public static final Color Green = new Color(0x4CAF50); 21 | public static final Color LightGreen = new Color(0x8BC34A); 22 | public static final Color Lime = new Color(0xCDDC39); 23 | public static final Color Yellow = new Color(0xFFEB3B); 24 | public static final Color Amber = new Color(0xFFC107); 25 | public static final Color Orange = new Color(0xFF9800); 26 | public static final Color DeepOrange = new Color(0xFF5722); 27 | public static final Color Brown = new Color(0x795548); 28 | public static final Color Grey = new Color(0x9E9E9E); 29 | public static final Color BlueGrey = new Color(0x607D8B); 30 | public static final Color Black = new Color(0x000000); 31 | public static final Color White = new Color(0xFFFFFF); 32 | 33 | public static void strokeCircle(Graphics2D g, int x, int y, int r){ 34 | 35 | Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r); 36 | g.draw(circle); 37 | } 38 | 39 | public static void fillCircle(Graphics2D g, int x, int y, int r){ 40 | 41 | Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r); 42 | g.fill(circle); 43 | } 44 | 45 | public static void strokeRectangle(Graphics2D g, int x, int y, int w, int h){ 46 | 47 | Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h); 48 | g.draw(rectangle); 49 | } 50 | 51 | public static void fillRectangle(Graphics2D g, int x, int y, int w, int h){ 52 | 53 | Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h); 54 | g.fill(rectangle); 55 | } 56 | 57 | public static void setColor(Graphics2D g, Color color){ 58 | g.setColor(color); 59 | } 60 | 61 | public static void setStrokeWidth(Graphics2D g, int w){ 62 | int strokeWidth = w; 63 | g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); 64 | } 65 | 66 | public static void pause(int t) { 67 | try { 68 | Thread.sleep(t); 69 | } 70 | catch (InterruptedException e) { 71 | System.out.println("Error sleeping"); 72 | } 73 | } 74 | 75 | public static void putImage(Graphics2D g, int x, int y, String imageURL){ 76 | 77 | ImageIcon icon = new ImageIcon(imageURL); 78 | Image image = icon.getImage(); 79 | 80 | g.drawImage(image, x, y, null); 81 | } 82 | 83 | public static void drawText(Graphics2D g, String text, int centerx, int centery){ 84 | 85 | if(text == null) 86 | throw new IllegalArgumentException("Text is null in drawText function!"); 87 | 88 | FontMetrics metrics = g.getFontMetrics(); 89 | int w = metrics.stringWidth(text); 90 | int h = metrics.getDescent(); 91 | g.drawString(text, centerx - w/2, centery + h); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /MoneyProblem/src/AlgoVisualizer.java: -------------------------------------------------------------------------------- 1 | import java.awt.*; 2 | import java.awt.event.KeyAdapter; 3 | import java.awt.event.KeyEvent; 4 | import java.awt.event.MouseAdapter; 5 | import java.awt.event.MouseEvent; 6 | import java.lang.reflect.Array; 7 | import java.util.Arrays; 8 | 9 | public class AlgoVisualizer { 10 | //控制器 11 | // TODO: 创建自己的数据 12 | private int[] money; // 数据 13 | private AlgoFrame frame; // 视图 14 | 15 | public AlgoVisualizer(int sceneWidth, int sceneHeight){ 16 | 17 | // 初始化数据 18 | money = new int[100]; 19 | for (int i = 0 ; i < money.length; i++) 20 | money[i] = 100; 21 | 22 | 23 | // 初始化视图 24 | EventQueue.invokeLater(() -> { 25 | frame = new AlgoFrame("Money Problem", sceneWidth, sceneHeight); 26 | new Thread(() -> { 27 | run(); 28 | }).start(); 29 | }); 30 | } 31 | 32 | // 动画逻辑 33 | private void run(){ 34 | 35 | int count = 0; 36 | while (true){ 37 | 38 | // Arrays.sort(money); 39 | frame.render(money,count); 40 | count++; 41 | if(count >= 10000){ 42 | AlgoVisHelper.pause(400000); 43 | } 44 | AlgoVisHelper.pause(40); 45 | for (int k = 0; k < 1; k++){ 46 | for (int i = 0; i < money.length;i++) 47 | if (money[i] > 0){ 48 | int j = (int)(Math.random() * money.length); 49 | money[i] -= 1; 50 | money[j] += 1; 51 | } 52 | } 53 | 54 | } 55 | 56 | } 57 | 58 | 59 | public static void main(String[] args) { 60 | 61 | int sceneWidth = 1000; 62 | int sceneHeight = 800; 63 | 64 | // TODO: 根据需要设置其他参数,初始化visualizer 65 | AlgoVisualizer visualizer = new AlgoVisualizer(sceneWidth, sceneHeight); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AmazingAlgo 2 | 算法可视化 3 | --------------------------------------------------------------------------------