package com.rockzhai.ai;
/**
* 主界面和业务逻辑
*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.Exception;
import java.lang.Math;
import java.lang.SecurityException;
import java.lang.String;
import java.lang.System;
import java.lang.Thread;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.event.MouseInputListener;
import javax.swing.filechooser.FileFilter;
public final class DrawingPanel extends FileFilter
implements ActionListener, MouseMotionListener, WindowListener {
// 常量
public static final String HEADLESS_PROPERTY = "my.headless";
public static final String MULTIPLE_PROPERTY = "my.multiple";
public static final String SAVE_PROPERTY = "my.save";
private static final String TITLE = "五子棋";
private static final Color GRID_LINE_COLOR = new Color(64, 64, 64, 128);
private static final int GRID_SIZE = 10; // 10px 网格间距
private static final int DELAY = 100; // 延时delay between repaints in millis
private static final int MAX_SIZE = 10000; // max width/height
private static final boolean DEBUG = true; // DeBug 开关
private static final boolean SAVE_SCALED_IMAGES = true; // true panel放大缩小时, 保留放大状态下的图片
private static int instances = 0;
private static Thread shutdownThread = null;
private static boolean hasProperty(String name) {
try {
return System.getProperty(name) != null;
} catch (SecurityException e) {
// 读值异常
if (DEBUG) System.out.println("Security exception when trying to read " + name);
return false;
}
}
// 返回主线程是否在运行 main is active
private static boolean mainIsActive() {
ThreadGroup group = Thread.currentThread().getThreadGroup();
int activeCount = group.activeCount();
// 在线程组中寻找主线程
Thread[] threads = new Thread[activeCount];
group.enumerate(threads);
for (int i = 0; i < threads.length; i++) {
Thread thread = threads[i];
String name = ("" + thread.getName()).toLowerCase();
if (name.indexOf("main") >= 0 ||
name.indexOf("testrunner-assignmentrunner") >= 0) {
// 找到主线程
// (TestRunnerApplet's main runner also counts as "main" thread)
return thread.isAlive();
}
}
// 没有找到主线程
return false;
}
// 自定义一个ImagePanel
private class ImagePanel extends JPanel {
private static final long serialVersionUID = 0;
private Image image;
public ImagePanel(Image image) {
setImage(image);
setBackground(Color.WHITE);
setPreferredSize(new Dimension(image.getWidth(this), image.getHeight(this)));
setAlignmentX(0.0f);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if (currentZoom != 1) {
g2.scale(currentZoom, currentZoom);
}
g2.drawImage(image, 0, 0, this);
// 为了调试方便加入的网格线
if (gridLines) {
g2.setPaint(GRID_LINE_COLOR);
for (int row = 1; row <= getHeight() / GRID_SIZE; row++) {
g2.drawLine(0, row * GRID_SIZE, getWidth(), row * GRID_SIZE);
}
for (int col = 1; col <= getWidth() / GRID_SIZE; col++) {
g2.drawLine(col * GRID_SIZE, 0, col * GRID_SIZE, getHeight());
}
}
}
public void setImage(Image image) {
this.image = image;
repaint();
}
}
// 控件
private int width, height; // 窗口 frame 的大小
private JFrame frame; // 总窗口的 frame
private JPanel panel; // 总的画布面板
private ImagePanel imagePanel; // 真正的绘画面板
private BufferedImage image; // 记录绘图的情况
private Graphics2D g2; // 2D绘图 graphics context
private JLabel statusBar; // 状态栏显示鼠标移动的位置
private JFileChooser chooser; // 保存选项 file chooser
private Timer timer; // 绘制的动画时间
private Color backgroundColor = Color.WHITE;
private boolean PRETTY = true; // 消除锯齿操作true to anti-alias
private boolean gridLines = false; //是否网格线
private int currentZoom = 1;
private int initialPixel; // 初始化每个像素点
// 根据width和height绘制一个panel
public DrawingPanel(int width, int height) {
if (width < 0 || width > MAX_SIZE || height < 0 || height > MAX_SIZE) {
throw new IllegalArgumentException("Illegal width/height: " + width + " x " + height);
}
//synchronized保证在同一时刻最多只有一个线程执行该段代码
synchronized (getClass()) {
instances++;
if (shutdownThread == null) {
shutdownThread = new Thread(new Runnable() {
public void run() {
try {
while (true) {
//完成执行主线程已经挂掉
if ((instances == 0 || shouldSave()) && !mainIsActive()) {
try {
System.exit(0);
} catch (SecurityException sex) {}
}
Thread.sleep(250);
}
} catch (Exception e) {}
}
});
shutdownThread.setPriority(Thread.MIN_PRIORITY);
shutdownThread.start();
}
}
this.width = width;
this.height = height;
if (DEBUG) System.out.println("w=" + width + ",h=" + height + ",graph=" + isGraphical() + ",save=" + shouldSave());
if (shouldSave()) {
// 图像不能超过256中颜色
image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED);
PRETTY = false; // 关闭抗锯齿,节省调色板颜色
// 用初始化的背景色填充frame,因为它不会透明显示ARGB图像
Graphics g = image.getGraphics();
g.setColor(backgroundColor);
// 加上1,防止width或height为0
g.fillRect(0, 0, width + 1, height + 1);
} else {
//ARGB
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}
initialPixel = image.getRGB(0, 0);
g2 = (Graphics2D) image.getGraphics();
g2.setColor(Color.BLACK);
没有合适的资源?快使用搜索试试~ 我知道了~
java实现采用Alpha-Beta剪枝搜索实现黑白棋AI源码(人工智能期末作业).zip
共13个文件
class:7个
java:2个
project:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 123 浏览量
2023-11-04
14:53:40
上传
评论 1
收藏 159KB ZIP 举报
温馨提示
java实现采用Alpha-Beta剪枝搜索实现黑白棋AI源码(人工智能期末作业).zip该项目是个人高分期末大作业设计项目源码,已获导师指导认可通过,都经过严格调试,确保可以运行!放心下载使用。 java实现采用Alpha-Beta剪枝搜索实现黑白棋AI源码(人工智能期末作业).zip该项目是个人高分期末大作业设计项目源码,已获导师指导认可通过,都经过严格调试,确保可以运行!放心下载使用。java实现采用Alpha-Beta剪枝搜索实现黑白棋AI源码(人工智能期末作业).zip该项目是个人高分期末大作业设计项目源码,已获导师指导认可通过,都经过严格调试,确保可以运行!放心下载使用。java实现采用Alpha-Beta剪枝搜索实现黑白棋AI源码(人工智能期末作业).zip该项目是个人高分期末大作业设计项目源码,已获导师指导认可通过,都经过严格调试,确保可以运行!放心下载使用。java实现采用Alpha-Beta剪枝搜索实现黑白棋AI源码(人工智能期末作业).zip该项目是个人高分期末大作业设计项目源码,已获导师指导认可通过,都经过严格调试,确保可以运行!放心下载使用。java实现采
资源推荐
资源详情
资源评论
收起资源包目录
基于Alpha-Beta剪枝博弈树的五子棋AI算法java实现.zip (13个子文件)
基于Alpha-Beta剪枝博弈树的五子棋AI算法java实现
.classpath 295B
.settings
org.eclipse.jdt.core.prefs 587B
src
com
rockzhai
ai
Ai.java 13KB
DrawingPanel.java 22KB
screenshot.png 126KB
bin
com
rockzhai
ai
DrawingPanel$ImagePanel.class 2KB
Ai.class 8KB
DrawingPanel.class 17KB
Node.class 1KB
DrawingPanel$1.class 983B
DrawingPanel$2.class 804B
MyMouseEvent.class 1KB
.project 361B
共 13 条
- 1
资源评论
盈梓的博客
- 粉丝: 9279
- 资源: 2197
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- HIVE-14706.01.patch
- C# WInForm IrisSkin2皮肤控件
- svn cleanup 失败怎么办
- Spring Boot集成Spring Security,HTTP请求授权配置:包含匿名访问、允许访问、禁止访问配置
- 易语言-画曲线模块及应用例程
- 电子元件行业知名厂商官网(TI/NXP/ST/Infineon/ADI/Microchip/Qualcomm/Diodes/Panasonic/TDK/TE/Vishay/Molex等)数据样例
- Cytoscape-3-10-0-windows-64bit.exe
- 基于STM32设计的宠物投喂器项目源代码(高分项目).zip
- 机器学习音频训练文件-24年抖音金曲
- 工业以太网无线通信解决方案
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功