package graphic;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class AlgoVisualizer {
private Circle[] circles; //data
private AlgoJFrame frame; //view
private boolean isAnimated = true; //control animate
public AlgoVisualizer(int screenWidth, int screenHeight, int N) {
//图形的数量以及初始化
int R = 100;
circles = new Circle[N];
for (int i = 0; i < circles.length; i++) {
int x = (int) (Math.random() * (screenWidth - 2 * R)) + R;
int y = (int) (Math.random() * (screenHeight - 2 * R)) + R;
int xv = (int) (Math.random() * 11) - 5;
int yv = (int) (Math.random() * 11) - 5;
circles[i] = new Circle(x, y, R, xv, yv);
}
EventQueue.invokeLater(() -> {
frame = new AlgoJFrame("Welcome", screenWidth, screenHeight);
frame.addKeyListener(new KeyListener());
frame.addMouseListener(new MouseListener());
new Thread(() -> {
while (true) {
run();
}
}).start();
});
}
//动画逻辑
private void run() {
frame.render(circles);
AlgoVisHelper.pause(10);
//更新数据
if (isAnimated) {
for (Circle circle : circles) {
circle.move(0, 0, frame.getCanvasWeight(), frame.getCanvasHeight());
}
}
}
private class KeyListener extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == ' ') {
isAnimated = !isAnimated;
}
}
}
private class MouseListener extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
e.translatePoint(0, -(frame.getBounds().height - frame.getCanvasHeight()));
// System.out.println(frame.getY());
for (Circle circle : circles) {
if (circle.contain(e.getPoint())) {
circle.isFilled = !circle.isFilled;
}
}
}
}
public static void main(String[] args) {
new AlgoVisualizer(1000, 1000, 8);
}
}
package graphic;
import javax.swing.*;
import java.awt.*;
public class AlgoJFrame extends JFrame {
private int canvasHeight;
private int canvasWeight;
public int getCanvasHeight() {
return canvasHeight;
}
public int getCanvasWeight() {
return canvasWeight;
}
public AlgoJFrame(String title, int canvasWeight, int canvasHeight) {
super(title);
this.canvasHeight = canvasHeight;
this.canvasWeight = canvasWeight;
//设置框架相关信息
setLocation(500, 10);
setVisible(true);
setResizable(false);
//设置面板相关信息
DrawJPanel panel = new DrawJPanel();
panel.setOpaque(true);
setContentPane(panel);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public AlgoJFrame(String title) {
this(title, 1024, 768);
}
//获取数据的接口
private Circle[] circles;
public void render(Circle[] circles) {
this.circles = circles;
repaint();
}
//显示相关的类
private class DrawJPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// 抗锯齿
RenderingHints hints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.addRenderingHints(hints);
int strokeWidth = 1;
//设置线条的粗细
AlgoVisHelper.setStrokeWidth(g2d, strokeWidth);
//设置图形颜色
AlgoVisHelper.setColor(g2d, Color.RED);
//绘制图形
for (Circle circle : circles) {
if (circle.isFilled) {
AlgoVisHelper.fillCircle(g2d, circle.x, circle.y, circle.getR());
} else {
AlgoVisHelper.strokeCircle(g2d, circle.x, circle.y, circle.getR());
}
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(canvasWeight, canvasHeight);
}
}
}
package graphic;
import java.awt.*;
public class Circle {
public int x;
public int y;
private int r;
private int xv;
private int yv;
public boolean isFilled = false;
public int getR() {
return r;
}
//设置圆的圆心位置 半径以及移动速度
public Circle(int x, int y, int r, int xv, int yv) {
this.x = x;
this.y = y;
this.r = r;
this.xv = xv;
this.yv = yv;
}
public void move(int minx, int miny, int maxx, int maxy) {
x += xv;
y += yv;
checkCollision(minx, miny, maxx, maxy);
}
//碰撞检测
private void checkCollision(int minx, int miny, int maxx, int maxy) {
if (x - r < minx) {
x = r; xv = -xv;
}
if (y -r < miny) {
y = r; yv = -yv;
}
if (x + r >= maxx) {
x = maxx - r; xv = -xv;
}
if (y + r >= maxy) {
y = maxy - r; yv = -yv;
}
}
public boolean contain(Point point) {
return (point.x - x) * (point.x - x) + (point.y - y) * (point.y - y) <= r * r;
}
}
package graphic;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class AlgoVisHelper {
public static void setColor(Graphics2D g2d, Color color) {
g2d.setColor(color);
}
public static void fillCircle(Graphics2D g2d, int x, int y, int r) {
Ellipse2D ellipse2D = new Ellipse2D.Double(x - r, y - r, 2 * r, 2 * r);
g2d.fill(ellipse2D);
}
public static void strokeCircle(Graphics2D g2d, int x, int y, int r) {
Ellipse2D ellipse2D = new Ellipse2D.Double(x - r, y - r, 2 * r, 2 * r);
g2d.draw(ellipse2D);
}
public static void setStrokeWidth(Graphics2D g2d, int strokeWidth) {
g2d.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
}
public static void pause(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}