java写五子棋 关于鼠标事件的问题
初学者,自己尝试编写人人对战的五子棋。但是鼠标事件弄不好。目前写成下面这个样子,却达不到目的,也没什么办法,请高人指教。源代码如下:importjava.awt.even...
初学者,自己尝试编写人人对战的五子棋。
但是鼠标事件弄不好。目前写成下面这个样子,却达不到目的,也没什么办法,请高人指教。
源代码如下:import java.awt.event.*;
import java.awt.* ;
import javax.swing.* ;
import java.lang.* ;
class Qipan extends JFrame implements MouseListener,MouseMotionListener{
Qipan(String title){
this.setTitle(title);
this.setSize(580,630);
this.setLocation(225,50);
this.setBackground(Color.WHITE);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane=getContentPane();
contentPane.addMouseListener(this);
contentPane.addMouseMotionListener(this);
}
static int zx=0,zy=0;
//画棋盘
public void paint(Graphics g){
super.paint(g);
for(int i=40;i<=540;i=i+20){
g.setColor(Color.black);
g.drawLine(i,80,i,580);
}
for(int i=80;i<=580;i=i+20){
g.setColor(Color.black);
g.drawLine(40,i,540,i);
}
int i,j;
g.drawString("游戏开始:",40,60);
Qizi q = new Qizi();
Xiaqi x = new Xiaqi();
int label=1;
int sign=0;
int gezi[][] = new int[33][33];
for(int m=4;m<30;m++)
for(int n=4;n<30;n++)
gezi[m][n]=0;
while(sign==0){
switch(label){
case 1:g.clearRect(350,60,100,20);
g.drawString("请红方落子...",300,60);
i=zx/20+2;
j=zy/20;
while(gezi[i][j]==0){
gezi[i][j]=1;
q.huaqizi(g,zx,zy,Color.red);
sign=x.panduan(i,j);
label=2;
}break;
case 2:g.clearRect(350,60,100,20);
g.drawString("请绿方落子...",300,60);
i=zx/20+2;
j=zy/20;
while(gezi[i][j]==0){
gezi[i][j]=1;
q.huaqizi(g,zx,zy,Color.green);
sign=x.panduan(i,j);
label=1;
}
}
}
//弹出游戏结束对话框
if(sign==1) {
JOptionPane.showMessageDialog(null,
"红方获胜!!!",
"Game Over !", JOptionPane.ERROR_MESSAGE);
}
else{
JOptionPane.showMessageDialog(null,
"绿方获胜!!!",
"Game Over !", JOptionPane.ERROR_MESSAGE);
}
//结束游戏
System.exit(0) ;
}
public void mousePressed(MouseEvent e){
zx=e.getX();
zy=e.getY();
}
public void mouseEntered(MouseEvent e){ }
public void mouseExited(MouseEvent e){ }
public void mouseClicked(MouseEvent e){ }
public void mouseReleased(MouseEvent e){ }
public void mouseMoved(MouseEvent e){ }
public void mouseDragged(MouseEvent e){ }
}
//画棋子
class Qizi{
int x;
int y;
Color z;
public void huaqizi(Graphics g,int x,int y,Color z){
g.setColor(z);
g.fillOval(x,y,14,14);
}
}
public class Test extends JFrame{
public static void main(String args[]){
Qipan q = new Qipan("欢迎使用bluebottle的五子棋(测试版)");
q.setVisible(true);
}
}
由于程序太长 ,我把Xiaqi类和里面的判断方法puanduan()省略了…… 展开
但是鼠标事件弄不好。目前写成下面这个样子,却达不到目的,也没什么办法,请高人指教。
源代码如下:import java.awt.event.*;
import java.awt.* ;
import javax.swing.* ;
import java.lang.* ;
class Qipan extends JFrame implements MouseListener,MouseMotionListener{
Qipan(String title){
this.setTitle(title);
this.setSize(580,630);
this.setLocation(225,50);
this.setBackground(Color.WHITE);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane=getContentPane();
contentPane.addMouseListener(this);
contentPane.addMouseMotionListener(this);
}
static int zx=0,zy=0;
//画棋盘
public void paint(Graphics g){
super.paint(g);
for(int i=40;i<=540;i=i+20){
g.setColor(Color.black);
g.drawLine(i,80,i,580);
}
for(int i=80;i<=580;i=i+20){
g.setColor(Color.black);
g.drawLine(40,i,540,i);
}
int i,j;
g.drawString("游戏开始:",40,60);
Qizi q = new Qizi();
Xiaqi x = new Xiaqi();
int label=1;
int sign=0;
int gezi[][] = new int[33][33];
for(int m=4;m<30;m++)
for(int n=4;n<30;n++)
gezi[m][n]=0;
while(sign==0){
switch(label){
case 1:g.clearRect(350,60,100,20);
g.drawString("请红方落子...",300,60);
i=zx/20+2;
j=zy/20;
while(gezi[i][j]==0){
gezi[i][j]=1;
q.huaqizi(g,zx,zy,Color.red);
sign=x.panduan(i,j);
label=2;
}break;
case 2:g.clearRect(350,60,100,20);
g.drawString("请绿方落子...",300,60);
i=zx/20+2;
j=zy/20;
while(gezi[i][j]==0){
gezi[i][j]=1;
q.huaqizi(g,zx,zy,Color.green);
sign=x.panduan(i,j);
label=1;
}
}
}
//弹出游戏结束对话框
if(sign==1) {
JOptionPane.showMessageDialog(null,
"红方获胜!!!",
"Game Over !", JOptionPane.ERROR_MESSAGE);
}
else{
JOptionPane.showMessageDialog(null,
"绿方获胜!!!",
"Game Over !", JOptionPane.ERROR_MESSAGE);
}
//结束游戏
System.exit(0) ;
}
public void mousePressed(MouseEvent e){
zx=e.getX();
zy=e.getY();
}
public void mouseEntered(MouseEvent e){ }
public void mouseExited(MouseEvent e){ }
public void mouseClicked(MouseEvent e){ }
public void mouseReleased(MouseEvent e){ }
public void mouseMoved(MouseEvent e){ }
public void mouseDragged(MouseEvent e){ }
}
//画棋子
class Qizi{
int x;
int y;
Color z;
public void huaqizi(Graphics g,int x,int y,Color z){
g.setColor(z);
g.fillOval(x,y,14,14);
}
}
public class Test extends JFrame{
public static void main(String args[]){
Qipan q = new Qipan("欢迎使用bluebottle的五子棋(测试版)");
q.setVisible(true);
}
}
由于程序太长 ,我把Xiaqi类和里面的判断方法puanduan()省略了…… 展开
2个回答
展开全部
我也是初学者,这是我刚刚编写的五子棋源程序,希望对你有帮助,也请多多指教。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyFrame extends JFrame{
public static final int LEN = 15;
public static int time = 0;
JPanel panel = new JPanel();
public JButton [][] b = new JButton[LEN][LEN];
public static boolean flag =true;
public static int [] [] message=new int [LEN] [LEN];
public static Color[] [] color=new Color [LEN] [LEN];
public static String word="黑方执棋";
public void setButton(JButton[][] b){
for(int i=0;i<LEN;i++)
for(int j=0;j<LEN;j++){
b[i][j]=new JButton();
b[i][j].setBackground(color[i][j]);
b[i][j].addActionListener(new MyButtonListener(i,j));
}
}
public void addButton(JPanel panel,JButton[][] b){
for(int i=0;i<LEN;i++)
for(int j=0;j<LEN;j++){
panel.add(b[i][j]);
}
}
public static void resetColor(){
for(int i=0;i<LEN;i++)
for(int j=0;j<LEN;j++)
color[i][j]=Color.yellow;
}
public MyFrame(){
super("五子棋");
this.setLocation(130,0);
this.setSize(500,570);
panel.setLayout(new GridLayout(LEN,LEN));
setButton(b);
addButton(panel,b);
this.setVisible(true);
this.add(panel,BorderLayout.CENTER);
JPanel p1=new JPanel();
JLabel l1=new JLabel(word);
p1.add(l1);
this.add(p1,BorderLayout.NORTH);
JPanel p2=new JPanel();
JButton bb=new JButton("新游戏");
bb.addActionListener(new BbListener());
p2.add(bb);
this.add(p2,BorderLayout.SOUTH);
}
public static void resetMessage(){
for(int i=0;i<LEN;i++)
for(int j=0;j<LEN;j++)
message[i][j]=0;
}
}
class MyButtonListener implements ActionListener{
public static final int LEN=MyFrame.LEN;
int i,j;
public MyButtonListener(int i,int j){
this.i=i;
this.j=j;
}
public int whoWins(){
int COR=1;
int result = 0;
if(i>=4&&MyFrame.message[i][j]*MyFrame.message[i-1][j]*MyFrame.message[i-2][j]*MyFrame.message[i-3][j]*MyFrame.message[i-4][j]==COR||i<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i+1][j]*MyFrame.message[i+2][j]*MyFrame.message[i+3][j]*MyFrame.message[i+4][j]==COR||j>=4&&MyFrame.message[i][j]*MyFrame.message[i][j-1]*MyFrame.message[i][j-2]*MyFrame.message[i][j-3]*MyFrame.message[i][j-4]==COR||j<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i][j+1]*MyFrame.message[i][j+2]*MyFrame.message[i][j+3]*MyFrame.message[i][j+4]==COR||i>=4&&j>=4&&MyFrame.message[i][j]*MyFrame.message[i-1][j-1]*MyFrame.message[i-2][j-2]*MyFrame.message[i-3][j-3]*MyFrame.message[i-4][j-4]==COR||i>=4&&j<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i-1][j+1]*MyFrame.message[i-2][j+2]*MyFrame.message[i-3][j+3]*MyFrame.message[i-4][j+4]==COR||i<=(LEN-5)&&j<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i+1][j+1]*MyFrame.message[i+2][j+2]*MyFrame.message[i+3][j+3]*MyFrame.message[i+4][j+4]==COR||i<=(LEN-5)&&j>=4&&MyFrame.message[i][j]*MyFrame.message[i+1][j-1]*MyFrame.message[i+2][j-2]*MyFrame.message[i+3][j-3]*MyFrame.message[i+4][j-4]==COR)
result = 1;
COR=32;
if(i>=4&&MyFrame.message[i][j]*MyFrame.message[i-1][j]*MyFrame.message[i-2][j]*MyFrame.message[i-3][j]*MyFrame.message[i-4][j]==COR||i<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i+1][j]*MyFrame.message[i+2][j]*MyFrame.message[i+3][j]*MyFrame.message[i+4][j]==COR||j>=4&&MyFrame.message[i][j]*MyFrame.message[i][j-1]*MyFrame.message[i][j-2]*MyFrame.message[i][j-3]*MyFrame.message[i][j-4]==COR||j<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i][j+1]*MyFrame.message[i][j+2]*MyFrame.message[i][j+3]*MyFrame.message[i][j+4]==COR||i>=4&&j>=4&&MyFrame.message[i][j]*MyFrame.message[i-1][j-1]*MyFrame.message[i-2][j-2]*MyFrame.message[i-3][j-3]*MyFrame.message[i-4][j-4]==COR||i>=4&&j<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i-1][j+1]*MyFrame.message[i-2][j+2]*MyFrame.message[i-3][j+3]*MyFrame.message[i-4][j+4]==COR||i<=(LEN-5)&&j<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i+1][j+1]*MyFrame.message[i+2][j+2]*MyFrame.message[i+3][j+3]*MyFrame.message[i+4][j+4]==COR||i<=(LEN-5)&&j>=4&&MyFrame.message[i][j]*MyFrame.message[i+1][j-1]*MyFrame.message[i+2][j-2]*MyFrame.message[i+3][j-3]*MyFrame.message[i+4][j-4]==COR)
result = 2;
return result;
}
public void actionPerformed(ActionEvent e){
if(TestMyFrame.f.message[i][j]==0){
if(TestMyFrame.f.flag){
TestMyFrame.f.message[i][j]=1;
TestMyFrame.f.color[i][j]=Color.black;
TestMyFrame.f.word="白方执棋";
}
else{
TestMyFrame.f.message[i][j]=2;
TestMyFrame.f.color[i][j]=Color.white;
TestMyFrame.f.word="黑方执棋";
}
TestMyFrame.f.flag=!TestMyFrame.f.flag;
}
if(whoWins()==1)
{TestMyFrame.f.word="黑方胜";
int i1=0,j1=0;
for(i1=0;i1<LEN;i1++)for(j1=0;j1<LEN;j1++)TestMyFrame.f.message[i1][j1]=-1;}
if(whoWins()==2)
{TestMyFrame.f.word="白方胜";
int i1=0,j1=0;
for(i1=0;i1<LEN;i1++)for(j1=0;j1<LEN;j1++)TestMyFrame.f.message[i1][j1]=-1;}
TestMyFrame.f1=TestMyFrame.f;
TestMyFrame.f=new MyFrame();
TestMyFrame.f1.setVisible(false);
}
}
class TestMyFrame{
public static MyFrame f = new MyFrame();
public static MyFrame f1;
public static void main(String [] args){
f.resetColor();
f.resetMessage();
}
}
class BbListener implements ActionListener{
public void actionPerformed(ActionEvent e){
TestMyFrame.f.flag=true;
TestMyFrame.f.resetColor();
TestMyFrame.f.resetMessage();
MyFrame.word="黑方执棋";
TestMyFrame.f.setVisible(false);
TestMyFrame.f=new MyFrame();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyFrame extends JFrame{
public static final int LEN = 15;
public static int time = 0;
JPanel panel = new JPanel();
public JButton [][] b = new JButton[LEN][LEN];
public static boolean flag =true;
public static int [] [] message=new int [LEN] [LEN];
public static Color[] [] color=new Color [LEN] [LEN];
public static String word="黑方执棋";
public void setButton(JButton[][] b){
for(int i=0;i<LEN;i++)
for(int j=0;j<LEN;j++){
b[i][j]=new JButton();
b[i][j].setBackground(color[i][j]);
b[i][j].addActionListener(new MyButtonListener(i,j));
}
}
public void addButton(JPanel panel,JButton[][] b){
for(int i=0;i<LEN;i++)
for(int j=0;j<LEN;j++){
panel.add(b[i][j]);
}
}
public static void resetColor(){
for(int i=0;i<LEN;i++)
for(int j=0;j<LEN;j++)
color[i][j]=Color.yellow;
}
public MyFrame(){
super("五子棋");
this.setLocation(130,0);
this.setSize(500,570);
panel.setLayout(new GridLayout(LEN,LEN));
setButton(b);
addButton(panel,b);
this.setVisible(true);
this.add(panel,BorderLayout.CENTER);
JPanel p1=new JPanel();
JLabel l1=new JLabel(word);
p1.add(l1);
this.add(p1,BorderLayout.NORTH);
JPanel p2=new JPanel();
JButton bb=new JButton("新游戏");
bb.addActionListener(new BbListener());
p2.add(bb);
this.add(p2,BorderLayout.SOUTH);
}
public static void resetMessage(){
for(int i=0;i<LEN;i++)
for(int j=0;j<LEN;j++)
message[i][j]=0;
}
}
class MyButtonListener implements ActionListener{
public static final int LEN=MyFrame.LEN;
int i,j;
public MyButtonListener(int i,int j){
this.i=i;
this.j=j;
}
public int whoWins(){
int COR=1;
int result = 0;
if(i>=4&&MyFrame.message[i][j]*MyFrame.message[i-1][j]*MyFrame.message[i-2][j]*MyFrame.message[i-3][j]*MyFrame.message[i-4][j]==COR||i<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i+1][j]*MyFrame.message[i+2][j]*MyFrame.message[i+3][j]*MyFrame.message[i+4][j]==COR||j>=4&&MyFrame.message[i][j]*MyFrame.message[i][j-1]*MyFrame.message[i][j-2]*MyFrame.message[i][j-3]*MyFrame.message[i][j-4]==COR||j<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i][j+1]*MyFrame.message[i][j+2]*MyFrame.message[i][j+3]*MyFrame.message[i][j+4]==COR||i>=4&&j>=4&&MyFrame.message[i][j]*MyFrame.message[i-1][j-1]*MyFrame.message[i-2][j-2]*MyFrame.message[i-3][j-3]*MyFrame.message[i-4][j-4]==COR||i>=4&&j<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i-1][j+1]*MyFrame.message[i-2][j+2]*MyFrame.message[i-3][j+3]*MyFrame.message[i-4][j+4]==COR||i<=(LEN-5)&&j<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i+1][j+1]*MyFrame.message[i+2][j+2]*MyFrame.message[i+3][j+3]*MyFrame.message[i+4][j+4]==COR||i<=(LEN-5)&&j>=4&&MyFrame.message[i][j]*MyFrame.message[i+1][j-1]*MyFrame.message[i+2][j-2]*MyFrame.message[i+3][j-3]*MyFrame.message[i+4][j-4]==COR)
result = 1;
COR=32;
if(i>=4&&MyFrame.message[i][j]*MyFrame.message[i-1][j]*MyFrame.message[i-2][j]*MyFrame.message[i-3][j]*MyFrame.message[i-4][j]==COR||i<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i+1][j]*MyFrame.message[i+2][j]*MyFrame.message[i+3][j]*MyFrame.message[i+4][j]==COR||j>=4&&MyFrame.message[i][j]*MyFrame.message[i][j-1]*MyFrame.message[i][j-2]*MyFrame.message[i][j-3]*MyFrame.message[i][j-4]==COR||j<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i][j+1]*MyFrame.message[i][j+2]*MyFrame.message[i][j+3]*MyFrame.message[i][j+4]==COR||i>=4&&j>=4&&MyFrame.message[i][j]*MyFrame.message[i-1][j-1]*MyFrame.message[i-2][j-2]*MyFrame.message[i-3][j-3]*MyFrame.message[i-4][j-4]==COR||i>=4&&j<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i-1][j+1]*MyFrame.message[i-2][j+2]*MyFrame.message[i-3][j+3]*MyFrame.message[i-4][j+4]==COR||i<=(LEN-5)&&j<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i+1][j+1]*MyFrame.message[i+2][j+2]*MyFrame.message[i+3][j+3]*MyFrame.message[i+4][j+4]==COR||i<=(LEN-5)&&j>=4&&MyFrame.message[i][j]*MyFrame.message[i+1][j-1]*MyFrame.message[i+2][j-2]*MyFrame.message[i+3][j-3]*MyFrame.message[i+4][j-4]==COR)
result = 2;
return result;
}
public void actionPerformed(ActionEvent e){
if(TestMyFrame.f.message[i][j]==0){
if(TestMyFrame.f.flag){
TestMyFrame.f.message[i][j]=1;
TestMyFrame.f.color[i][j]=Color.black;
TestMyFrame.f.word="白方执棋";
}
else{
TestMyFrame.f.message[i][j]=2;
TestMyFrame.f.color[i][j]=Color.white;
TestMyFrame.f.word="黑方执棋";
}
TestMyFrame.f.flag=!TestMyFrame.f.flag;
}
if(whoWins()==1)
{TestMyFrame.f.word="黑方胜";
int i1=0,j1=0;
for(i1=0;i1<LEN;i1++)for(j1=0;j1<LEN;j1++)TestMyFrame.f.message[i1][j1]=-1;}
if(whoWins()==2)
{TestMyFrame.f.word="白方胜";
int i1=0,j1=0;
for(i1=0;i1<LEN;i1++)for(j1=0;j1<LEN;j1++)TestMyFrame.f.message[i1][j1]=-1;}
TestMyFrame.f1=TestMyFrame.f;
TestMyFrame.f=new MyFrame();
TestMyFrame.f1.setVisible(false);
}
}
class TestMyFrame{
public static MyFrame f = new MyFrame();
public static MyFrame f1;
public static void main(String [] args){
f.resetColor();
f.resetMessage();
}
}
class BbListener implements ActionListener{
public void actionPerformed(ActionEvent e){
TestMyFrame.f.flag=true;
TestMyFrame.f.resetColor();
TestMyFrame.f.resetMessage();
MyFrame.word="黑方执棋";
TestMyFrame.f.setVisible(false);
TestMyFrame.f=new MyFrame();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个你到网上下一个,看看别人的,你就会写了,主要的就是鼠标拖动问题
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询