package mywuziqi;
//import javax.swing.*;
//import javax.swing.*;
//import javax.swing.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class mychessjframe extends JFrame implements MouseListener {
int x = 0;
int y = 0;
//0表示没有棋子 1表示黑子 2表示白子
int[][] allchess = new int[21][21];
boolean is = false;
// 取得桌面的宽高
// int width = Toolkit.getDefaultToolkit().getScreenSize().width;
// int height = Toolkit.getDefaultToolkit().getScreenSize().height;
public mychessjframe() {
this.setTitle("五子棋"); //窗口标题
this.setSize(500, 500); //画布大小
// this.setLocation(width-500/2,height-500/2);
this.setLocationRelativeTo(null); //居中
this.setResizable(false); //窗口大小不可变
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(this);
this.addMouseListener(this);
this.setVisible(true); //窗口显示
}
public void paint(Graphics g) {
BufferedImage image = null;
try {
image = ImageIO.read(new File("D:/pic/bg.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
g.drawImage(image, 0, 0, this);
g.setFont(new Font("黑体", 40, 40));
g.drawString("游戏信息", 10, 70);
g.setFont(new Font("宋体", 0, 15));
g.drawString("黑方时间:无限制", 10, 480);
g.drawString("白方时间:无限制", 250, 480);
for (int i = 0; i < 21; i++) {
g.drawLine(10, 100 + i * 15, 310, 100 + i * 15);
g.drawLine(10 + i * 15, 100, 10 + i * 15, 400);
}
// g.fillOval(96, 160, 5, 5);
// g.fillOval(360, 160, 5, 5);
// g.fillOval(96, 339, 5, 5);
// g.fillOval(360, 340, 5, 5);
// g.fillOval(x,y,12,12);
// allchess[1][1]=1;
// g.fillOval(10+30-5,100+30-5,10,10);
for (int i = 0; i < 21; i++) {
for (int j = 0; j < 21; j++) {
//如果等于1画黑旗
if (allchess[i][j] == 1) {
int tempx = i * 15 + 10;
int tempy = j * 15 + 100;
g.fillOval(tempx - 5, tempy - 5, 10, 10);
}
//如果等于2画白棋
if (allchess[i][j] == 2) {
int tempx = i * 15 + 10;
int tempy = j * 15 + 100;
g.setColor(Color.white);
g.fillOval(tempx - 5, tempy - 5, 10, 10);
g.setColor(Color.black);
g.drawOval(tempx - 5, tempy - 5, 10, 10);
}
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
// System.out.println(x);
// System.out.println(y);
if (x > 10 && x < 450 && y > 100 && y < 400) {
x = (x - 10) / 15;
y = (y - 100) / 15;
if (is) {
allchess[x][y] = 1;
is = false;
System.out.println("执行了if");
}
else {
allchess[x][y] = 2;
is = true;
System.out.println("执行了else");
}
// allchess[x][y]=1;
this.repaint();
}
else {
System.out.println("鼠标位置超出棋盘");
}
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}