import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
public class Listener implements MouseListener {
private Jpanel Panel;
int height;
private Graphics g;
private static ImageIcon icon = new ImageIcon("D:/java/代码/数据结构课设2/src/queen.PNG");
private Location temp;
private static final int SIZE = 8;
private static int count = 0;
static LinkedList<Location> list = new LinkedList<Location>();
public Listener(Jpanel P) {
Panel = P;
}
public Listener() {
}
static class Location {
int x;
int y;
Location(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "(" + x + ", " + y + ")";
}
}
private void eightQueen(LinkedList<Location> list, int x, int y) throws Exception {
if (list.size() == SIZE) {
printLocation(list);
return;
}
for (int i = x; i < SIZE; i++) {
Location loc = new Location(i, y);
if (isLegalLoc(loc)) {
list.offer(loc);
icon.paintIcon(Panel, g, Jpanel.x[loc.x], Jpanel.y[loc.y]);
Thread.sleep(30);
eightQueen(list, 0, y + 1);
temp = list.pollLast();
g.clearRect(Jpanel.x[temp.x], Jpanel.y[temp.y], icon.getIconWidth(), icon.getIconHeight());
Thread.sleep(30);
}
}
}
private boolean isLegalLoc(Location loc) {
for (Location each : list) {
if (loc.x == each.x || loc.y == each.y)
return false;
else if (Math.abs(loc.x - each.x) == Math.abs(loc.y - each.y))
return false;
}
return true;
}
private void printLocation(LinkedList<Location> list) throws InterruptedException {
count++;
StringBuilder str = new StringBuilder("路径" + count + ":");
for (Location each : list) {
System.out.print(each.toString() + "\t");
str.append(each.toString());
}
Panel.textArea.append(str.toString() + "\r\n");
//Panel.textArea.setCaretPosition(Panel.textArea.getText().length());
Panel.textArea.paintImmediately(Panel.textArea.getX(), Panel.textArea.getY(), Panel.textArea.getWidth(),
Panel.textArea.getHeight());
Panel.textArea.setCaretPosition(Panel.textArea.getDocument().getLength());
Panel.invalidate();
System.out.println();
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
JButton btn = (JButton) (e.getSource());
if (btn.getActionCommand().trim().equals("动态展示")) {
try {
g = Panel.getGraphics();
eightQueen(list, 0, 0);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
X-MTing
- 粉丝: 2w+
- 资源: 15
最新资源
- WebSocket:实时通信的利器-pdf
- WebSocket:实现实时网络通信的强大工具.pdf
- PMSM永磁同步电机参数辨识仿真,适用于表贴式,内嵌式永磁同步电机: 辨识内容: ① 定子电阻,精度在0.1%左右; ② DQ电感辨识(脉冲电压法),精度在0.02%左右; ③ 转子磁链辨识,精度在0
- WebSocket:推动实时网络通信的变革力量.pdf
- YUNYUNyun云云
- STM32开发的串口接收不定长数据源码, 可解决串口延时处理,数据丢失等适合串口传输大量数据,可移植性非常广,程序源码注释详细,非常适合开发人员,需要可以看下
- 130道Python基础练习题
- 电子科技大学ASIC作业参考
- 低成本NI实时仿真机刷机文件教程,包括机箱的配置,启动盘的生成,刷机教程等等,可用于跑CarSim模型和simulink实时模型,支持labview-rt和veristand-rt 文档资料
- java decompiler (java反编译工具)
- 毕业设计A049-基于Java的大学城水电管理系统的设计与实现
- MATLAB代码:基于条件风险价值CVaR的微网动态定价与调度策略 关键词:P2P交易 微网优化调度 条件风险价值 合作博弈 动态定价 参考文档:加好友获取 仿真平台:MATLAB yalmip+c
- 毕业设计B056-基于Java的班级回忆录的设计与实现
- 电机控制器,电动车电驱方案,主动阻尼控制,damping control,转矩补偿,振动、谐振抑制 公司多个量产实际项目中用的, matlab二质量模型… 使用巴特沃斯高通滤波器提取转速波动进行转矩补
- KPCA-ISSA-SVM分类预测,基于核主成分分析和改进的麻雀搜索算法优化支持向量机故障诊断分类预测,MATLAB代码(含SVM、SSA-SVM、ISSA-SVM、KPCA-ISSA-SVM四个模型
- linux常用命令大全笔记.md
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈