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
- 粉丝: 1w+
- 资源: 15
最新资源
- (源码)基于物联网技术的办公自动化与安全管理系统.zip
- (源码)基于Spring Boot和Vue的若依管理系统.zip
- C#汽车4S综合管理系统源码 汽车美容4S店管理服务源码数据库 SQL2008源码类型 WebForm
- (源码)基于Arduino的STM32F1F4语音录制与播放系统.zip
- (源码)基于ASP.NET Boilerplate框架的微信支付管理系统.zip
- win屏幕墙,桌面画面动态更新,远程操作
- 基于纯JS实现的三维光学引擎
- “碰一碰”渠道营销源码
- (源码)基于Spring Boot框架的电商系统.zip
- 【重磅,更新!】全国290多个地级市资源错配指数、劳动和资本相对扭曲指数
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈