當前位置:編程學習大全網 - 編程語言 - 求java 象棋小程序 壹小段代碼

求java 象棋小程序 壹小段代碼

這個程序實現還要壹個關聯文件夾“image”象棋棋子圖片 部分代碼如下:(代碼太長 給個郵箱 我發給妳)

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

import java.io.*;

//主類

public class Chess{

public static void main(String args[]){

new ChessMainFrame("中國象棋:觀棋不語真君子,棋死無悔大丈夫");

}

}

//主框架類

class ChessMainFrame extends JFrame implements ActionListener,MouseListener,Runnable{

//玩家

JLabel play[] = new JLabel[32];

//棋盤

JLabel image;

//窗格

Container con;

//工具欄

JToolBar jmain;

//重新開始

JButton anew;

//悔棋

JButton repent;

//退出

JButton exit;

//當前信息

JLabel text;

//保存當前操作

Vector Var;

//規則類對象(使於調用方法)

ChessRule rule;

/**

** 單擊棋子

** chessManClick = true 閃爍棋子 並給線程響應

** chessManClick = false 吃棋子 停止閃爍 並給線程響應

*/

boolean chessManClick;

/**

** 控制玩家走棋

** chessPlayClick=1 黑棋走棋

** chessPlayClick=2 紅棋走棋 默認紅棋

** chessPlayClick=3 雙方都不能走棋

*/

int chessPlayClick=2;

//控制棋子閃爍的線程

Thread tmain;

//把第壹次的單擊棋子給線程響應

static int Man,i;

ChessMainFrame(){

new ChessMainFrame("中國象棋");

}

/**

** 構造函數

** 初始化圖形用戶界面

*/

ChessMainFrame(String Title){

//獲行客格引用

con = this.getContentPane();

con.setLayout(null);

//實例化規則類

rule = new ChessRule();

Var = new Vector();

//創建工具欄

jmain = new JToolBar();

text = new JLabel("歡迎使用象棋對弈系統");

//當鼠標放上顯示信息

text.setToolTipText("信息提示");

anew = new JButton(" 新 遊 戲 ");

anew.setToolTipText("重新開始新的壹局");

exit = new JButton(" 退 出 ");

exit.setToolTipText("退出象棋程序程序");

repent = new JButton(" 悔 棋 ");

repent.setToolTipText("返回到上次走棋的位置");

//把組件添加到工具欄

jmain.setLayout(new GridLayout(0,4));

jmain.add(anew);

jmain.add(repent);

jmain.add(exit);

jmain.add(text);

jmain.setBounds(0,0,558,30);

con.add(jmain);

//添加棋子標簽

drawChessMan();

//註冊按扭監聽

anew.addActionListener(this);

repent.addActionListener(this);

exit.addActionListener(this);

//註冊棋子移動監聽

for (int i=0;i<32;i++){

con.add(play[i]);

play[i].addMouseListener(this);

}

//添加棋盤標簽

con.add(image = new JLabel(new ImageIcon("image\\Main.GIF")));

image.setBounds(0,30,558,620);

image.addMouseListener(this);

//註冊窗體關閉監聽

this.addWindowListener(

new WindowAdapter() {

public void windowClosing(WindowEvent we){

System.exit(0);

}

}

);

//窗體居中

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

Dimension frameSize = this.getSize();

if (frameSize.height > screenSize.height){

frameSize.height = screenSize.height;

}

if (frameSize.width > screenSize.width){

frameSize.width = screenSize.width;

}

this.setLocation((screenSize.width - frameSize.width) / 2 - 280 ,(screenSize.height - frameSize.height ) / 2 - 350);

//設置

this.setIconImage(new ImageIcon("image\\紅將.GIF").getImage());

this.setResizable(false);

this.setTitle(Title);

this.setSize(558,670);

this.show();

}

/**

** 添加棋子方法

*/

public void drawChessMan(){

//流程控制

int i,k;

//圖標

Icon in;

//黑色棋子

//車

in = new ImageIcon("image\\黑車.GIF");

for (i=0,k=24;i<2;i++,k+=456){

play[i] = new JLabel(in);

play[i].setBounds(k,56,55,55);

play[i].setName("車1");

}

//馬

in = new ImageIcon("image\\黑馬.GIF");

for (i=4,k=81;i<6;i++,k+=342){

play[i] = new JLabel(in);

play[i].setBounds(k,56,55,55);

play[i].setName("馬1");

}

//相

in = new ImageIcon("image\\黑象.GIF");

for (i=8,k=138;i<10;i++,k+=228){

play[i] = new JLabel(in);

play[i].setBounds(k,56,55,55);

play[i].setName("象1");

}

//士

in = new ImageIcon("image\\黑士.GIF");

for (i=12,k=195;i<14;i++,k+=114){

play[i] = new JLabel(in);

play[i].setBounds(k,56,55,55);

play[i].setName("士1");

}

//卒

in = new ImageIcon("image\\黑卒.GIF");

for (i=16,k=24;i<21;i++,k+=114){

play[i] = new JLabel(in);

play[i].setBounds(k,227,55,55);

play[i].setName("卒1" + i);

}

//炮

in = new ImageIcon("image\\黑炮.GIF");

for (i=26,k=81;i<28;i++,k+=342){

play[i] = new JLabel(in);

play[i].setBounds(k,170,55,55);

play[i].setName("炮1" + i);

}

//將

in = new ImageIcon("image\\黑將.GIF");

play[30] = new JLabel(in);

play[30].setBounds(252,56,55,55);

play[30].setName("將1");

//紅色棋子

//車

in = new ImageIcon("image\\紅車.GIF");

for (i=2,k=24;i<4;i++,k+=456){

play[i] = new JLabel(in);

play[i].setBounds(k,569,55,55);

play[i].setName("車2");

}

//馬

in = new ImageIcon("image\\紅馬.GIF");

for (i=6,k=81;i<8;i++,k+=342){

play[i] = new JLabel(in);

play[i].setBounds(k,569,55,55);

play[i].setName("馬2");

}

//相

in = new ImageIcon("image\\紅象.GIF");

for (i=10,k=138;i<12;i++,k+=228){

play[i] = new JLabel(in);

play[i].setBounds(k,569,55,55);

play[i].setName("象2");

}

//士

in = new ImageIcon("image\\紅士.GIF");

for (i=14,k=195;i<16;i++,k+=114){

play[i] = new JLabel(in);

play[i].setBounds(k,569,55,55);

play[i].setName("士2");

}

//兵

in = new ImageIcon("image\\紅卒.GIF");

for (i=21,k=24;i<26;i++,k+=114){

play[i] = new JLabel(in);

play[i].setBounds(k,398,55,55);

play[i].setName("卒2" + i);

}

//炮

in = new ImageIcon("image\\紅炮.GIF");

for (i=28,k=81;i<30;i++,k+=342){

play[i] = new JLabel(in);

play[i].setBounds(k,455,55,55);

play[i].setName("炮2" + i);

}

//帥

in = new ImageIcon("image\\紅將.GIF");

play[31] = new JLabel(in);

play[31].setBounds(252,569,55,55);

play[31].setName("帥2");

}

/**

** 線程方法控制棋子閃爍

*/

}

  • 上一篇:自動編程過程包括
  • 下一篇:異形石有哪些種類?
  • copyright 2024編程學習大全網