(以下为游戏刚开始,由100条蓝色矩形组成,每条矩形代表一个人,矩形长度代表拥有的钱数量,刚开始每人为100块)
(以下为动态变化过程)
(为了更好体现数据,即使钱为0了也要给另一个人1块钱,代表负债,红色为负债,蓝色为拥有的钱和债权)
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.lang.InterruptedException;
//模型类M
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");
}
}
}
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.RenderingHints;
import java.util.ArrayList;
import java.util.Map;
import java.util.Random;
import java.util.HashMap;
import javax.swing.*;
//视图类V
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 int[] money;
//设置渲染方法
public void render(int[] money){
this.money = money;
repaint();//重新执行下面的paintComponent方法
}
//设置画布类继承JPanel用于绘图
private class AlgoCanvas extends JPanel{
public AlgoCanvas(){
super(true);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
//将图形强制转化为2D
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 / money.length;
//具体绘制内容
for(int i = 0 ; i < money.length ; i ++)
if(money[i] > 0) {
//当拥有的钱大于0显示为蓝色
AlgoVisHelper.setColor(g2d,AlgoVisHelper.Blue);
AlgoVisHelper.fillRectangle(g2d,
i * w + 1, canvasHeight /2 - money[i], w - 1, money[i]);
}
else{
//当拥有的钱小于0时显示为红色
AlgoVisHelper.setColor(g2d, AlgoVisHelper.Red);
AlgoVisHelper.fillRectangle(g2d,
i * w + 1, canvasHeight / 2, w - 1, -money[i]);
}
}
@Override
public Dimension getPreferredSize(){
return new Dimension(canvasWidth, canvasHeight);
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Arrays;
//控制类C
public class AlgoVisualizer {
private static int DELAY = 1000;//设置渲染暂停时间
private int[] money;//定义money数组
private AlgoFrame frame;//声明绘图类
public AlgoVisualizer(int sceneWidth, int sceneHeight){
//设置游戏人数为100人
money = new int[100];
//给每一个人一百块作为初始值
for(int i = 0 ; i < money.length ; i ++)
money[i] = 100;
EventQueue.invokeLater(() -> {
frame = new AlgoFrame("Money Problem", sceneWidth, sceneHeight);
new Thread(() -> {
run();
}).start();
});
}
//编写动画逻辑
public void run(){
while(true){
Arrays.sort(money);
//对money数组进行渲染
frame.render(money);
//设置渲染时等待时间
AlgoVisHelper.pause(DELAY);
//每一个人将自己的1块钱随机给另一个人
for(int k = 0 ; k < 50 ; k ++){
for(int i = 0 ; i < money.length; i ++){
// if(money[i] > 0){
int j = (int)(Math.random() * money.length);
money[i] -= 1;
money[j] += 1;
// }
}
}
}
}
public static void main(String[] args) {
//设置窗口宽高
int sceneWidth = 1000;
int sceneHeight = 650;
//实例化控制类对象
AlgoVisualizer vis = new AlgoVisualizer(sceneWidth, sceneHeight);
}
}
以上为全部代码