java做五子棋的问题,请教高手
在画好棋盘后,通过drawImage函数来画棋子,在监听调用的时候不能做到,但是单独通过给定坐标却能在棋盘上边画到一个棋子,不知道是哪里出了问题,请教下高手,在线等待
----------------解决方案--------------------------------------------------------
最好贴出你的代码,让大家一起研究下
----------------解决方案--------------------------------------------------------
我写过一个,也用drawImage,没有出什么问题啊,不过功能不完整,图片也没法给你,
残缺代码你参考下,希望能有所帮助:
程序代码:
package day716;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class FiveFrame extends JFrame {
JButton start = new JButton("开始");
JButton back = new JButton("悔棋");
static JButton save = new JButton("保存");
JButton show = new JButton("回放");
JPanel panel1 = new JPanel();
JPanel boardPanel = new BoardPanel();// 棋盘面板
// 棋盘、棋子图片
BufferedImage imgBoard, imgBlack, imgWhite;
int[][] board = new int[15][15];
int currentColor = BLACK;
static final int BLACK = -1;
static final int WHITE = 1;
// 保存走棋顺序的集合
List<String> steps = new ArrayList<String>();
public FiveFrame() throws Exception {
imgBoard = ImageIO.read(FiveFrame.class
.getResourceAsStream("/image/chessboard.jpg"));
imgBlack = ImageIO.read(FiveFrame.class
.getResourceAsStream("/image/b.gif"));
imgWhite = ImageIO.read(FiveFrame.class
.getResourceAsStream("/image/w.gif"));
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
start();
}
});
back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
back();
}
});
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
save();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
show.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
show();
}
});
panel1.add(start);
panel1.add(back);
panel1.add(save);
panel1.add(show);
// 在棋盘面板上绘制棋盘图片
// boardPanel.getGraphics().drawImage(imgBoard,0,0,null);
this.add(panel1, BorderLayout.NORTH);
this.add(boardPanel);
this.setSize(540, 590);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("五子棋");
}
public void playChess(int row, int col) {
if (row < 0 || col < 0 || row > 14 || col > 14)
return;
if (board[row][col] != 0)
return;
board[row][col] = currentColor;
currentColor = -currentColor;
// 放入走棋步骤集合
steps.add(row + "," + col);
}
// 开始新游戏的方法
public void start() {
board = new int[15][15];// 新棋盘
currentColor = BLACK;// 黑棋先行
boardPanel.repaint();// 重新绘制面板
}
// 悔棋方法
public void back() {
if (steps.isEmpty())
return;
String s = steps.remove(steps.size() - 1);
String[] arr = s.split(",");
int row = Integer.parseInt(arr[0]);
int col = Integer.parseInt(arr[1]);
board[row][col] = 0;
currentColor = -currentColor;
boardPanel.repaint();
}
// 内部类,棋盘panel
class BoardPanel extends JPanel {
public BoardPanel() {
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
playChess(e.getY() / 35, e.getX() / 35);
repaint();
}
});
}
// 演示Demo
public void paintChess(int x, int y) {
int row = y / 35 * 35 + 4;
int col = x / 35 * 35 + 4;
Graphics g = getGraphics();
g.drawImage(imgBlack, col, row, null);
}
// 重写绘制panel的方法
// 在屏幕上绘制自身的方法
protected void paintComponent(Graphics g) {
super.paintComponent(g);// 执行父类中的代码将组件绘制
// 等组件绘制完成,接着绘制自己的内容
g.drawImage(imgBoard, 0, 0, null);
// 画走过的棋子
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[0].length; col++) {
if (board[row][col] != 0) {
BufferedImage img = imgBlack;
if (board[row][col] == WHITE) {
img = imgWhite;
}
g.drawImage(img, col * 35 + 4, row * 35 + 4, null);// 列计算x坐标,行计算y坐标
}
}
}
}
}
// 保存游戏
public void save() throws Exception {
System.out.println(steps);
Document doc = null;
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(
new File("/home/soft13/Desktop/myjava/five.xml"), true));
doc = DocumentHelper.createDocument();
Element five = doc.addElement("five");
int i = 0;
for (String str : steps) {
Element step = five.addElement("step");
Element row = step.addElement("row");
Element col = step.addElement("col");
Element color = step.addElement("color");
str = steps.get(i);
String[] str1 = str.split(",");
step.addAttribute("id", i + "");
row.addText(str1[0]);
col.addText(str1[1]);
i++;
color.addText(-currentColor + "");
currentColor = -currentColor;
boardPanel.repaint();
// writer.close();
// writer.flush();
}
OutputFormat f = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(out, f);
writer.write(doc);
writer.close();
}
/*
public void show() {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
//FiveFrame ff1 = new FiveFrame();
DefaultHandler dh = new DefaultHandler();
parser.parse("/home/soft13/Desktop/myjava/five.xml", dh);
} catch (Exception e) {
e.printStackTrace();
}
}*/
public static void main(String[] args) throws Exception {
// new FiveFrame().setVisible(true);
FiveFrame ff = new FiveFrame();
ff.setVisible(true);
}
}
class Chess{
public static final String XPATH_STEP="/five/step";
public static final String XPATH_ROL="/five/step/rol";
public static final String XPATH_COW="/five/step/cow";
public static final String XPATH_COLOR="/five/step/color";
private String step;
private String rol;
private String cow;
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getCow() {
return cow;
}
public void setCow(String cow) {
this.cow = cow;
}
public String getRol() {
return rol;
}
public void setRol(String rol) {
this.rol = rol;
}
public String getStep() {
return step;
}
public void setStep(String step) {
this.step = step;
}
}
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class FiveFrame extends JFrame {
JButton start = new JButton("开始");
JButton back = new JButton("悔棋");
static JButton save = new JButton("保存");
JButton show = new JButton("回放");
JPanel panel1 = new JPanel();
JPanel boardPanel = new BoardPanel();// 棋盘面板
// 棋盘、棋子图片
BufferedImage imgBoard, imgBlack, imgWhite;
int[][] board = new int[15][15];
int currentColor = BLACK;
static final int BLACK = -1;
static final int WHITE = 1;
// 保存走棋顺序的集合
List<String> steps = new ArrayList<String>();
public FiveFrame() throws Exception {
imgBoard = ImageIO.read(FiveFrame.class
.getResourceAsStream("/image/chessboard.jpg"));
imgBlack = ImageIO.read(FiveFrame.class
.getResourceAsStream("/image/b.gif"));
imgWhite = ImageIO.read(FiveFrame.class
.getResourceAsStream("/image/w.gif"));
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
start();
}
});
back.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
back();
}
});
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
save();
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
show.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
show();
}
});
panel1.add(start);
panel1.add(back);
panel1.add(save);
panel1.add(show);
// 在棋盘面板上绘制棋盘图片
// boardPanel.getGraphics().drawImage(imgBoard,0,0,null);
this.add(panel1, BorderLayout.NORTH);
this.add(boardPanel);
this.setSize(540, 590);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("五子棋");
}
public void playChess(int row, int col) {
if (row < 0 || col < 0 || row > 14 || col > 14)
return;
if (board[row][col] != 0)
return;
board[row][col] = currentColor;
currentColor = -currentColor;
// 放入走棋步骤集合
steps.add(row + "," + col);
}
// 开始新游戏的方法
public void start() {
board = new int[15][15];// 新棋盘
currentColor = BLACK;// 黑棋先行
boardPanel.repaint();// 重新绘制面板
}
// 悔棋方法
public void back() {
if (steps.isEmpty())
return;
String s = steps.remove(steps.size() - 1);
String[] arr = s.split(",");
int row = Integer.parseInt(arr[0]);
int col = Integer.parseInt(arr[1]);
board[row][col] = 0;
currentColor = -currentColor;
boardPanel.repaint();
}
// 内部类,棋盘panel
class BoardPanel extends JPanel {
public BoardPanel() {
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
playChess(e.getY() / 35, e.getX() / 35);
repaint();
}
});
}
// 演示Demo
public void paintChess(int x, int y) {
int row = y / 35 * 35 + 4;
int col = x / 35 * 35 + 4;
Graphics g = getGraphics();
g.drawImage(imgBlack, col, row, null);
}
// 重写绘制panel的方法
// 在屏幕上绘制自身的方法
protected void paintComponent(Graphics g) {
super.paintComponent(g);// 执行父类中的代码将组件绘制
// 等组件绘制完成,接着绘制自己的内容
g.drawImage(imgBoard, 0, 0, null);
// 画走过的棋子
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[0].length; col++) {
if (board[row][col] != 0) {
BufferedImage img = imgBlack;
if (board[row][col] == WHITE) {
img = imgWhite;
}
g.drawImage(img, col * 35 + 4, row * 35 + 4, null);// 列计算x坐标,行计算y坐标
}
}
}
}
}
// 保存游戏
public void save() throws Exception {
System.out.println(steps);
Document doc = null;
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(
new File("/home/soft13/Desktop/myjava/five.xml"), true));
doc = DocumentHelper.createDocument();
Element five = doc.addElement("five");
int i = 0;
for (String str : steps) {
Element step = five.addElement("step");
Element row = step.addElement("row");
Element col = step.addElement("col");
Element color = step.addElement("color");
str = steps.get(i);
String[] str1 = str.split(",");
step.addAttribute("id", i + "");
row.addText(str1[0]);
col.addText(str1[1]);
i++;
color.addText(-currentColor + "");
currentColor = -currentColor;
boardPanel.repaint();
// writer.close();
// writer.flush();
}
OutputFormat f = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(out, f);
writer.write(doc);
writer.close();
}
/*
public void show() {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
//FiveFrame ff1 = new FiveFrame();
DefaultHandler dh = new DefaultHandler();
parser.parse("/home/soft13/Desktop/myjava/five.xml", dh);
} catch (Exception e) {
e.printStackTrace();
}
}*/
public static void main(String[] args) throws Exception {
// new FiveFrame().setVisible(true);
FiveFrame ff = new FiveFrame();
ff.setVisible(true);
}
}
class Chess{
public static final String XPATH_STEP="/five/step";
public static final String XPATH_ROL="/five/step/rol";
public static final String XPATH_COW="/five/step/cow";
public static final String XPATH_COLOR="/five/step/color";
private String step;
private String rol;
private String cow;
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getCow() {
return cow;
}
public void setCow(String cow) {
this.cow = cow;
}
public String getRol() {
return rol;
}
public void setRol(String rol) {
this.rol = rol;
}
public String getStep() {
return step;
}
public void setStep(String step) {
this.step = step;
}
}
----------------解决方案--------------------------------------------------------
好多代码。。。
----------------解决方案--------------------------------------------------------