import java.awt.Color;
import javax.swing.*;
public class Chess extends JPanel
{//继承面板类
public Chess(int grids,int gridsize)
{//grids:行数和列数, gridsize:单元格的高和宽
super(null);
for(int i=0; igrids; i++)
{
for(int j=0; jgrids; j++)
JLabel l = new JLabel();//生成标签实例
l.setSize(gridsize,gridsize);
l.setLocation(i*gridsize,j*gridsize);
{ //当小方格的坐标和刚好是偶数时,
l.setBackground(Color.black); //设置为方格为黑色
l.setOpaque(true); //设置为不透明
}
l.setBorder(BorderFactory.createLineBorder(Color.black)); //设置边界为黑色
add(l);//将l标签添加到面板
public static void main(String[] args)
JFrame f = new JFrame();
f.setLocationRelativeTo(null); //设置窗口相对于指定组件的位置
f.setVisible(true);
车的规则就是 目标点 不能超过前面的第一个人.
炮的规则就是 目标点 必须超过前面的第一个 不超过前面的第二个人.
X X X
X X
-----
------------------------------
---------------------------
public int go(int x,int y,int path,int len){
if(loaction[x][y]==1) {//是帅
if(path==1){//向左
if(x=0) return -1;//返回-1代表走发错误
else {loaction[x-1][y]=1;loaction[x][y]=-1;}//将左一个格子标记为帅
...
}//else path
}//if key
....
车就差不多了
我并不懂编程,但是还是希望以下答案对你有帮助!
package com.plague.math;
import java.lang.Math;
public class Chess {
private int total;
public Chess () {
System.out.println("国王的赏赐,其实,问题是这样的:" +
for (int i = 0; i CHESS_NUMBER; i++) {
System.out.println("国王的赏赐是:"+total);
public static void main(String[] args) {
Chess c1 = new Chess();
这个程序实现还要一个关联文件夹"image"象棋棋子图片 部分代码如下:(代码太长 给个邮箱 我发给你)
import java.awt.*;
import java.awt.event.*;
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 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 黑棋走棋
//控制棋子闪烁的线程
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.add(anew);
jmain.add(repent);
jmain.add(exit);
jmain.add(text);
con.add(jmain);
//添加棋子标签
drawChessMan();
//注册按扭监听
anew.addActionListener(this);
repent.addActionListener(this);
exit.addActionListener(this);
//注册棋子移动监听
con.add(play[i]);
play[i].addMouseListener(this);
//添加棋盘标签
con.add(image = new JLabel(new ImageIcon("image\\Main.GIF")));
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.setIconImage(new ImageIcon("image\\红将.GIF").getImage());
this.setResizable(false);
this.setTitle(Title);
this.show();
** 添加棋子方法
public void drawChessMan(){
//流程控制
int i,k;
//图标
Icon in;
//黑色棋子
//车
in = new ImageIcon("image\\黑车.GIF");
play[i] = new JLabel(in);
play[i].setName("车1");
//马
in = new ImageIcon("image\\黑马.GIF");
play[i].setName("马1");
//相
in = new ImageIcon("image\\黑象.GIF");
play[i].setName("象1");
//士
in = new ImageIcon("image\\黑士.GIF");
play[i].setName("士1");
//卒
in = new ImageIcon("image\\黑卒.GIF");
play[i].setName("卒1" + i);
//炮
in = new ImageIcon("image\\黑炮.GIF");
play[i].setName("炮1" + i);
//将
in = new ImageIcon("image\\黑将.GIF");
//红色棋子
in = new ImageIcon("image\\红车.GIF");
in = new ImageIcon("image\\红马.GIF");
in = new ImageIcon("image\\红象.GIF");
in = new ImageIcon("image\\红士.GIF");
//兵
in = new ImageIcon("image\\红卒.GIF");
in = new ImageIcon("image\\红炮.GIF");
//帅
in = new ImageIcon("image\\红将.GIF");
** 线程方法控制棋子闪烁
以上就是土嘎嘎小编为大家整理的java象棋规则代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!