📄 mainpanel.java
字号:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class mainPanel extends JPanel implements MouseListener,MouseMotionListener{
static int[][] chessMap;
static int black_or_red;
private string winner;
mainPanel(){
black_or_red=-1;
chessMap=new int[28+1][28+1];
for(int i=0;i<=28;i++)
for(int j=0;j<=28;j++)
chessMap[i][j]=0;
setOpaque(true);
addMouseListener(this);
addMouseMotionListener(this);
//cHead=a;
}
public void drawchess(Graphics g,int i,int j){
int x=i*20+20;
int y=j*20+20;
g.setColor(Color.BLUE);
g.drawRect(x,y,20,20);
if(chessMap[i][j] == 1 )
{
g.setColor(Color.gray);
g.drawOval(x-8,y-8,16,16);
g.setColor(Color.black);
g.fillOval(x-8,y-8,16,16);
//g.fillRect(x, y, 20, 20);
}
if(chessMap[i][j] == 2 )
{
g.setColor(Color.gray);
g.drawOval(x-8,y-8,16,16);
g.setColor(Color.red);
g.fillOval(x-8,y-8,16,16);
//g.fillRect(x, y, 20, 20);
}
}
public void restart(){
for(int i=0;i<=28;i++)
for(int j=0;j<=28;j++)
chessMap[i][j]=0;
repaint();
}
public void paintComponent(Graphics g){
//super.paintComponent(g);
for(int i=0;i<=26;i++){
for(int j=0;j<=26;j++){
drawchess(g,i,j);
}
}
// g.drawLine(10,10,100,100);
}
public void mouseClicked(MouseEvent e)
{
//restart();
// System.out.println("here");
//int x = evt.getX() / 20;
//int y = evt.getY() / 20;
}
public void mouseReleased(MouseEvent evt)
{
}
public void mouseEntered(MouseEvent mouseevt)
{
}
public void mouseExited(MouseEvent mouseevent)
{
}
public void mouseMoved(MouseEvent moveevt){
// int x=(moveevt.getX()-10)/20;
// int y=(moveevt.gety()-10)/20;
// repaint();
}
public void mouseDragged(MouseEvent evt){
}
public void mousePressed(MouseEvent e) {
restart();
int x = (e.getX()-10) / 20;
int y = (e.getY()-10) / 20;
int count=0;
int ax=x,ay=y;
if (e.getModifiers()==MouseEvent.BUTTON1_MASK)
{
JOptionPane.showMessageDialog( mychess.thisGame, " are Winner!!","Win",1);
if(black_or_red<0)
{
chessMap[ax][ay]=1;
black_or_red=1;
}
else
{
chessMap[ax][ay]=2;
black_or_red=-1;
}
}
JOptionPane.showMessageDialog( mychess.thisGame, " are Winner!!","Win",1);
//看上下是否有5个棋子
while(chessMap[ax][ay]==chessMap[x][y]){
count++;
if(ax<26) ax++;
JOptionPane.showMessageDialog( mychess.thisGame, " are Winner!!","Win",1);
}
ax=x;count--;
while(chessMap[ax][ay]==chessMap[x][y]){
count++;
if(ax==0) break;
if(ax>0) ax--;
}
if(count>2)
{
if(chessMap[ax][ay]==1)
{
winner="blackplayer"
}
else
{
winner="REDplayer";
}
JOptionPane.showMessageDialog( mychess.thisGame, winner+" are Winner!!","Win",1);
restart();
return;
}
repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -