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

📄 qlabel.java

📁 八皇后游戏
💻 JAVA
字号:
/*
 * Qlabel.java
 *
 * Created on 2008年3月24日, 下午10:55
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package javaapplication15;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


/**
 *
 * @author vivi
 */
public class Qlabel extends JLabel implements MouseListener{
    public DrawQueen dq;
    static public QueenFrame qf;
    public QueenFrame qf1;
    public int n1;
    public int row;
    public int col;
    public static int frestart;
    public static int amount;
     public static int score;
   
    /** Creates a new instance of Qlabel */
    public Qlabel() { 
        this.addMouseListener(this);
    }
  public Qlabel(DrawQueen dq1,QueenFrame qf2) {
      this.qf1=qf2;
      this.dq=dq1;
      this.n1=dq.n;
      this.addMouseListener(this);
     
  }
  
 public Qlabel(QueenFrame qfv,String txt) {
      qf=qfv;
      this.setText(txt);
  } 
  
    public void mouseClicked(MouseEvent e) {
        if (SwingUtilities.isLeftMouseButton(e))  {
            
         for(int i=0;i<this.n1;i++)
        {
            for(int j=0;j<this.n1;j++)
            {
         if(e.getSource()==this.dq.label[i][j]){
            this.row=i;
            this.col=j;
//
                   }
            }
       }
          if(qf.pause==0){
          if(!dq.chess[this.row][this.col]){
          if(allowedQueen(this.row,this.col)){
              dq.chess[this.row][this.col]=true;
                         this.setIcon(new ImageIcon("chongchong.png"));
                         this.setBackground(Color.GRAY);
               this.amount++;
               score=10*amount;                                       
               Integer scores=new Integer(score);
               qf.b3.setText(scores.toString());                      // 显示得分
               qf.b3.repaint();
              this.repaint();
              System.out.println(this.amount);
              System.out.println(qf.z);
              if(qf1.z==1){ShowDanger();}                              //显示危险境地
             if(Complete()&& qf1.invailable==0){                       //游戏完成
                 qf1.time.stop();
                  new JOptionPane();
                 JOptionPane.showMessageDialog(null,"you are win!","win",2);
                 Record record=new Record(qf1.gradeFile);
                 record.setTime(qf1.t);
                 record.setVisible(true);
                 frestart=1;
                 qf1.t=0;
                 qf1.pause=1;
                 qf1.b3.setText("你的得分");
                 qf1.b2.setText("time:");
                 qf1.restart();
                 }
              
             else if(Complete()&& qf1.invailable==1){                  //完成游戏但使用过提示功
                  qf1.time.stop();
               new JOptionPane();
                 JOptionPane.showMessageDialog(null,"您使用过相关提示功能,成绩不计入排行榜","warn",2);
                  qf1.t=0;
                  qf1.pause=1;
           }
              
          if(Dead()&&this.amount<this.n1&&frestart==0){
                  qf1.time.stop();   
                  new JOptionPane();
                 JOptionPane.showMessageDialog(null,"此局是死棋","warn",2);
                  qf1.t=0;
                  qf1.pause=1;
                  qf1.b3.setText("你的得分");
                  qf1.b2.setText("time:");
                  qf1.restart();
          }
          }
          
          else if(!allowedQueen(this.row,this.col)){
                ChongTu(this.row,this.col);
                }
          }
          
          else if(dq.chess[this.row][this.col]){
              new JOptionPane();
                 JOptionPane.showMessageDialog(null,"此处已有子","warn",1);   //在有棋子地方下棋
         }
          }
        }
     
         if(SwingUtilities.isRightMouseButton(e)){                   //右键悔棋
           if(qf.pause==0){
            if(dq.chess[this.row][this.col]){
               this.setIcon(null);
              this.amount--;
               score=10*amount;
               Integer scores=new Integer(score);
               qf.b3.setText(scores.toString());
               qf.b3.repaint();
              for(int i=0;i<this.n1;i++)
        {
            for(int j=0;j<this.n1;j++)
            {
//                 if((this.row+this.col)%2==0)
//                  this.setBackground(Color.black);
//                else
//                  this.setBackground(Color.white);  
                if((this.row+this.col)%2==0)
                this.setIcon(new ImageIcon("w6.jpg"));
                else 
                 this.setIcon(new ImageIcon("w4.jpg"));
            }
              }
          }
           dq.chess[this.row][this.col]=false;
                         if(qf.z==1){ShowDanger();}
        }   
    }
    }
   
      public boolean allowedQueen(int r,int c)                           //标记不可下棋区域
   {     
          boolean ok=true;
         for(int i=0;ok&&i<this.n1;i++)
             ok=!dq.chess[r][i];
         
         for(int i=0;ok&&i<this.n1;i++)
             ok=!dq.chess[i][c];
         
         for(int i=1;ok&&r-i>=0&&c-i>=0;i++)
              ok=!dq.chess[r-i][c-i];
         
         for(int i=1;ok&&r+i<this.n1&&c+i<this.n1;i++)
              ok=!dq.chess[r+i][c+i];
         
         for(int i=1;ok&&r-i>=0&&c+i<this.n1;i++)
              ok=!dq.chess[r-i][c+i];
        
          for(int i=1;ok&&r+i<this.n1&&c-i>=0;i++)
              ok=!dq.chess[r+i][c-i];
         
         return ok;
  }
      public boolean Complete(){                                    //判断是否完成游戏
          boolean finish=false;
          if(this.amount==this.n1){
              finish=true;
              }
          return finish;
              }
      public boolean Dead(){                                      //判断是否是死棋
          int d=0;
          boolean dead=false;
           for(int i=0;i<this.n1;i++)
        {
            for(int j=0;j<this.n1;j++)
            {
                if(dq.chess[i][j]||!allowedQueen(i,j)){
                d++;
                }
                }
            }
          if(d==this.n1*this.n1) dead=true;
          return dead;
      }
      public void ShowDanger(){                                   //显示危险境地
       for(int i=0;i<this.n1;i++)
        {
            for(int j=0;j<this.n1;j++)
            {
                if(!dq.chess[i][j]){
          if(!allowedQueen(i,j)){

                if((i+j)%2==0)
                this.dq.label[i][j].setIcon(new ImageIcon("w6.danger.png"));
                else 
                 this.dq.label[i][j].setIcon(new ImageIcon("w4.danger.png"));
                 }
                else if(allowedQueen(i,j)){
              if((i+j)%2==0)
                this.dq.label[i][j].setIcon(new ImageIcon("w6.jpg"));
                else 
                 this.dq.label[i][j].setIcon(new ImageIcon("w4.jpg"));
                  }
                }
             }
         }
     }
      public  void Canceldamger(){                              //取消显示危险境地
         for(int i=0;i<this.n1;i++)
        {
            for(int j=0;j<this.n1;j++)
            {
                if(!dq.chess[i][j]){
          if(!allowedQueen(i,j)){
              if((i+j)%2==0)
                this.dq.label[i][j].setIcon(new ImageIcon("w6.jpg"));
                else 
                 this.dq.label[i][j].setIcon(new ImageIcon("w4.jpg"));
                 }
                }
            }
        }
      }
     
      public void ChongTu(int r,int c){                     //提示下棋冲突                   
            for(int i=0;i<this.n1;i++)
             if(this.dq.chess[r][i]){
                 this.dq.label[r][i].setBackground(Color.PINK);
         JOptionPane.showMessageDialog(null,"行冲突了!","remind",1);
             }
         for(int i=0;i<this.n1;i++)
             if(this.dq.chess[i][c]){
                   this.dq.label[i][c].setBackground(Color.PINK);
          JOptionPane.showMessageDialog(null,"列冲突了!","remind",1);}

         for(int i=1;r-i>=0&&c-i>=0;i++)
              if(this.dq.chess[r-i][c-i]){
                    this.dq.label[r-i][c-i].setBackground(Color.PINK);
      JOptionPane.showMessageDialog(null,"与左上冲突了!","remind",1);}
         
          for(int i=1;r+i<this.n1&&c+i<this.n1;i++)
               if(this.dq.chess[r+i][c+i]){
                     this.dq.label[r+i][c+i].setBackground(Color.PINK);
            JOptionPane.showMessageDialog(null,"与右下冲突了!","remind",1);}     
 
           for(int i=1;r-i>=0&&c+i<this.n1;i++)
           if(this.dq.chess[r-i][c+i]){
                 this.dq.label[r-i][c+i].setBackground(Color.PINK);
          JOptionPane.showMessageDialog(null,"与右上冲突了!","remind",1); }    
        
          for(int i=1;r+i<this.n1&&c-i>=0;i++)
              if(this.dq.chess[r+i][c-i]){
                    this.dq.label[r+i][c-i].setBackground(Color.PINK);
        JOptionPane.showMessageDialog(null,"与左下冲突了!","remind",1);     
         }
     }
    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {                    //提示可下棋区域
        if(qf1.pause==0&&qf1.remind==1){
        for(int i=0;i<this.n1;i++)
        {
            for(int j=0;j<this.n1;j++)
            {
         if(e.getSource()==this.dq.label[i][j]){
            this.row=i;
            this.col=j;}}}
        for(int i=0;i<this.n1;i++)
        {
            for(int j=0;j<this.n1;j++)
            {     
                 if(!dq.chess[this.row][this.col]&&allowedQueen(this.row,this.col)){
                 if((this.row+this.col)%2==0)
                this.setIcon(new ImageIcon("w61.jpg"));
                else 
                 this.setIcon(new ImageIcon("w41.jpg"));
                 this.repaint();
                }
                }
            }
    }
    }
   

    public void mouseExited(MouseEvent e) {
      if(qf1.pause==0&&qf1.remind==1){
       for(int i=0;i<this.n1;i++)
        {
            for(int j=0;j<this.n1;j++)
            {
         if(e.getSource()==this.dq.label[i][j]){
            this.row=i;
            this.col=j;}}}
        for(int i=0;i<this.n1;i++)
        {
            for(int j=0;j<this.n1;j++)
            {    
                 if(!dq.chess[this.row][this.col]&&allowedQueen(this.row,this.col)){
                 if((this.row+this.col)%2==0)
                this.setIcon(new ImageIcon("w6.jpg"));
                else 
                 this.setIcon(new ImageIcon("w4.jpg"));
                 this.repaint();
                }
           }
    }
}
}
}

⌨️ 快捷键说明

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