java五子棋代如何实现点击棋盘显示出棋子的事件

我的棋盘是画的(非常感谢您的答案,我会给您一个满意的分数的)我现在是不知道如何用那个paint()方法去实现... 我的棋盘是画的(非常感谢您的答案,我会给您一个满意的分数的)我现在是不知道如何用那个paint()方法去实现 展开
 我来答
Shisoft
2011-02-14
知道答主
回答量:9
采纳率:0%
帮助的人:0
展开全部
获取点击的位置和你画的棋子位置进行比较
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
hartx
2011-02-15 · TA获得超过332个赞
知道答主
回答量:199
采纳率:0%
帮助的人:159万
展开全部
以下的code只是使用了很简单的Graphic2D, 加入背景, 用棋子图片代替黑色和白色的圈, 再加入胜利判定应该就是个很完整的五子棋游戏了.

左上角的菜单可以清空棋盘/退出程序.

修改以下的值可以调整棋盘有几行,几列,以及每个方格的边长.
private final static int ROWS = 20;
private final static int COLUMNS = 30;
private final static int SIDE = 32;

希望有帮助.
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class GomokuBlackWhite extends JPanel
implements ActionListener, MouseListener {
private enum Pieces {
WHITE,
BLACK,
NOT_USED;
}
private final static int ROWS = 20;
private final static int COLUMNS = 30;
private final static int SIDE = 32;
private boolean isBlack;
private Graphics2D g2d;
private Pieces[][] pieces;
private static JFrame frame;
private static JMenuBar menuBar;

public GomokuBlackWhite () {
isBlack = true;
pieces = new Pieces[COLUMNS - 1][ROWS - 1];
frame = new JFrame();
menuBar = new JMenuBar();
JMenu menu = new JMenu("Game");
JMenuItem resetAction = new JMenuItem("Reset");
JMenuItem exitAction = new JMenuItem("Exit");
resetAction.addActionListener(this);
exitAction.addActionListener(this);
menu.add(resetAction);
menu.add(exitAction);
menuBar.add(menu);

resetBoard();
setPreferredSize(new Dimension(COLUMNS * SIDE, ROWS * SIDE));
addMouseListener(this);
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g2d = (Graphics2D) g;
drawBoard(g2d);
drawPieces(g2d);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Reset")) {
resetBoard();
this.repaint();
}
else
frame.dispose();
}
@Override
public void mouseClicked(MouseEvent e) {
updateBoard(e.getX(), e.getY());
this.repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}

private void drawBoard(Graphics2D g) {
g.drawLine(0, 0, COLUMNS * SIDE, 0);
g.drawLine(0, ROWS * SIDE, COLUMNS * SIDE, ROWS * SIDE);
g.drawLine(0, 0, 0, ROWS * SIDE);
g.drawLine(COLUMNS * SIDE, 0 * SIDE, COLUMNS * SIDE, ROWS * SIDE);
for (int i = 1; i < ROWS; i++) {
g.drawLine(0, SIDE * i, SIDE / 2, SIDE * i);
g.drawLine(COLUMNS * SIDE - (SIDE / 2), SIDE * i,
COLUMNS * SIDE, SIDE * i);
}
for (int j = 1; j < COLUMNS; j++) {
g.drawLine(SIDE * j, 0, SIDE * j, SIDE / 2);
g.drawLine(SIDE * j, SIDE * ROWS - (SIDE / 2),
j * SIDE, ROWS * SIDE);
}
for (int i = 0; i < COLUMNS - 1; i++) {
for (int j = 0; j < ROWS - 1; j++) {
if (pieces[i][j] == Pieces.NOT_USED) {
g.drawLine((SIDE / 2) + (i * SIDE),
(j + 1) * SIDE,
((SIDE / 2) + ((i + 1) * SIDE)),
(j + 1) * SIDE);
g.drawLine((i + 1) * SIDE,
(SIDE / 2) + (j * SIDE),
(i + 1) * SIDE,
(SIDE / 2) + ((j + 1) * SIDE));
}
}
}
}

private void drawPieces(Graphics2D g) {
for (int i = 0; i < COLUMNS - 1; i++) {
for (int j = 0; j < ROWS - 1; j++) {
if (pieces[i][j] != Pieces.NOT_USED) {
int x = i * SIDE;
int y = j * SIDE;
if (pieces[i][j] == Pieces.BLACK) {
g.fillOval(x + (SIDE / 2), y + (SIDE / 2), SIDE, SIDE);
}
if (pieces[i][j] == Pieces.WHITE) {
g.drawOval(x + (SIDE / 2), y + (SIDE / 2), SIDE, SIDE);
}
}
}
}
}

private void updateBoard(int x, int y) {
int centreX, centreY;
if (x % SIDE <= SIDE / 2) {
centreX = x / SIDE - 1;
}
else {
centreX = x / SIDE;
}
if (y % SIDE <= SIDE / 2) {
centreY = y / SIDE - 1;
}
else {
centreY = y / SIDE;
}
if (centreX < 0 || centreY < 0 || centreX > (COLUMNS - 2)
|| centreY > (ROWS - 2)) {
JOptionPane.showMessageDialog(null,
"You cannot place a piece here.");
}
else {
if (pieces[centreX][centreY] != Pieces.NOT_USED) {
JOptionPane.showMessageDialog(null,
"You cannot place a piece above another one.");
}
else {
pieces[centreX][centreY] =
(isBlack) ? Pieces.BLACK : Pieces.WHITE;
isBlack = !isBlack;
this.repaint();
}
}
}

private void resetBoard() {
for (int i = 0; i < COLUMNS - 1; i++) {
for (int j = 0; j < ROWS - 1; j++) {
pieces[i][j] = Pieces.NOT_USED;
}
}
}

private static void createAndShowGUI() {
JPanel panel = new GomokuBlackWhite();

frame.setTitle("Connect Five");
frame.setJMenuBar(menuBar);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式