我想画一个8*8的棋盘,现在只实现了轮流下子(黑白交替),具体算法还未实现。每一个格子是一个Cell类,用JPanel实现,画棋子是在paintComponent中用画圆实现的。但是现在有一个问题就是我每点一个格子,棋子倒是出现了,但是乱七八糟,感觉像没有刷新,但是我只要稍微拉动一下整个棋盘的窗口(不管怎么拉动都可以),棋子的显示就正常了,请问各位这是为什么?谢谢。
代码如下:
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.border.LineBorder;public class othello extends JFrame{
private char whoseTurn='B';
private Cell[][] cells=new Cell[8][8]; //绘制8*8棋盘
private JLabel turn=new JLabel("B's turn Black: 0 White: 0");
private JPanel p=new JPanel(new GridLayout(8,8));
int black_count=0;
int white_count=0;
void init() {
setSize(500, 500);
setLocation(300, 100);
setTitle("Test");
setBackground(Color.gray);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
p.add(cells[i][j]=new Cell());
}
}
getContentPane().add(turn,BorderLayout.SOUTH);
getContentPane().add(p,BorderLayout.CENTER);
setVisible(true);
}
public othello(){
init();
}
//inner class Cell类
public class Cell extends JPanel implements MouseListener{
private char token=' ';
public Cell(){
setBorder(new LineBorder(Color.black,1));
addMouseListener(this);
}
public char getToken(){
return token;
}
public void setToken(char c){
token=c;
repaint();
}
protected void paintComponent(Graphics g){
super.paintComponents(g);
//画黑子
if(token=='B'){
g.fillOval(4, 2, 50, 50);
g.setColor(Color.black);
}
//画白子
else if(token=='W'){
g.drawOval(4, 2, 50, 50);
//g.setColor(Color.white);
}
}
public void mouseClicked(MouseEvent e){
if(token==' ' && whoseTurn!=' '){
setToken(whoseTurn);
whoseTurn=(whoseTurn=='B')?'W':'B';
if(getToken()=='B'){
black_count++;
}
else if(getToken()=='W'){
white_count++;
}
turn.setText(whoseTurn+"'s turn Black: "+black_count+" White: "+white_count);
}
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
} public static void main(String[] args){
othello frame=new othello();
}}
代码如下:
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.border.LineBorder;public class othello extends JFrame{
private char whoseTurn='B';
private Cell[][] cells=new Cell[8][8]; //绘制8*8棋盘
private JLabel turn=new JLabel("B's turn Black: 0 White: 0");
private JPanel p=new JPanel(new GridLayout(8,8));
int black_count=0;
int white_count=0;
void init() {
setSize(500, 500);
setLocation(300, 100);
setTitle("Test");
setBackground(Color.gray);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
p.add(cells[i][j]=new Cell());
}
}
getContentPane().add(turn,BorderLayout.SOUTH);
getContentPane().add(p,BorderLayout.CENTER);
setVisible(true);
}
public othello(){
init();
}
//inner class Cell类
public class Cell extends JPanel implements MouseListener{
private char token=' ';
public Cell(){
setBorder(new LineBorder(Color.black,1));
addMouseListener(this);
}
public char getToken(){
return token;
}
public void setToken(char c){
token=c;
repaint();
}
protected void paintComponent(Graphics g){
super.paintComponents(g);
//画黑子
if(token=='B'){
g.fillOval(4, 2, 50, 50);
g.setColor(Color.black);
}
//画白子
else if(token=='W'){
g.drawOval(4, 2, 50, 50);
//g.setColor(Color.white);
}
}
public void mouseClicked(MouseEvent e){
if(token==' ' && whoseTurn!=' '){
setToken(whoseTurn);
whoseTurn=(whoseTurn=='B')?'W':'B';
if(getToken()=='B'){
black_count++;
}
else if(getToken()=='W'){
white_count++;
}
turn.setText(whoseTurn+"'s turn Black: "+black_count+" White: "+white_count);
}
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
} public static void main(String[] args){
othello frame=new othello();
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货