⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainpanel.java

📁 用Java编写的一个五子棋小游戏代码! 有什么问题可以与我联系共同探讨!
💻 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{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private chessHead cHead;
	mainPanel(chessHead a){
		 setOpaque(true);
		 addMouseListener(this);	
		 addMouseMotionListener(this);
		 cHead=a;
	}
	public void draw(Graphics g,int i,int j){
		int x=i*20+20;
		int y=j*20+20;
	//	if(i!=20 && j!=30)
    //    {
           g.setColor(Color.black);
           g.drawRect(x,y,20,20);
   //     }
		if(chessHead.chessMap[i][j] == 1 )
        {
           g.setColor(Color.gray);
           g.drawOval(x-8,y-8,16,16);
           if(chessHead.blackPlayer.getColor().equals("Black"))
                g.setColor(Color.black);
           else if(chessHead.blackPlayer.getColor().equals("Blue"))
               g.setColor(Color.blue)     ;
           g.fillOval(x-8,y-8,16,16);
           //g.fillRect(x, y, 20, 20);
        }
      //draw the red chess
        if(chessHead.chessMap[i][j] == 2 )
        {
           g.setColor(Color.gray);
           g.drawOval(x-8,y-8,16,16);
           
           g.fillOval(x-8,y-8,16,16);
            if(chessHead.whitePlayer.getColor().equals("Red"))
                g.setColor(Color.red);
           else if(chessHead.whitePlayer.getColor().equals("White"))
               g.setColor(Color.white)     ;
           
           g.fillOval(x-8,y-8,16,16);
        }
	}
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		for(int i=0;i<=15;i++){
			for(int j=0;j<=12;j++){
				draw(g,i,j);
			}
		}
	}
	 public void restart(){
		for(int i=0;i<=20;i++)
			for(int j=0;j<=15;j++)
				chessHead.chessMap[i][j]=0;
		repaint();
	}
    public void mouseClicked(MouseEvent e)
    {
    	
	   //  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) {

		 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)
	        {
	            cHead.doingJob(x, y);
	            repaint();
	        }
	     //judge winner
	     	//up&down
	     while(chessHead.chessMap[ax][ay]==chessHead.chessMap[x][y]){
	    	 count++;
	    	 if(ax<20) ax++;
	     }
	     ax=x;count--;
	     while(chessHead.chessMap[ax][ay]==chessHead.chessMap[x][y]){
	    	 count++;
	    	 if(ax==0) break;
	    	 if(ax>0) ax--;
	    	 
	     }
	     if(count>4) 
	    	 {
	    	 JOptionPane.showMessageDialog( mainClass.thisGame, cHead.currenPlayer()+" are Winner!!","Win",1);
	    	 restart();
	    	 return;
	    	 }
	     
	     // left&right
	     ax=x;count=0;
	     while(chessHead.chessMap[ax][ay]==chessHead.chessMap[x][y]){
	    	 count++;
	    	 if(ay<15) ay++;
	     }
	     ay=y;count--;
	     while(chessHead.chessMap[ax][ay]==chessHead.chessMap[x][y]){
	    	 count++;
	    	 if(ay==0) break;
	    	 if(ay>0) ay--;
	     }
	     if(count>4) {
	    	 JOptionPane.showMessageDialog( mainClass.thisGame, cHead.currenPlayer()+" are Winner!!","Win",1);
	    	 restart();
	    	 return;
	     }
	     //northeast&&
	     ax=x;ay=y;count=0;
	     while(chessHead.chessMap[ax][ay]==chessHead.chessMap[x][y]){
	    	 count++;
	    	 if(ax==0||ay==0) break;
	    	 if(ay>0&ax>0) {
	    		 ay--;ax--;
	    	 }
	     }
	     ay=y;ax=x;;count--;
	     while(chessHead.chessMap[ax][ay]==chessHead.chessMap[x][y]){
	    	 count++;
	    	 if(ay<15&&ax<20) {
	    		 ay++;ax++;
	    	 } 
	     }
	     if(count>4) {
	    	 JOptionPane.showMessageDialog( mainClass.thisGame, cHead.currenPlayer()+" are Winner!!","Win",1);
	    	 restart();
	    	 return;
	     }
	     
	     ax=x;ay=y;count=0;
	     while(chessHead.chessMap[ax][ay]==chessHead.chessMap[x][y]){
	    	 count++;
	    	 if(ax==0) break;
	    	 if(ay<15&&ax>0) {
	    		 ay++;ax--;
	    	 }
	     }
	     ay=y;ax=x;count--;
	     while(chessHead.chessMap[ax][ay]==chessHead.chessMap[x][y]){
	    	 count++;
	    	 if(ay==0) break;
	    	 if(ay>0&&ax<20) {
	    		 ay--;ax++;
	    	 }
	     }
	     if(count>4) {
	    	 JOptionPane.showMessageDialog( mainClass.thisGame, cHead.currenPlayer()+" are Winner!!","Win",1);
	    	 restart();
	    	 return;
	     }
		// TODO Auto-generated method stub
//		 int x = (e.getX()-10) / 20;
//	     int y = (e.getY()-10) / 20;
//	     if (e.getModifiers()==MouseEvent.BUTTON1_MASK)
//	        {
//	            cHead.doingJob(x, y);
//	            repaint();
//	            System.out.println(cm.getisOdd()+" "+cm.getarrMapShow()[x][y]);
	           
//	            if(cHead.judgeSuccess(x,y,cm.getisOdd()))
//	            {
//	               cm.showSuccess(this);
//	               evt.consume();
//	               ChessFrame.iscomputer=false;
//	               //cf.restart();
//	            }
//	            if(ChessFrame.iscomputer&&!cm.getisExist())//
//	            {
//	               cm.computerdo(cm.getWidth(),cm.getHeight());
	               //evt.consume(); 
//	               repaint();
//	               if(cm.judgeSuccess(cm.getX(),cm.getY(),cm.getisOdd()))
//	               {
//	                   cm.showDefeat(this);
//	                   evt.consume();
//	                   //cf.restart();
//	               }
//	            }
//	        }
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -