java五子棋为什么添加绘制棋子的代码后显示白板
importjava.awt.Color;importjava.awt.Graphics;importjava.awt.Menu;importjava.awt.MenuB...
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class FiveChessFrame extends JFrame{
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
int x,y;
int[][] allChess = new int[30][30];
boolean isBlack=false;
boolean isWhite=false;
boolean canPlay=false;
private MenuBar bar;
private Menu menu;
private MenuItem start,close;
public FiveChessFrame(){
for(int i=0; i<30; i++)
for(int j=0; j<30; j++){
allChess[i][j] = 0;
}
this.setSize(820,620);
this.setLocation( (width-820)/2, (height-620)/2 );
this.setResizable(false);
bar = new MenuBar();
menu = new Menu("游戏");
start = new MenuItem("游戏开始");
close = new MenuItem("退出游戏");
bar.add(menu);
menu.add(start);
menu.add(close);
this.setMenuBar(bar);
this.setVisible(true);
handelEvent(this);
}
public void paint(Graphics g){
BufferedImage bi = new BufferedImage(820,620,BufferedImage.TYPE_INT_RGB);
Graphics g2 = bi.createGraphics();
g2.setColor(Color.ORANGE);
g2.fillRect(0, 0, 820, 620);
g2.setColor(Color.BLACK);
for(int i=0; i<32; i++){
g2.drawLine(0, 20*i, 620, 20*i);
g2.drawLine(20*i, 0, 20*i, 620);
}
////////添加绘制棋子后出现白板,没有这一段的话,棋盘显示正常
for(int i=0; i<31; i++)
for(int j=0; j<31; j++){
//黑子
if(allChess[i][j]==1){
g2.fillOval(10+20*i, 10+20*j, 20, 20);
}
//白子
if(allChess[i][j]==2){
g2.setColor(Color.WHITE);
g2.fillOval(10+i*20, 10+j*20, 20, 20);
}
}
g.drawImage(bi,0,0,this);
} 展开
import java.awt.Graphics;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class FiveChessFrame extends JFrame{
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
int x,y;
int[][] allChess = new int[30][30];
boolean isBlack=false;
boolean isWhite=false;
boolean canPlay=false;
private MenuBar bar;
private Menu menu;
private MenuItem start,close;
public FiveChessFrame(){
for(int i=0; i<30; i++)
for(int j=0; j<30; j++){
allChess[i][j] = 0;
}
this.setSize(820,620);
this.setLocation( (width-820)/2, (height-620)/2 );
this.setResizable(false);
bar = new MenuBar();
menu = new Menu("游戏");
start = new MenuItem("游戏开始");
close = new MenuItem("退出游戏");
bar.add(menu);
menu.add(start);
menu.add(close);
this.setMenuBar(bar);
this.setVisible(true);
handelEvent(this);
}
public void paint(Graphics g){
BufferedImage bi = new BufferedImage(820,620,BufferedImage.TYPE_INT_RGB);
Graphics g2 = bi.createGraphics();
g2.setColor(Color.ORANGE);
g2.fillRect(0, 0, 820, 620);
g2.setColor(Color.BLACK);
for(int i=0; i<32; i++){
g2.drawLine(0, 20*i, 620, 20*i);
g2.drawLine(20*i, 0, 20*i, 620);
}
////////添加绘制棋子后出现白板,没有这一段的话,棋盘显示正常
for(int i=0; i<31; i++)
for(int j=0; j<31; j++){
//黑子
if(allChess[i][j]==1){
g2.fillOval(10+20*i, 10+20*j, 20, 20);
}
//白子
if(allChess[i][j]==2){
g2.setColor(Color.WHITE);
g2.fillOval(10+i*20, 10+j*20, 20, 20);
}
}
g.drawImage(bi,0,0,this);
} 展开
1个回答
展开全部
你这段话不报错吗?我怎么感觉数组下标溢出了:
for(int i=0; i<31; i++)
for(int j=0; j<31; j++){
//黑子
if(allChess[i][j]==1){
g2.fillOval(10+20*i, 10+20*j, 20, 20);
//i 和j 都可以等于30 那么就会有allChess[30][xx] 而
//int[][] allChess = new int[30][30]; 下标是0--29
}
所以,我怀疑你的代码能运行出来吗?难道我理解错了?
要不,你将31 改为30看看!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询