java版數(shù)獨游戲界面實現(xiàn)(二)

本文實例為大家分享了java版數(shù)獨游戲界面實現(xiàn)的具體代碼,供大家參考,具體內(nèi)容如下

成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供西平企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、成都網(wǎng)站建設(shè)、H5響應(yīng)式網(wǎng)站、小程序制作等業(yè)務(wù)。10年已為西平眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。

實現(xiàn)效果圖:

這里寫圖片描述

主函數(shù)用于啟動程序:

package hlc.shudu.app;

import hlc.shudu.src.ShuduHelper;
import hlc.shudu.ui.ShuduMainFrame;

public class AppStart {

  public static void main(String[] args) {
    ShuduMainFrame mainFrame = new ShuduMainFrame();
    mainFrame.setVisible(true);

  }
}

主窗體類(包含消息區(qū),時間區(qū),游戲區(qū)):

package hlc.shudu.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Time;
import java.text.SimpleDateFormat;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.border.TitledBorder;

/*
 * 數(shù)獨主窗體
 */
public class ShuduMainFrame extends JFrame {

  public static int pass = 1; // 關(guān)卡
  public static JLabel lbPass; // 顯示關(guān)卡的lable
  public static long usedTime = 0; // 玩家用時
  private ShuduCanvers panelCanvers; // 主游戲區(qū)
  public static Timer userTimeAction;

  /*
   * 默認(rèn)構(gòu)造函數(shù)
   */
  public ShuduMainFrame() {
    // 初始化方法
    init();
    // 添加組件
    addComponent();
    // 添加主游戲區(qū)
    addCanvers();

  }

  /*
   * 添加主游戲區(qū)
   */
  private void addCanvers() {
    panelCanvers = new ShuduCanvers();
    panelCanvers.setBorder(new TitledBorder("游戲區(qū)"));

    // 將主游戲區(qū)添加到窗體中
    this.add(panelCanvers, BorderLayout.CENTER);

  }

  /*
   * 添加組件區(qū)
   */
  private void addComponent() {
    JPanel panelComponent = new JPanel();
    // 添加消息區(qū)
    addPanelMsg(panelComponent);
    // 添加時間區(qū)
    addPanelTime(panelComponent);

    // 將組件添加到窗體頂部
    this.add(panelComponent, BorderLayout.NORTH);

  }

  private void addPanelTime(JPanel panelComponent) {
    JPanel panelTime = new JPanel();
    panelTime.setBorder(new TitledBorder("時間"));
    panelTime.setLayout(new GridLayout(2, 1));

    final JLabel lbSysTime = new JLabel();
    final JLabel lbUserTime = new JLabel();

    panelTime.add(lbSysTime, BorderLayout.NORTH);
    panelTime.add(lbUserTime, BorderLayout.SOUTH);

    // 設(shè)置系統(tǒng)時間定時器
    Timer sysTimeAction = new Timer(500, new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        long timeMillis = System.currentTimeMillis();
        SimpleDateFormat df = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");
        lbSysTime.setText("  系統(tǒng)時間: " + df.format(timeMillis));
      }
    });
    sysTimeAction.start();
    userTimeAction = new Timer(1000, new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        lbUserTime.setText("  您已用時: " + (++usedTime)+ " sec.");
      }
    });
    userTimeAction.start();

    panelComponent.add(panelTime, BorderLayout.EAST);

  }

  /*
   * 添加消息區(qū)
   */
  private void addPanelMsg(JPanel panelComponent) {
    // panelComponent.setBorder(new TitledBorder("消息區(qū)"));
    panelComponent.setLayout(new GridLayout(1, 3));
    Font font14 = new Font("", 4, 14);
    Font font28 = new Font("", 2, 28);

    JPanel panelMsg = new JPanel();
    panelMsg.setBorder(new TitledBorder("消息區(qū)"));

    JLabel lbPass1 = new JLabel("關(guān)卡:第");
    lbPass1.setFont(font14);
    panelMsg.add(lbPass1);

    // 顯示關(guān)卡數(shù)
    lbPass = new JLabel("" + pass);
    lbPass.setForeground(Color.RED);
    lbPass.setFont(font28);
    panelMsg.add(lbPass);

    JLabel lbPass2 = new JLabel("關(guān)/總共10關(guān)");
    lbPass2.setFont(font14);
    panelMsg.add(lbPass2);
    panelComponent.add(panelMsg, BorderLayout.CENTER);

  }

  /*
   * 界面初始化
   */
  private void init() {
    ImageIcon image = new ImageIcon("icon/icon.png");
    this.setIconImage(image.getImage());
    // 設(shè)置窗口初始大小
    this.setSize(515, 600);
    // 設(shè)置窗口初始位置
    this.setLocation(500, 50);
    // 設(shè)置窗口標(biāo)題
    this.setTitle("數(shù)獨游戲(By:侯龍超)");
    // 設(shè)置窗體不允許改變大小
    this.setResizable(false);
    // 設(shè)置默認(rèn)關(guān)閉操作
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

游戲區(qū)畫布:

package hlc.shudu.ui;

import hlc.shudu.src.ShuduHelper;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog.ModalExclusionType;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

public class ShuduCanvers extends JPanel implements MouseListener {
  ShuduCell[][] cells;
  // 得到數(shù)獨數(shù)組
  int[][] maps = new int[9][9];
  private SelectNumFrame selectNum;

  /*
   * 默認(rèn)構(gòu)造函數(shù)
   */
  public ShuduCanvers() {
    ShuduMainFrame.usedTime = 0;
    maps = ShuduHelper.getMap();
    // 加載數(shù)獨區(qū)
    this.setLayout(null);
    cells = new ShuduCell[9][9];
    for (int i = 0; i < 9; i++) {
      for (int j = 0; j < 9; j++) {
        // this.remove(cells[i][j]);
        // 創(chuàng)建單元格
        cells[i][j] = new ShuduCell();
        // 設(shè)置位置
        cells[i][j].setLocation(20 + i * 50 + (i / 3) * 5, 20 + j * 50
            + (j / 3) * 5);
        if (passRole(ShuduMainFrame.pass)) {
          cells[i][j].setText("" + maps[i][j]);
          // 設(shè)置背景顏色
          cells[i][j].setBackground(getColor(maps[i][j]));
          cells[i][j].setEnabled(false);
          cells[i][j].setForeground(Color.gray);
        } else {
          cells[i][j].addMouseListener(this);
        }
        this.add(cells[i][j]);
      }
    }
    checkFinish();

    // reLoadCanvers();
  }

  /*
   * 檢查是否完成
   */
  private void checkFinish() {
    for (int i = 0; i < 9; i++) {
      for (int j = 0; j < 9; j++) {
          if (!check(i, j)) {
            return;
          }
        }
      }

    // 停止用戶用時計時器
    ShuduMainFrame.userTimeAction.stop();
    // 清除所有cell監(jiān)聽
    clearAllListener();
    // 闖關(guān)數(shù)加一
    ShuduMainFrame.pass += 1;
    if (ShuduMainFrame.pass > 10) {
      int o = JOptionPane
          .showConfirmDialog(this, "您已經(jīng)通關(guān)了,是否重頭開始?", "", 0);
      if (o == 1) {
        System.exit(0);
      } else {
        ShuduMainFrame.pass = 1;
      }
    } else {
      JOptionPane.showMessageDialog(this, "恭喜你通過本關(guān)!用時:"
          + ShuduMainFrame.usedTime + "秒\n即將進(jìn)入下一關(guān)!");
    }
    // 更新關(guān)卡提示
    ShuduMainFrame.lbPass.setText("" + ShuduMainFrame.pass);
    // 開始新的關(guān)卡
    reLoadCanvers();
    // 打開用戶用時計時器
    ShuduMainFrame.userTimeAction.start();

  }

  /*
   * 檢查指定坐標(biāo)處的單元格
   */

  private boolean check(int i, int j) {
    if (cells[i][j].getText().isEmpty()) {
      return false;
    }

    for (int k = 0; k < 9; k++) {
      if (cells[i][j].getText().trim().equals(cells[i][k].getText().trim()) && j!=k) {
        return false;
      }
      if (cells[i][j].getText().trim().equals(cells[k][j].getText().trim()) && i != k) {
        return false;
      }
      int ii = (i / 3) * 3 + k / 3;
      int jj = (j / 3) * 3 + k % 3;
      if (cells[i][j].getText().trim().equals(cells[ii][jj].getText().trim()) &&!(i == ii && j == jj)) {
        return false;
      }

    }
    return true;
  }

  /*
   * 重新加載數(shù)獨區(qū)
   */
  public void reLoadCanvers() {
    ShuduMainFrame.usedTime = 0;
    maps = ShuduHelper.getMap();
    for (int i = 0; i < 9; i++) {
      for (int j = 0; j < 9; j++) {
        this.remove(cells[i][j]);
        // 創(chuàng)建單元格
        cells[i][j] = new ShuduCell();
        // 設(shè)置位置
        cells[i][j].setLocation(20 + i * 50 + (i / 3) * 5, 20 + j * 50
            + (j / 3) * 5);
        if (passRole(ShuduMainFrame.pass)) {
          cells[i][j].setText("" + maps[i][j]);
          // 設(shè)置背景顏色
          cells[i][j].setBackground(getColor(maps[i][j]));
          cells[i][j].setEnabled(false);
          cells[i][j].setForeground(Color.gray);
        } else {
          cells[i][j].addMouseListener(this);
        }
        this.add(cells[i][j]);
      }
    }
    this.repaint();
    checkFinish();

  }

  /*
   * 根據(jù)關(guān)卡隨機(jī)產(chǎn)生該位置是否顯示數(shù)字
   */
  private boolean passRole(int pass) {
    // TODO Auto-generated method stub
    return Math.random() * 11 > pass;
  }

  /*
   * 根據(jù)數(shù)字獲得顏色
   */
  private Color getColor(int i) {
    Color color = Color.pink;
    switch (i) {
    case 1:
      color = new Color(255, 255, 204);
      break;
    case 2:
      color = new Color(204, 255, 255);
      break;
    case 3:
      color = new Color(255, 204, 204);
      break;
    case 4:
      color = new Color(255, 204, 153);
      break;
    case 5:
      color = new Color(204, 255, 153);
      break;
    case 6:
      color = new Color(204, 204, 204);
      break;
    case 7:
      color = new Color(255, 204, 204);
      break;
    case 8:
      color = new Color(255, 255, 255);
      break;
    case 9:
      color = new Color(153, 255, 153);
      break;
    default:
      break;
    }
    return color;
  }

  @Override
  public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub

  }

  @Override
  public void mousePressed(MouseEvent e) {
    int modes = e.getModifiers();
    if ((modes & InputEvent.BUTTON3_MASK) != 0) {// 點擊鼠標(biāo)右鍵
      // 清空點擊單元格上的內(nèi)容
      ((ShuduCell) e.getSource()).setText("");
    } else if ((modes & InputEvent.BUTTON1_MASK) != 0) {// 點擊鼠標(biāo)左鍵
      // 如果選擇數(shù)字窗口存在則銷毀
      if (selectNum != null) {
        selectNum.dispose();
      }
      // 新建一個選擇窗口
      selectNum = new SelectNumFrame();
      // 設(shè)置成模態(tài)窗口
      selectNum.setModal(true);
      // 設(shè)置選擇窗口在顯示器上的位置
      selectNum.setLocation(e.getLocationOnScreen().x,
          e.getLocationOnScreen().y);
      // 將點擊的單元格傳遞給數(shù)字選擇窗口
      selectNum.setCell((ShuduCell) e.getSource());
      // 顯示數(shù)字選擇窗口
      selectNum.setVisible(true);
    }
    checkFinish();
  }

  /*
   * 清除所有cell的點擊監(jiān)聽
   */
  private void clearAllListener() {
    for (int i = 0; i < 9; i++) {
      for (int j = 0; j < 9; j++) {
        cells[i][j].removeMouseListener(this);
      }
    }

  }

  @Override
  public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

  }

  @Override
  public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

  }

  @Override
  public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

  }

}

數(shù)獨單元格:

package hlc.shudu.ui;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JButton;

public class ShuduCell extends JButton {
 public ShuduCell(){
  this.setSize(50,50);
  Font font = new Font("",2,24);
  this.setFont(font);
  this.setBackground(new Color(255,153,102));
  this.setForeground(Color.BLUE);
 }
}

數(shù)字選擇框:

package hlc.shudu.ui;

import java.awt.Color;
import java.awt.Window;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class SelectNumFrame extends JDialog implements MouseListener {
 private ShuduCell cell;

 public void setCell(ShuduCell cell) {
  this.cell = cell;
 }

 public SelectNumFrame(){
  //隱藏界面上面的工具欄
  this.setUndecorated(true);
  this.setSize(150, 150);
  this.setBackground(new Color(255,204,153, 123));
  this.setLayout(null);
  addNum();
 }
 //添加數(shù)字1~9
 private void addNum() {
  for (int i = 0; i < 3; i++) {
   for (int j = 0; j < 3; j++) {
    JButton btn = new JButton();
    btn.setSize(50, 50);
    btn.setLocation(i*50,j*50);
    btn.setText(""+(j*3+i+1));
    btn.addMouseListener(this);
    this.add(btn);
   }
  }

 }

 @Override
 public void mouseClicked(MouseEvent e) {
  // TODO Auto-generated method stub

 }

 @Override
 public void mousePressed(MouseEvent e) {
  int modes = e.getModifiers();
  if ((modes & InputEvent.BUTTON1_MASK) != 0) {
   JButton btn = (JButton) e.getSource();
   cell.setText(btn.getText());
  }
  this.dispose();
 }

 @Override
 public void mouseReleased(MouseEvent e) {
  // TODO Auto-generated method stub

 }

 @Override
 public void mouseEntered(MouseEvent e) {
  // TODO Auto-generated method stub

 }

 @Override
 public void mouseExited(MouseEvent e) {
  // TODO Auto-generated method stub

 }


}

完整程序包可在GitHub上下載:https://github.com/houlongchao/shudu.git

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

分享名稱:java版數(shù)獨游戲界面實現(xiàn)(二)
鏈接URL:http://bm7419.com/article10/pcoddo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、商城網(wǎng)站、電子商務(wù)、定制開發(fā)、網(wǎng)站維護(hù)企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營