帮我看看这个程序,为什么 五子棋的网格显示不出来啊 ???

yzk775love 2007-11-22 11:14:32
package com.swing1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class ChessPanel extends JPanel { //棋盘;

private int space = 20; //网格间的距离;
private int grids = 30; //棋盘的网格数;
private int radius = space / 2; //棋的半径;

//当chesses[i][j]=0时,表示网格节点(i,j)上无棋;
//当chesses[i][j]=1时,表示网格节点(i,j)上放白棋;
//当chesses[i][j]=2时,表示网格节点(i,j)上放黑棋;
private int[][] chesses = new int[grids + 1][grids + 1];
private int currColor = 1; //当前棋盘的颜色;

private JMenuBar chessMenuBar = new JMenuBar(); //import javax.swing.JMenuBar;
private JMenu optMenu = new JMenu("操作"); //import javax.swing.JMenu;
private JMenuItem startMenItem = new JMenuItem("开始");
private JMenuItem exitMenuItem = new JMenuItem("退出");

private ActionListener startHandler = new ActionListener() {//开始菜单;
public void actionPerformed(ActionEvent e) {
clearGrids(); //清空棋盘;
currColor = 1;
repaint(); //刷新图形;
}
};

private ActionListener exitHandler = new ActionListener() {//退出;
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
};

private MouseListener playChessHandler = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
//放一棵棋子;
if (x <= grids * space && x >= 0 && y <= grids * space && y >= 0) {
if (chesses[round(x)][round(y)] == 0) {
chesses[round(x)][round(y)] = currColor;
currColor = currColor == 1 ? 2 : 1; //切换棋子的颜色;
repaint(); //刷新图形;
}
}
}
};

public int round(float a) { //获得接近a的网格节点坐标;
float f = a / space;
return Math.round(f);
}

public ChessPanel(int space, int grids) {
this.space = space;
this.grids = grids;
this.radius = space / 2;


setBackground(Color.YELLOW);
setSize(space * grids, space * grids);
startMenItem.addActionListener(startHandler);
exitMenuItem.addActionListener(exitHandler);
addMouseListener(playChessHandler);

chessMenuBar.add(optMenu);
optMenu.add(startMenItem);
optMenu.add(exitMenuItem);
}

public JMenuBar getMenuBar() {
return chessMenuBar;
}

/**画一个棋子 */
private void drawChess(Graphics g, int x, int y, int color) {
g.setColor(color == 1 ? Color.WHITE : Color.BLACK);
g.fillOval(x * space - radius, y * space - radius, radius * 2,
radius * 2);
}

/**画网格 */
private void drawGrids(Graphics g) {
g.setColor(Color.DARK_GRAY);
for (int i = 0; i <= grids; i++) {
g.drawLine(0, i * space, grids * space, i * space);
g.drawLine(i * space, 0, i * space, grids * space);
}
}

/**清空棋盘 */
private void clearGrids() {
for (int i = 0; i <= grids; i++) {
for (int j = 0; j <= grids; j++) {
chesses[i][j] = 0;
}
}
}

public void paintComponet(Graphics g) { //覆盖(重写)paintComponet方法;(绘制网格及棋子)
super.paintComponent(g); //必须先调用父类的方法;

this.drawGrids(g); //画网格;
for (int i = 0; i <= grids; i++) {
for (int j = 0; j <= grids; j++) {
if (chesses[i][j] != 0) {
drawChess(g, i, j, chesses[i][j]); //画棋子;
}
}
}
}

}


public class ChessPlayer extends JFrame {
private ChessPanel chessPanel = new ChessPanel(20, 30);

public ChessPlayer(String title) {
super(title);

Container contentPane = getContentPane();
contentPane.add(chessPanel);
setJMenuBar(chessPanel.getMenuBar());


setSize(600, 600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new ChessPlayer("五子棋/围棋");

}
}
...全文
146 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
hcmsxy 2007-11-23
  • 打赏
  • 举报
回复
public void paintComponet(Graphics g) { //覆盖(重写)paintComponet方法;(绘制网格及棋子)


换成


@Override
public void paint(Graphics g){


就应该可以了。

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧