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);
盈梓的博客
- 粉丝: 9560
- 资源: 2308
最新资源
- 植物树木检测11-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 免费下载:Advanced Sensing in Image Processing and Iot .zip
- 免费下载:Azure DevOps 設計策略與實戰分析:開發工程師從入門到進階完全指南 (范明城) _OCLPo.zip
- 免费下载:Adobe Lightroom A Complete Course and Compendium of Features (Jeff Carlson) _pJXB6.zip
- 马铃薯食物缺陷图像分类数据集【已标注,约450张数据】
- 免费下载:2023王道计算机数据结构考研复习指导 (王道) _QmvQ5.zip
- 免费下载:BBQ Days, BBQ Nights Barbecue recipes for year-round feasting (Helen Graves) _QBCTA.zip
- 免费下载:An Introductory Course on Molecular Biology (Ramón Serrano) _qKhUB.zip
- 免费下载:Annie Ernaux (Cahiers de LHerne) (Pierre-Louis Fort) _nvcIk.zip
- 免费下载:BBQ Days, BBQ Nights Barbecue Recipes for Year-Round Feasting (Helen Graves) (1)_i4w8g.zip
- 免费下载:Bao Dim Sum 60 Easy Bun and Dumpling Recipes (Orathay Souksisavanh) _1wc6l.zip
- 免费下载:2024年王道计算机网络复习指导 (王道计算机) _cvEal.zip
- 免费下载:2023王道计算机组成原理考研复习指导 (王道) _vYpLL.zip
- 免费下载:2023计算机组成原理考研复习指导 (王道论坛) _Y5xRO.zip
- 免费下载:Andai Sel-Sel Dalam Tubuhmu Berbicara (Rizal Do) _DZduB.zip
- 免费下载:Acting Gods, Playing Heroes, and the Interaction Between Judaism, Christianity.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈