Graphics 象棋
来源:互联网 发布:网络密钥是什么意思 编辑:程序博客网 时间:2024/04/29 01:40
package main;
import javaclass2.MyFrame;
public class Mymain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
}
package javaclass2;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
public class ChessLine {
private final int lineRows=10;
private final int lineCols=9;
public int getLineRows() {
return lineRows;
}
public int getLineCols() {
return lineCols;
}
/**
* 棋盘离左边界的距离
*/
private int marginleft=40;
public int getMarginleft() {
return marginleft;
}
public void setMarginleft(int marginleft) {
this.marginleft = marginleft;
}
/**
* 棋盘离上边界的距离
*/
private int margintop=40;
public int getMargintop() {
return margintop;
}
public void setMargintop(int margintop) {
this.margintop = margintop;
}
/**
* 卒位十字标的单线长度
*/
private int linemark=6;
public int getLinemark() {
return linemark;
}
public void setLinemark(int linemark) {
this.linemark = linemark;
}
/**
* 画出炮敦的方法
* @param g 画图类Graphics
* @param x 炮敦的中心横坐标X
* @param y 炮敦的中心纵坐标Y
*/
private void DrawMarkleft(Graphics g,int x,int y){
/**
*画出十字标的左边部分
*/
g.drawLine(x+linemark+2, y-2, x+2, y-2);
g.drawLine(x+linemark+2, y+2, x+2, y+2);
g.drawLine(x+2,y-linemark-2,x+2,y-2);
g.drawLine(x+2,y+linemark+2,x+2,y+2);
}
private void DrawMarkright(Graphics g,int x,int y){
/**
* 画出十字标的右边部分
*/
g.drawLine(x-linemark-2, y-2, x-2, y-2);
g.drawLine(x-linemark-2, y+2, x-2, y+2);
g.drawLine(x-2,y-linemark-2,x-2,y-2);
g.drawLine(x-2,y+linemark+2,x-2,y+2);
}
/**
* 画出十字标
*/
private void DrawMark(Graphics g,int x,int y){
DrawMarkleft(g,x,y);
DrawMarkright(g,x,y);
}
/**
* 标记方形格子的宽和高的变量
*/
private int Tablewidth,Tableheight;
/**
* 画出棋盘的方法
* @param g 画图的类Graphics
* @param PanWidth 棋盘的宽度
* @param PanHeight 棋盘的高度
*/
public void Drawline(Graphics g,int PanWidth,int PanHeight){
/**
* 得到方形格子的宽和高
*/
Tablewidth=(PanWidth-2*marginleft)/(lineCols-1);
Tableheight=(PanHeight-2*margintop)/(lineRows-1);
/**
* 画出外围的边界线
*/
g.drawRect(marginleft-2, margintop-2, Tablewidth*(lineCols-1)+4, Tableheight*(lineRows-1)+4);
/**
* 画出纵向的线
*/
for(int i=0;i<lineCols;i++){
g.drawLine(marginleft+Tablewidth*i, margintop, marginleft+Tablewidth*i, margintop+Tableheight*(lineRows-1));
/**
* 判断出不同卒位,及不同的形状 并画出
*/
if(i%2==0){
if(i==0){
DrawMarkleft(g, marginleft+Tablewidth*i, margintop+Tableheight*3);
DrawMarkleft(g, marginleft+Tablewidth*i, margintop+Tableheight*6);
}else if(i==lineCols-1){
DrawMarkright(g, marginleft+Tablewidth*i, margintop+Tableheight*3);
DrawMarkright(g, marginleft+Tablewidth*i, margintop+Tableheight*6);
}else{
DrawMark(g, marginleft+Tablewidth*i, margintop+Tableheight*3);
DrawMark(g, marginleft+Tablewidth*i, margintop+Tableheight*6);
}
}else if(i==3){
g.drawLine(marginleft+Tablewidth*i, margintop, marginleft+Tablewidth*(i+2), margintop+Tableheight*2);
g.drawLine(marginleft+Tablewidth*i, margintop+Tableheight*2, marginleft+Tablewidth*(i+2), margintop);
g.drawLine(marginleft+Tablewidth*i, margintop+Tableheight*7, marginleft+Tablewidth*(i+2), margintop+Tableheight*9);
g.drawLine(marginleft+Tablewidth*i, margintop+Tableheight*9, marginleft+Tablewidth*(i+2), margintop+Tableheight*7);
}
}
/**
* 画出横向的线
*/
for(int i=0;i<lineRows;i++){
g.drawLine(marginleft, margintop+Tableheight*i, marginleft+Tablewidth*(lineCols-1), margintop+Tableheight*i);
/**
* 判断位置 画出楚河汉界
*/
if(i==4){
g.setColor(Color.WHITE);
g.fillRect(marginleft+1, margintop+Tableheight*i+1,Tablewidth*(lineCols-1)-1,Tableheight-1);
g.setColor(Color.BLACK);
g.setFont(new Font("黑体",Font.BOLD,Tableheight*2/3));
g.drawString("楚河", marginleft+Tablewidth*2, margintop+Tableheight*(i+1)-Tableheight/6);
g.drawString("汉界", marginleft+Tablewidth*5, margintop+Tableheight*(i+1)-Tableheight/6);
/**
* 判断炮敦的位置并画出炮敦
*/
}else if(i==2||i==7){
DrawMark(g, marginleft+Tablewidth*1, margintop+Tableheight*i);
DrawMark(g, marginleft+Tablewidth*7, margintop+Tableheight*i);
}
}
}
}
package javaclass2;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.Enumeration;
import java.util.Hashtable;
public class Chessman {
/**
* 记录棋盘初始位置的x,y值
*/
private static int startx=40;
private static int starty=40;
/**
* 记录列数、行数的变量
*/
private static int cols=9;
private static int rows=10;
/**
* 得到棋盘中最小单元格的宽度、高度
*/
private static int width;
private static int height;
/**
* 获得单元格的宽度的方法
* @param width 画棋盘容器的宽度
*/
public static void setWidth(int width) {
Chessman.width = (width-2*startx)/(cols-1);
}
/**
* 获得单元格的高度的方法
* @param height 画棋盘容器的高度
*/
public static void setHeight(int height) {
Chessman.height = (height-2*starty)/(rows-1);
}
/**
* 盛装每个棋子上字符串的数组
*/
private static String[] strchesmanone={"車","馬","相","仕","将","仕","相","馬","車","炮","炮","卒","卒","卒","卒","卒"};
private static String[] strchesmantwo={"兵","兵","兵","兵","兵","炮","炮","车","马","象","士","帅","士","象","马","车"};
/**
* 盛装甲方棋子的信息
*/
private static Hashtable<Point, String> htone=new Hashtable<Point, String>();
public static Hashtable<Point, String> getHtone() {
return htone;
}
/**
* 盛装乙方棋子的信息
*/
private static Hashtable<Point, String> httwo=new Hashtable<Point, String>();
public static Hashtable<Point, String> getHttwo() {
return httwo;
}
/**
* 定义存放棋子信息的point类
*/
private static Point point;
/**
* 初始每个棋子位置的方法
* @param panwidth 画棋盘的容器的宽度
* @param panheight 画棋盘的容器的高度
*/
public static void Chessmanload(int panwidth,int panheight){
Chessman.width=(panwidth-2*startx)/(cols-1);
Chessman.height=(panheight-2*starty)/(rows-1);
int startone=-1;
int starttwo=-1;
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
if(i==0||(i==2&&(j==1||j==7))||(i==3&&j%2==0)){
startone++;
point=new Point();
/**
* 记录棋子在棋盘中的位置
* j 棋子所在的行
* i,棋子所在的列
*/
point.setLocation(j,i);
/**
* 将棋子的坐标加入到HashTable中(以棋盘最小单元格的宽和高为基本单位)
*/
htone.put(point, strchesmanone[startone]);
}else if(i==9||(i==7&&(j==1||j==7))||(i==6&&j%2==0)){
starttwo++;
point=new Point();
point.setLocation(j,i);
httwo.put(point, strchesmantwo[starttwo]);
}
}
}
startone=-1;
starttwo=-1;
}
/**
* 引用原始画图类的实例g
*/
private Graphics g;
public void setG(Graphics g) {
this.g = g;
}
/**
* 类的构造方法
*/
public Chessman(){
}
/**
* 画出一个棋子的方法
* @param point 棋子的位置
* @param strname 棋子上的字符串
* @param chesmancolor 棋子上字符串的颜色
*/
private void Chman(Point point,String strname,Color chesmancolor){
g.setColor(Color.DARK_GRAY);
g.fillOval(startx+width*point.x-width/2, starty+height*point.y-height/2, width, height);
/**
* 将选中棋子变为深灰色
*/
if(pointold!=null&&pointold.x==point.x&&pointold.y==point.y){
g.setColor(Color.darkGray);}
else{g.setColor(Color.GRAY);}
g.fillOval(startx+width*point.x-width/2+3, starty+height*point.y-height/2+3, width-6, height-6);
g.setColor(Color.WHITE);
g.drawOval(startx+width*point.x-width/2+5, starty+height*point.y-height/2+5, width-10, height-10);
g.setColor(chesmancolor);
g.setFont(new Font("黑体",Font.BOLD,height/2));
g.drawString(strname, startx+width*point.x-(width-10)/4, starty+height*point.y+10);
g.setColor(Color.BLACK);
}
/**
* 画出所有棋子位置的方法
*/
public void Drawchemanagin(){
/**
* 实现Enumeration接口将pointOne中的所有元素的key便利出来
*/
Enumeration<Point> pointone=htone.keys();
while(pointone.hasMoreElements()){
Point p=pointone.nextElement();
/**
* 便利后调用画棋子的方法画出所有棋子
*/
Chman(p,htone.get(p),Color.RED);
}
Enumeration<Point> pointtwo=httwo.keys();
while(pointtwo.hasMoreElements()){
Point p=pointtwo.nextElement();
Chman(p,httwo.get(p),Color.BLACK);
}
}
/**
* 标记鼠标是否选中棋子的变量
*/
private static boolean boolmark=false;
/**
* 记录棋子原来的位置的变量
*/
private static Point pointold=null;
public static Point getpointold(){
return pointold;
}
/**
* 记录用户选中的新的位置
*/
private static Point pointnew=null;
public static Point getpointnew(){
return pointnew;
}
/**
*更改棋子信息的方法
* @param e 鼠标事件
*/
private void getnewhashtable(MouseEvent e){
if(boolmark==false){
Point pnow=getNowrec(e.getX(),e.getY());
/**
* 如果选中的区域为棋子有效区并且当前位置有棋子则记录此位置为旧的棋子位置
*/
if(pnow!=null&&(htone.containsKey(pnow)||httwo.containsKey(pnow))&&pointold==null){
pointold=pnow;
boolmark=true;
}
}else {
Point pnow=getNowrec(e.getX(),e.getY());
if(pnow!=null){
/**
* 如果再次选中自己棋子,则以本次为准
*/
if((htone.containsKey(pointold)&&htone.containsKey(pnow))||(httwo.containsKey(pointold)&&httwo.containsKey(pnow))){
pointold=pnow;
return;
}
/**
* 通过规则判断所选位置是否合法
*/
if(Chrule.rule(pnow)==false){
return;
}
pointnew=pnow;
/**
* 将甲方棋子的位置变更为用户选中的位置
*/
if(htone.containsKey(pointold)){
if(pointold.x!=pointnew.x||pointold.y!=pointnew.y){
htone.put(pointnew,htone.get(pointold));
htone.remove(pointold);
/**
* 如果甲方选中的位置有乙方棋子,则将乙方棋子删除
*/
if(httwo.containsKey(pointnew)){
httwo.remove(pointnew);
}
}
}
if(httwo.containsKey(pointold)){
if(pointold.x!=pointnew.x||pointold.y!=pointnew.y){
httwo.put(pointnew, httwo.get(pointold));
httwo.remove(pointold);
/**
* 如果甲方选中的位置有乙方棋子,则将乙方棋子删除
*/
if(htone.containsKey(pointnew)){
htone.remove(pointnew);
}
}
}
/**
* 将标记变量清空
*/
boolmark=false;
pointold=null;
pointnew=null;
}
}
}
/**
* 根据鼠标位置定义坐标
* @param x 鼠标位置的横坐标
* @param y 鼠标位置的纵坐标
* @return 鼠标点击区域的相对坐标(以棋盘最小单元格的宽和高为基本单位的坐标)
*/
private Point getNowrec(int x,int y){
int intmark=0;
Point pnow=new Point();
/**
* 的到有效的point.x
*/
for(int i=0;i<cols;i++){
if(x<startx+width*i+width/2&&x>startx+width*i-width/2){
pnow.x=i;
intmark++;
break;
}
}
/**
* 得到有效的point.y
*/
for(int i=0;i<rows;i++){
if(y<starty+height*i+height/2&&y>starty+height*i-height/2){
pnow.y=i;
intmark++;
break;
}
}
/**
* 判断point.x和point.y是否均为有效值
*/
if(intmark==2){
return pnow;
}
return null;
}
static int inta=0;
public void doMuseclick(MouseEvent e){
/**
*按用户需求调用更改棋子信息的方法
*/
getnewhashtable(e);
/**
* 调用重绘棋子的方法
*/
Drawchemanagin();
/**
* 郁闷中....
*/
changesize();
}
private void changesize(){
inta++;
if(inta%2==0){
MyFrame.Jpanel.setSize(MyFrame.Jpanel.getWidth(),MyFrame.Jpanel.getHeight()+1);}
else{
MyFrame.Jpanel.setSize(MyFrame.Jpanel.getWidth(),MyFrame.Jpanel.getHeight()-1);}
if(inta>2147483646){
inta=0;
}
}
}
package javaclass2;
import java.awt.Point;
import java.util.Hashtable;
public class Chrule {
/**
* 引用存放甲方棋子的变量
*/
private static Hashtable<Point, String> htone;
/**
* 引用存放乙方棋子的变量
*/
private static Hashtable<Point, String> httwo;
/**
* 选中棋子原先的坐标
*/
private static Point pointold;
/**
* 标记新坐标是否可用的变量
*/
private static boolean bool=false;
/**
* 判断新坐标是否可用的方法
* @param p 用户选中的坐标
* @return 标记新坐标是否可用的变量
*/
public static boolean rule(Point p){
/**
* 初始变量值为false
*/
bool=false;
/**
* 获得存放甲方棋子的HashTable
*/
htone=Chessman.getHtone();
/**
* 获得存放甲方棋子的HashTable
*/
httwo=Chessman.getHttwo();
/**
* 获得棋子原先的坐标
*/
pointold=Chessman.getpointold();
Chessman.getpointnew();
/**
* 调用象棋规则
*/
ruleall(p);
/**
* 返回标记新的坐标是否可用的变量
*/
return bool;
}
/**
* 象棋的规则
* @param p 棋手选则的新位置坐标
*/
private static void ruleall(Point p){
String strht="";
if(htone.containsKey(pointold)){
strht=htone.get(pointold);
}else{
strht=httwo.get(pointold);
}
if(strht.equals("将")||strht.equals("帅")){
general(p);
}else if(strht.equals("車")||strht.equals("车")){
vehicel(p);
}else if(strht.equals("馬")||strht.equals("马")){
horse(p);
}else if(strht.equals("相")||strht.equals("象")){
elephant(p);
}else if(strht.equals("仕")||strht.equals("士")){
scholar(p);
}else if(strht.equals("炮")||strht.equals("炮")){
cannon(p);
}else if(strht.equals("卒")||strht.equals("兵")){
soldier(p);
}
}
/**
* 将,帅行走规则
*/
private static void general(Point p){
int intx=p.x;
int inty=p.y;
if(httwo.containsKey(pointold)){
inty=Math.abs(pointold.y-9);
}
/**
* 判断将,帅是否在活动区域内
*/
if(Math.abs(intx-4)<=1&&Math.abs(inty-1)<=1){
/**
* 判断将,帅是否每次走一格
*/
if((Math.abs(pointold.x-p.x)==1&&pointold.y==p.y)||(Math.abs(pointold.y-p.y)==1&&pointold.x==p.x)){
/**
* 判断所选棋子为甲方棋子,并且新位置上没有甲方棋子
*/
if(htone.containsKey(pointold)&&!htone.containsKey(p)){
bool=true;
/**
* 判断新位置上是否有乙方棋子,有就就将其吃掉
*/
if(httwo.containsKey(p)){
httwo.remove(p);
}
}
/**
* 判断所选棋子为乙方棋子,并且新位置上没有乙方棋子
*/
else if(httwo.containsKey(pointold)&&!httwo.containsKey(p)){
bool=true;
/**
* 判断新位置上是否有甲方棋子,有就就将其吃掉
*/
if(htone.containsKey(p)){
htone.remove(p);
}
}
}
}
}
/**
* 車,车行走规则
*/
private static void vehicel(Point p){
if(pointold.x==p.x||pointold.y==p.y){
/**
* 假定棋子上下移动时
*/
boolean boolmark=true;
if(pointold.x==p.x){
/**
* 实例一个坐标,来接收pointOld到p之间的所有坐标
*/
Point point=new Point();
point.x=p.x;
/**
* 将pointOld到p之间的所有坐标遍历出来
*/
for(int i=Math.min(pointold.y, p.y)+1;i<Math.max(pointold.y, p.y);i++){
point.y=i;
/**
* 如果pointOld到p之间有棋子,则新的坐标p为无效坐标
*/
if(htone.containsKey(point)||httwo.containsKey(point)){
boolmark=false;
break;
}
}
}
/**
* 假定棋子为左右移动时
*/
else if(pointold.y==p.y){
/**
* 实例一个坐标,来接收pointOld到p之间的所有坐标
*/
Point point=new Point();
point.y=p.y;
/**
* 将将pointOld到p之间的所有坐标遍历出来
*/
for(int i=Math.min(pointold.x, p.x)+1;i<Math.max(pointold.x, p.x);i++){
point.x=i;
/**
* 如果pointOld到p之间有棋子,则新的坐标p为无效坐标
*/
if(htone.containsKey(point)||httwo.containsKey(point)){
boolmark=false;
break;
}
}
}else{return;}
if(boolmark==true){
/**
* 如果pointOld到p之间没有棋子,则新的坐标p为有效坐标
*/
if(!htone.containsKey(p)&&!httwo.containsKey(p)){
bool=true;
}
/**
* 如果选中棋子为甲方棋子,且新的坐标p处有乙方棋子,则新坐标p为有效坐标,并将p处乙方棋子吃掉
*/
else if(htone.containsKey(pointold)&&!htone.containsKey(p)&&httwo.containsKey(p)){
bool=true;
httwo.remove(p);
}
/**
* 如果选中棋子为乙方棋子,且新的坐标p处有甲方棋子,则新坐标p为有效坐标,并将p处甲方棋子吃掉
*/
else if(httwo.containsKey(pointold)&&!httwo.containsKey(p)&&htone.containsKey(p)){
bool=true;
htone.remove(p);
}
}
}
}
/**
* 馬,马的行走规则
*/
private static void horse(Point p){
/**
* 记录马蹩腿的坐标
*/
Point point=new Point();
/**
* 获得马腿的坐标
*/
if(Math.abs(pointold.x-p.x)==1&&Math.abs(pointold.y-p.y)==2){
point.x=pointold.x;
if(pointold.y>p.y){
point.y=pointold.y-1;
}else{
point.y=pointold.y+1;
}
}else if(Math.abs(pointold.x-p.x)==2&&Math.abs(pointold.y-p.y)==1){
point.y=pointold.y;
if(pointold.x>p.x){
point.x=pointold.x-1;
}else{
point.x=pointold.x+1;
}
}else{return;}
/**
* 判断马腿坐标是否有棋子
*/
if(!htone.containsKey(point)&&!httwo.containsKey(point)){
/**
* 如果选中棋子为甲方棋子
*/
if(htone.containsKey(pointold)&&!htone.containsValue(p)){
bool=true;
if(httwo.containsKey(p)){
httwo.remove(p);
}
}
/**
* 如果选中棋子为乙方棋子
*/
else if(httwo.containsKey(pointold)&&!httwo.containsKey(p)){
bool=true;
if(htone.containsKey(p)){
htone.remove(p);
}
}
}
}
/**
* 象,相的行走规则
*/
private static void elephant(Point p){
/**
* 判断新的坐标p与选中棋子的坐标pointOld是否构成田字格
*/
if(Math.abs(pointold.x-p.x)==2&&Math.abs(pointold.y-p.y)==2){
/**
* 实例新的坐标来记录相眼坐标
*/
Point point=new Point();
point.x=(pointold.x+p.x)/2;
point.y=(pointold.y+p.y)/2;
/**
* 判断象眼point处是否有棋子
*/
if(!htone.containsKey(point)&&!httwo.containsKey(point)){
/**
* 如果选中棋子为甲方棋子,且新的坐标p没有越过汉界,p处没有甲方棋子,则新坐标p为有效坐标
*/
if(htone.containsKey(pointold)&&p.y<=4&&!htone.containsKey(p)){
bool=true;
/**
* 如果新的坐标p处有乙方棋子,则将其吃掉
*/
if(httwo.containsKey(p)){
httwo.remove(p);
}
}
/**
* 如果选中棋子为乙方棋子,且新的坐标p没有越过汉界,p处没有乙方棋子,则新的坐标p为有效坐标
*/
else if(httwo.containsKey(pointold)&&p.y>=5&&!httwo.containsKey(p)){
bool=true;
/**
* 如果新的坐标p处有甲方棋子,则将其吃掉
*/
if(htone.containsKey(p)){
htone.remove(p);
}
}
}
}
}
/**
* 士,仕的行走规则
*/
private static void scholar(Point p){
int intx=p.x;
int inty=p.y;
if(httwo.containsKey(pointold)){
inty=Math.abs(pointold.y-9);
}
/**
* 判断士,仕是否在活动区域内
*/
if(Math.abs(intx-4)<=1&&Math.abs(inty-1)<=1){
/**
* 判断选中棋子走的是否是仕线
*/
if(Math.abs(pointold.x-p.x)==1&&Math.abs(pointold.y-p.y)==1){
/**
* 如果选中棋子为甲方棋子,且新坐标p在甲方仕线区域内,新坐标p处没有甲方棋子,则新坐标p为有效坐标
*/
if(htone.containsKey(pointold)&&p.y<=2&&!htone.containsKey(p)){
bool=true;
/**
* 如果新坐标p处有乙方棋子,则将其吃掉
*/
if(httwo.containsKey(p)){
httwo.remove(p);
}
}
/**
* 如果选中棋子为乙方棋子,且新坐标p在乙方仕线区域内,新坐标p处没有乙方棋子,则新坐标p为有效坐标
*/
else if(httwo.containsKey(pointold)&&p.y>=7&&!httwo.containsKey(p)){
bool=true;
/**
* 如果新坐标p处有甲方棋子,则将其吃掉
*/
if(htone.containsKey(p)){
htone.remove(p);
}
}
}
}
}
/**
* 炮的行走规则
*/
private static void cannon(Point p){
if(pointold.x==p.x||pointold.y==p.y){
/**
* 假定棋子上下移动时
*/
int intmark=0;
if(pointold.x==p.x){
/**
* 实例一个坐标,来接收pointOld到p之间的所有坐标
*/
Point point=new Point();
point.x=p.x;
/**
* 将pointOld到p之间的所有坐标遍历出来
*/
for(int i=Math.min(pointold.y, p.y)+1;i<Math.max(pointold.y, p.y);i++){
point.y=i;
/**
* 如果pointOld到p之间有棋子,则新的坐标p为无效坐标
*/
if(htone.containsKey(point)||httwo.containsKey(point)){
intmark++;
}
}
}
/**
* 假定棋子为左右移动时
*/
else if(pointold.y==p.y){
/**
* 实例一个坐标,来接收pointOld到p之间的所有坐标
*/
Point point=new Point();
point.y=p.y;
/**
* 将将pointOld到p之间的所有坐标遍历出来
*/
for(int i=Math.min(pointold.x, p.x)+1;i<Math.max(pointold.x, p.x);i++){
point.x=i;
/**
* 如果pointOld到p之间(不包括p点)有棋子,则新的坐标p为无效坐标
*/
if(htone.containsKey(point)||httwo.containsKey(point)){
intmark++;
}
}
}else{return;}
if(intmark==0){
/**
* 如果pointOld到p之间(包括p点)没有棋子,则新的坐标p为有效坐标
*/
if(!htone.containsKey(p)&&!httwo.containsKey(p)){
bool=true;
}else{return;}
}
/**
* 选中棋子的坐标pointOld与新的坐标p之间有一个棋子
*/
else if(intmark==1){
/**
* 所选为甲方棋子,且新坐标p处有乙方棋子,p为有效坐标 吃掉乙方该棋子
*/
if(htone.containsKey(pointold)&&httwo.containsKey(p)){
bool=true;
httwo.remove(p);
}
/**
* 所选为乙方棋子,且新坐标p处有甲方棋子,p为有效坐标 吃掉甲方该棋子
*/
else if(httwo.containsKey(pointold)&&htone.containsKey(p)){
bool=true;
htone.remove(p);
}else{return;}
}else{return;}
}else{return;}
}
/**
* 兵,卒的行走规则
*/
private static void soldier(Point p){
/**
* 如果每次横向或纵向走一格
*/
if((Math.abs(pointold.x-p.x)==1&&pointold.y==p.y)||(Math.abs(pointold.y-p.y)==1&&pointold.x==p.x)){
/**
* 选中为甲方棋子,且新坐标p处无甲方棋子 ,卒、兵不后退
*/
if(htone.containsKey(pointold)&&!htone.containsKey(p)&&p.y>=pointold.y){
/**
* 过了汉界可横向移动,没过汉界只能向前冲
*/
if(pointold.y>4||(pointold.y<=4&&pointold.y==p.y-1)){
bool=true;
/**
* 若新坐标p处有乙方棋子,将其吃掉
*/
if(httwo.containsKey(p)){
httwo.remove(p);
}
}
}
/**
* 选中为乙方棋子,且新坐标p处无乙方棋子 ,卒、兵不后退
*/
else if(httwo.containsKey(pointold)&&!httwo.containsKey(p)&&p.y<=pointold.y){
/**
* 过了汉界可横向移动,没过汉界只能向前冲
*/
if(pointold.y<5||(pointold.y>=5&&pointold.y==p.y+1)){
bool=true;
/**
* 若新坐标p处有甲方棋子,将其吃掉
*/
if(htone.containsKey(p)){
htone.remove(p);
}
}
}
}
}
}
package javaclass2;
public enum FmStartPoint {
/**
* 屏幕左上方的位置
*/
Manual,
/**
* 窗体在屏幕中央位置
*/
CenterScreen,
/**
* 系统默认位置
*/
Widowdefaultloction,
/**
* 父窗体中央
*/
Centerparent
}
package javaclass2;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
@SuppressWarnings("all")
public class MyFrame extends JFrame {
public static MyJpanel Jpanel;
public MyFrame(){
super();
/**
* 设置窗体的最大大小
*/
this.setMaximumSize(new Dimension(766,908));
/**
* 设置窗体的最小大小
*/
this.setMinimumSize(new Dimension(380,450));
/**
* 设置窗体的大小
*/
this.setSize(670, 729);
/**
* 设置用户是否能调整窗体的大小
*/
//this.setResizable(false);
/**
* 窗体的位置
*/
//this.setLocation(360,10);
this.SetMyFramecenter();
/**
* 窗体的标题信息
*/
this.setCursor(new Cursor(Cursor.HAND_CURSOR));
/**
* 设置光标外观
*/
this.setTitle("中国象棋");
Jpanel =new MyJpanel();
/**
* 调用画棋子类的方法,为棋子初始坐标ChessLine的方法
*/
Chessman.Chessmanload(this.getWidth(),this.getHeight()-30);
this.add(Jpanel);
this.setVisible(true);
/**
* 定义窗体关闭为程序退出事件
*/
this.setDefaultCloseOperation(MyFrame.EXIT_ON_CLOSE);
}
/**
* 窗体位于屏幕中央的方法
*/
public void SetMyFramecenter(){
Toolkit kit =Toolkit.getDefaultToolkit();
Dimension Sensize=kit.getScreenSize();
int intx=Sensize.width;
int inty=Sensize.height;
this.setLocation((intx-this.getWidth())/2,(inty-this.getHeight())/2);
}
public void SetMyFrameloction(FmStartPoint enumark){
switch(enumark){
case Manual:
this.setLocation(0, 0);
break;
case CenterScreen:
SetMyFramecenter();
break;
case Widowdefaultloction:
this.setLocation(15, 15);
break;
case Centerparent:
//待续..........
break;
default:
break;
}
}
}
package javaclass2;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MyJpanel extends JPanel {
private Graphics g;
private int PanWidth;
private int PanHeight;
private Chessman chessman;
private ChessLine line;
public MyJpanel() {
super();
line = new ChessLine();
chessman = new Chessman();
this.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
line.Drawline(g, PanWidth,PanHeight);
chessman.doMuseclick(e);
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
@Override
public void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
this.g=g;
this.PanWidth=this.getWidth();
this.PanHeight=this.getHeight();
line.Drawline(g, PanWidth,PanHeight);
Chessman.setWidth(this.PanWidth);
Chessman.setHeight(this.PanHeight);
chessman.setG(g);
chessman.Drawchemanagin();
}
}
在画象棋棋子模块,为什么在doMuseclick方法中调用Jpanel的paintComponent无效?
请高手指点,先谢谢了。
- Graphics 象棋
- 象棋
- 象棋
- 象棋
- 象棋
- Java用graphics和鼠标监听写了个象棋程序,简单线下对弈版
- graphics
- Graphics
- Graphics
- Graphics
- graphics
- graphics
- Graphics
- Graphics
- Graphics
- Graphics
- Graphics
- Graphics
- hh
- dffd
- 私人的
- 疑惑
- 对你说
- Graphics 象棋
- 闪烁的窗体标题栏
- JavaWeb学习中
- SetTimer函数的用法
- 【转】人生之最
- 计算机快捷键
- 初学者,如何用hibernate配置SqlServer?(MyEclipse)2
- 我的课程
- 初学Java的基本脉络(仅供初学Java的同学参考)(二)