问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class MyFive extends JFrame implements MouseListener{
int x=0;
int y=0;
int chess[][]=new int[15][15];
boolean isblack=true;
boolean canplay =true;
String messageString="黑子先行";
public MyFive()
{
this.setTitle("五子棋");
this.setSize(550,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(500,500);
this.setResizable(true);
int width =java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
int height =java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
this.setLocation((width-600)/2,(height-500)/2);
this.addMouseListener(this);
this.setVisible(true);
}
@Override
public void paint(Graphics g) {
BufferedImage b=new BufferedImage(500,500, BufferedImage.TYPE_INT_ARGB);
Graphics g2=b.createGraphics();
BufferedImage image = null;
try {
image = ImageIO.read(new File("D:/eclipse/PHOTO/4.jpeg"));
} catch (IOException e) {
e.printStackTrace();
}
g2.setColor(Color.BLACK);
g2.setFont(new Font("宋体",0,14));
g2.drawImage(image,3,20,this);
g2.drawString("游戏信息:"+messageString,370, 50);
g2.drawString("黑方时间", 30, 470);
g2.drawString("白方时间", 250, 470);
for (int i = 0; i <15; i++) {
g2.drawLine(20, 40+25*i, 420, 40+25*i);
g2.drawLine(20+25*i, 40, 20+25*i, 410);
}
g2.drawRect(430,66,55,20);
g2.drawString("重新开始ʼ",432,80);
g2.drawRect(430, 90, 55, 20);
/* g2.drawString("悔棋", 442, 135);
g2.drawRect(430, 120, 55, 20);*/
g2.drawString("认输", 440, 105);
g.drawImage(b, 0, 0,this);
g2.fillOval(x, y, 124, 12);
for (int i = 0; i < 15; i++) {
for (int j = 0; j <15; j++) {
if (chess[i][j]==1)
{
int temx=i*25+20;
int temy=j*25+40;
g2.fillOval(temx-6, temy-6, 12, 12);
}
if (chess[i][j]==2) {
int temx=i*25+20;
int temy=j*25+40;
g2.setColor(Color.WHITE);
g2.fillOval(temx-4, temy-4, 14, 14);
g2.setColor(Color.BLACK);
g2.drawOval(temx-4, temy-4, 14, 14);
}
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
//System.out.println("x"+e.getX());
//System.out.println("y"+e.getY());
x=e.getX();
y=e.getY();
if (x>=20&&x<=370&&y>=40 &&y<=390)
{
x= (x-20)/25;
y= (y-40)/25;
if (chess[x][y]==0) {
if (isblack==true) {
chess[x][y]=1;
isblack=false;
messageString="轮到黑方";
}else {
chess[x][y]=2;
isblack=true;
messageString="轮到白方";
}
boolean ace= this.winner();
if (ace==true) {
JOptionPane.showConfirmDialog(this, "游戏结束,"+(chess[x][y]==1 ? "黑方":"白方")+"获胜");
}
}
else {
JOptionPane.showMessageDialog(this, "该地方已有子");
}
this.repaint();
}
if(e.getX()>=430&&e.getX()<=485&&e.getY()>= 66&& e.getY()<=80)
{
int result= JOptionPane.showConfirmDialog(this, "是否重新开始");
if (result==0)
{
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
chess[i][j] = 0; //清空
}
messageString="轮到黑方";
isblack=true;
this.repaint();
}}}
if (e.getX() >= 400 && e.getX() <= 470 && e.getY() >= 270 && e.getY() <= 300)
{
int result1 = JOptionPane.showConfirmDialog(this, "是否认输");
if (result1 == 0)
{
if (isblack)
{
JOptionPane.showMessageDialog(this, "黑棋认输");
} else
{
JOptionPane.showMessageDialog(this, "白棋认输");
}
}
}
}
private boolean winner() {
boolean flag =false;
int co=1;
int color = chess[x][y];
int i=1;
while( color==chess[x+i][y] )
{
co++;
i++;
}
i=1;
while( color==chess[x-i][y] )
{
co++;
i++;
}
if (co>=5) {
flag=true;
}
int co1=1;
int i2=1;
while( color==chess[x][y+i2] )
{
co1++;
i2++;
}
i2=1;
while( color==chess[x][y-i2] )
{
co1++;
i2++;
}
if (co1>=5) {
flag=true;
}
int co2=1;
int i3=1;
while( color==chess[x+i3][y-i3] )
{
co2++;
i3++;
}
i3=1;
while( color==chess[x-i3][y+i3] )
{
co2++;
i3++;
}
if (co2>=5) {
flag=true;
}
int co3=1;
int i4=1;
while( color==chess[x+i4][y-i4] )
{
co3++;
i4++;
}
i4=1;
while( color==chess[x-i4][y+i4] )
{
co3++;
i4++;
}
if (co3>=5) {
flag=true;
}
int co4=1;
int i5=1;
while( color==chess[x+i5][y+i5] )
{
co4++;
i5++;
}
i5=1;
while( color==chess[x-i5][y-i5] )
{
co4++;
i5++;
}
if (co4>=5)
{
flag=true;
}
return flag;
}
}

运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果