Javafx实现国际象棋游戏

本文实例为大家分享了Javafx实现国际象棋游戏的具体代码,供大家参考,具体内容如下

基本规则

  • 棋子马设计“日”的移动方式
  • 兵设计只能向前直走,每次只能走一格。但走第一步时,可以走一格或两格的移动方式
  • 请为后设计横、直、斜都可以走,步数不受限制,但不能越子的移动方式。
  • 车只能横向或者竖向行走
  • 国王是在以自己为中心的九宫格内行走
  • 骑士只能走对角线

项目目录结构

UML类图关系

以骑士为例

实现基本功能

  • 吃子
  • 不能越子
  • 游戏结束提示
  • 基本移动策略
  • 背景音乐

效果

控制器

PressedAction

package com.Exercise3;

import com.Exercise3.Controller.PressedAction;
import com.Exercise3.Controller.ReleaseAction;
import com.Exercise3.Controller.ResetAction;
import com.Exercise3.view.ChessBoard;
import com.Exercise3.view.ChessPane;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;

import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

public class Test extends Application {

 public static void main(String[] args) {
  launch(args);
 }

 @Override
 public void start(Stage primaryStage) {

  String MEDIA_URL = "file:/E:/IdeaProjects/Experiment/src/com/Exercise3/music/BackgroundMusic.mp3";
  ChessBoard chessBoard = ChessBoard.getInstance(100,40,40);

  //添加媒体资源

  Media media = new Media(MEDIA_URL);
  MediaPlayer mediaPlayer = new MediaPlayer(media);
  mediaPlayer.setAutoPlay(true);
  mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
  mediaPlayer.play();

  ChessPane pane = new ChessPane(chessBoard);
  pane.setOnMousePressed(new PressedAction(pane,mediaPlayer));

  pane.setOnMouseReleased(new ReleaseAction(pane));

  BorderPane borderPane = new BorderPane();
  borderPane.setCenter(pane);
  HBox hBox = new HBox();
  hBox.setAlignment(Pos.TOP_CENTER);

  Button button = new Button("悔棋");
  button.setOnAction(new ResetAction(pane));

  hBox.getChildren().add(button);
  borderPane.setBottom(hBox);
  Scene scene = new Scene(borderPane,900,900);
  primaryStage.setScene(scene);
  primaryStage.setTitle("国际象棋");
  primaryStage.show();

 }
}

ReleasedAction

package com.Exercise3.Controller;

import com.Exercise3.entity.Piece.ChessPiece;
import com.Exercise3.entity.PieceType;
import com.Exercise3.view.ChessBoard;
import com.Exercise3.view.ChessPane;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.input.MouseEvent;

import java.util.Stack;

public class ReleaseAction implements EventHandler<MouseEvent> {
 private ChessPane chessPane;
 static Stack<ChessPiece> stack = new Stack<>();

 public ReleaseAction(ChessPane chessPane) {
  this.chessPane = chessPane;
 }

 @Override
 public void handle(MouseEvent e) {
  chessPane.drawBoard();
  ChessBoard chessBoard = chessPane.getChessBoard();
  int x = (int) ((e.getX() - chessBoard.getStartX()) / (chessBoard.getCellLength()));
  int y = (int) ((e.getY() - chessBoard.getStartY()) / (chessBoard.getCellLength()));

  for (ChessPiece o : chessPane.getChessPieces()) {
   if (o.isSelected()) {

    System.out.println(o.isSelected()+" "+o.getRow()+" "+o.getCol());
    if (chessBoard.getCurrSide()==o.getSide()){
     if(o.getMoveStrategy().move(x, y,chessPane.getChessPieces())){
      o.setSelected(false);
      if(judgeGame(x,y)){
       printTip(o.getSide());
      }
      eatPiece(x,y);
      stack.push((ChessPiece) o.clone());
      o.setCol(x);
      o.setRow(y);

      chessBoard.changeSide();
     }

    }

    break;
   }

  }

  chessPane.drawPiece();
 }

 public void eatPiece(int x,int y){
  chessPane.getChessPieces().removeIf(e->{
   if(e.getCol()==x&&e.getRow()==y){
    stack.push(e);
    return true;
   }
   return false;
  });
 }

 public boolean judgeGame(int x,int y){
  for(ChessPiece e:chessPane.getChessPieces()){
   if(e.getCol()==x&&e.getRow()==y&&(
     e.getType()== PieceType.KINGBLACK||e.getType()==PieceType.KINGWHITE))
    return true;
  }

  return false;
 }

 public void printTip(char side){
  Alert alert = new Alert(Alert.AlertType.INFORMATION);
  alert.setContentText((side=='B'?"黑":"白")+"方取得胜利");
  alert.setTitle("游戏结束");
  alert.showAndWait();
 }

}

ResetAction

package com.Exercise3.Controller;

import com.Exercise3.entity.Piece.ChessPiece;
import com.Exercise3.view.ChessPane;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;

import java.util.Stack;

public class ResetAction implements EventHandler<ActionEvent>{
 private ChessPane chessPane;
 public ResetAction(ChessPane chessPane) {
  this.chessPane = chessPane;
 }

 @Override
 public void handle(ActionEvent e) {
  Stack<ChessPiece> stack = ReleaseAction.stack;
  if(!stack.empty()){
   chessPane.getChessPieces().removeIf(o->o.equals(stack.peek()));//去除原来的棋子
   chessPane.getChessPieces().add(stack.pop());//将以前压入堆栈的棋子重新加入

   chessPane.drawBoard();
   chessPane.drawPiece();
  }
 }
}

实体

棋子

ChessPiece

package com.Exercise3.entity.Piece;

import com.Exercise3.entity.PieceType;
import com.Exercise3.entity.Strategy.CarStrategy;

public class Car extends ChessPiece {
 public Car(PieceType type, int row, int col) {
  super(type, row, col);
  setMoveStrategy(new CarStrategy(getCol(),getRow()));
 }
}

Car

package com.Exercise3.entity.Piece;

import com.Exercise3.entity.PieceType;
import com.Exercise3.entity.Strategy.CarStrategy;

public class Car extends ChessPiece {
 public Car(PieceType type, int row, int col) {
  super(type, row, col);
  setMoveStrategy(new CarStrategy(getCol(),getRow()));
 }
}

Horse

package com.Exercise3.entity.Piece;

import com.Exercise3.entity.PieceType;
import com.Exercise3.entity.Strategy.HorseStategy;

public class Horse extends ChessPiece{
 public Horse(PieceType type, int row, int col) {
  super(type, row, col);
  setMoveStrategy(new HorseStategy(getCol(),getRow()));
 }
}

King

package com.Exercise3.entity.Piece;

import com.Exercise3.entity.PieceType;
import com.Exercise3.entity.Strategy.KingStrategy;

public class King extends ChessPiece {
 public King(PieceType type, int row, int col) {
  super(type, row, col);
  setMoveStrategy(new KingStrategy(getCol(),getRow()));
 }
}

Knight

package com.Exercise3.entity.Piece;

import com.Exercise3.entity.PieceType;
import com.Exercise3.entity.Strategy.KnightStrategy;

public class Knight extends ChessPiece {
 public Knight(PieceType type, int row, int col) {
  super(type, row, col);
  setMoveStrategy(new KnightStrategy(getCol(),getRow()));
 }
}

Queen

package com.Exercise3.entity.Piece;

import com.Exercise3.entity.PieceType;
import com.Exercise3.entity.Strategy.QueenStrategy;

public class Queen extends ChessPiece {
 public Queen(PieceType type, int row, int col) {
  super(type, row, col);
  setMoveStrategy(new QueenStrategy(getCol(),getRow()));
 }
}

Soldier

package com.Exercise3.entity.Piece;

import com.Exercise3.entity.PieceType;
import com.Exercise3.entity.Strategy.SoldierStategy;

public class Soldier extends ChessPiece{
 public Soldier(PieceType type, int row, int col) {
  super(type, row, col);
  setMoveStrategy(new SoldierStategy(getCol(),getRow(),getSide()));
 }

}

移动策略

MoveStategy

package com.Exercise3.entity.Strategy;

import com.Exercise3.entity.Piece.ChessPiece;

import java.util.Set;

public interface MoveStrategy {
 boolean move(int x, int y, Set<ChessPiece> chessPieces);
}

CarStrategy

package com.Exercise3.entity.Strategy;

import com.Exercise3.entity.Piece.ChessPiece;

import java.util.List;
import java.util.Set;

public class CarStrategy implements MoveStrategy {
 private int curX;
 private int curY;

 public CarStrategy() {
 }

 public CarStrategy(int curX, int curY) {
  this.curX = curX;
  this.curY = curY;
 }

 public boolean move(int x, int y, Set<ChessPiece> chessPieces) {
  if(x!=curX&&y!=curY)
   return false;
  if(isOverPiece(Math.min(curX,x),Math.min(curY,y),
    Math.max(curX,x),Math.max(curY,y),chessPieces))
   return false;
  curX = x;
  curY = y;
  return true;
 }

 public static boolean isOverPiece(int stX,int stY,int edX,int edY,Set<ChessPiece> chessPieces){
  for(ChessPiece e:chessPieces)
   if((e.getRow()>stY&&e.getRow()<edY)&&e.getCol()==stX||
     (e.getCol()>stX&&e.getCol()<edX&&e.getRow()==stY))
    return true;
  return false;
 }

 public int getCurX() {
  return curX;
 }

 public void setCurX(int curX) {
  this.curX = curX;
 }

 public int getCurY() {
  return curY;
 }

 public void setCurY(int curY) {
  this.curY = curY;
 }
}

HorseStrategy

package com.Exercise3.entity.Strategy;

import com.Exercise3.entity.Piece.ChessPiece;

import java.util.List;
import java.util.Set;

public class HorseStategy implements MoveStrategy{
 private int curX;
 private int curY;

 public HorseStategy(int curX, int curY) {
  this.curX = curX;
  this.curY = curY;
 }

 @Override
 public boolean move(int x, int y, Set<ChessPiece> chessPieces) {
  if((Math.abs(curX-x)==1&&Math.abs(curY-y)==2)||
    (Math.abs(curX-x)==2&&Math.abs(curY-y)==1)){
   curX = x;
   curY = y;
   return true;
  }
  return false;
 }

 public int getCurX() {
  return curX;
 }

 public void setCurX(int curX) {
  this.curX = curX;
 }

 public int getCurY() {
  return curY;
 }

 public void setCurY(int curY) {
  this.curY = curY;
 }
}

KingStrategy

package com.Exercise3.entity.Strategy;

import com.Exercise3.entity.Piece.ChessPiece;

import java.util.List;
import java.util.Set;

public class KingStrategy implements MoveStrategy {
 private int curX;
 private int curY;

 public KingStrategy(int curX, int cuY) {
  this.curX = curX;
  this.curY = cuY;
 }

 @Override
 public boolean move(int x, int y, Set<ChessPiece> chessPieces) {
  if(Math.abs(curX-x)<=1&&Math.abs(curY-y)<=1){
   curX = x;
   curY = y;
   return true;
  }

  return false;
 }

 public int getCurX() {
  return curX;
 }

 public void setCurX(int curX) {
  this.curX = curX;
 }

 public int getCurY() {
  return curY;
 }

 public void setCurY(int curY) {
  this.curY = curY;
 }
}

KnightStrage

package com.Exercise3.entity.Strategy;

import com.Exercise3.entity.Piece.ChessPiece;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class KnightStrategy implements MoveStrategy {
 private int curX;
 private int curY;

 public KnightStrategy(int curX, int curY) {
  this.curX = curX;
  this.curY = curY;
 }

 @Override
 public boolean move(int x, int y, Set<ChessPiece> chessPieces) {
  if(Math.abs(x-curX)==Math.abs(y-curY)){
   if(isOverPiece(Math.min(curX,x),Math.min(curY,y),
     Math.max(curX,x),Math.max(curY,y),chessPieces))
    return false;
   curX=x;
   curY=y;
   return true;
  }
  return false;
 }

 public static boolean isOverPiece(int stX,int stY,int edX,int edY,Set<ChessPiece> chessPieces){
  for(ChessPiece e:chessPieces){
   if(e.getCol()-stX==edX-e.getCol()&&edY-e.getRow()==e.getRow()-stY){
    System.out.println(e.isSelected()+" "+e.getRow()+" "+e.getCol());
    return true;
   }
  }

  return false;
 }
 public int getCurX() {
  return curX;
 }

 public void setCurX(int curX) {
  this.curX = curX;
 }

 public int getCurY() {
  return curY;
 }

 public void setCurY(int curY) {
  this.curY = curY;
 }
}

QueeStrategy

package com.Exercise3.entity.Strategy;

import com.Exercise3.entity.Piece.ChessPiece;

import java.util.List;
import java.util.Set;

public class QueenStrategy implements MoveStrategy{
 private int curX;
 private int curY;

 public QueenStrategy(int curX, int curY) {
  this.curX = curX;
  this.curY = curY;
 }

 @Override
 public boolean move (int x, int y, Set<ChessPiece> chessPieces) {
  if(Math.abs(x-curX)==Math.abs(y-curY)||!(x!=curX&&y!=curY)){
   if(isOverPiece(Math.min(curX,x),Math.min(curY,y),
     Math.max(curX,x),Math.max(curY,y),chessPieces))
    return false;
   curX = x;
   curY = y;
   return true;
  }
  return false;
 }

 public boolean isOverPiece (int stX,int stY,int edX,int edY,Set<ChessPiece> chessPieces) {
  for(ChessPiece e:chessPieces){
   if(e.getRow()!=stY&&e.getCol()!=stX){
    return KnightStrategy.isOverPiece(stX,stY,edX,edY,chessPieces);
   }
   else{
    return CarStrategy.isOverPiece(stX,stY,edX,edY,chessPieces);
   }
  }
  return false;
 }

 public int getCurX() {
  return curX;
 }

 public void setCurX(int curX) {
  this.curX = curX;
 }

 public int getCurY() {
  return curY;
 }

 public void setCurY(int curY) {
  this.curY = curY;
 }
}

SoldierStrategy

package com.Exercise3.entity.Strategy;

import com.Exercise3.entity.Piece.ChessPiece;

import java.util.List;
import java.util.Set;

public class SoldierStategy implements MoveStrategy{
 private int curX;
 private int curY;
 private char side;
 private boolean firstMove = true;

 public SoldierStategy(int curX, int curY,char side) {
  this.curX = curX;
  this.curY = curY;
  this.side = side;
 }

 @Override
 public boolean move(int x, int y, Set<ChessPiece> chessPieces) {
  //直线移动
  if(curY==y){
   switch (side){
    case 'B': {
     if(isFirstMove()&&(x==curX+1||curX+2==x)){
      setFirstMove(false);
      curY = y;
      curX = x;
      return true;
     }
     else if(!isFirstMove()&&curX+1==x){
      curY = y;
      curX = x;
      return true;
     }
     break;
    }

    case 'W':{
     if(isFirstMove()&&(x==curX-1||x==curX-2)){
      setFirstMove(false);
      curY = y;
      curX = x;
      return true;
     }
     else if(!isFirstMove()&&curX-1==x){
      curY = y;
      curX = x;
      return true;
     }
     break;
    }
   }
  }

  //吃子移动
  for(ChessPiece e:chessPieces){
   if(Math.abs(e.getRow()-curY)==1){
    if(e.getCol()-curX==1&&e.getSide()=='W'||
    curX-e.getCol()==1&&e.getSide()=='B'){
     curY = y;
     curX = x;
     return true;
    }

   }
  }

  return false;
 }

 public boolean isFirstMove() {
  return firstMove;
 }

 public void setFirstMove(boolean firstMove) {
  this.firstMove = firstMove;
 }

 public int getCurX() {
  return curX;
 }

 public void setCurX(int curX) {
  this.curX = curX;
 }

 public int getCurY() {
  return curY;
 }

 public void setCurY(int curY) {
  this.curY = curY;
 }
}

棋子类型

package com.Exercise3.entity;

public enum PieceType {
 KINGBLACK("KingBlack","com/Exercise3/img/KingBlack.jpg"),
 QUEENBLACK("QueenBlack","com/Exercise3/img/QueenBlack.jpg"),
 CARBLACK("CarBlack","com/Exercise3/img/CarBlack.jpg"),
 HORSEBLACK("HorseBlack","com/Exercise3/img/HorseBlack.jpg"),
 SOLDIERBLACK("SoldierBlack","com/Exercise3/img/SoldierBlack.jpg"),
 KNIGHTBLACK("KnightBlack","com/Exercise3/img/KnightBlack.jpg"),

 KINGWHITE("KingWhite","com/Exercise3/img/KingWhite.jpg"),
 QUEENWHITE("QueenWhite","com/Exercise3/img/QueenWhite.jpg"),
 CARWHITE("CarWhite","com/Exercise3/img/CarWhite.jpg"),
 HORSEWHITE("HorseWhite","com/Exercise3/img/HorseWhite.jpg"),
 SOLDIERWHITE("SoldierWhite","com/Exercise3/img/SoldierWhite.jpg"),
 KNIGHTWHITE("KnightWhite","com/Exercise3/img/KnightWhite.jpg");

 private String desc;
 private PieceType(String desc,String url ){
  this.desc = desc;
  this.url = url;
 }

 private String url;

 public String getDesc(){
  return desc;
 }

 public String getUrl() {
  return url;
 }
}

视图

package com.Exercise3.view;

public class ChessBoard {
 static ChessBoard chessBoard = null;
 private int row;
 private int col;
 private double cellLength;
 private double startX;
 private double startY;
 private char currSide;

 private ChessBoard(double cellLength, double startX, double startY) {
  this.row = 8;
  this.col = 8;
  this.cellLength = cellLength;
  this.startX = startX;
  this.startY = startY;
  this.currSide = 'B';
 }

 public static ChessBoard getInstance(double cellLength, double startX, double startY){
  if(chessBoard == null)
   return new ChessBoard(cellLength,startX,startY);
  return chessBoard;
 }

 public ChessBoard getInstance(){
  return chessBoard;
 }

 public int getCol() {
  return col;
 }

 public int getRow() {
  return row;
 }

 public double getCellLength() {
  return cellLength;
 }

 public void changeSide(){
  currSide=(currSide=='B'?'W':'B');
 }

 public void setCellLength(double cellLength) {
  this.cellLength = cellLength;
 }

 public double getStartX() {
  return startX;
 }

 public void setStartX(double startX) {
  this.startX = startX;
 }

 public double getStartY() {
  return startY;
 }

 public void setStartY(double startY) {
  this.startY = startY;
 }

 public char getCurrSide() {
  return currSide;
 }
}
package com.Exercise3.view;

import com.Exercise3.entity.Piece.*;
import com.Exercise3.entity.PieceType;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.*;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;

import java.util.*;

public class ChessPane extends Pane {
 private Set<ChessPiece> chessPieces;
 private ChessBoard chessBoard;
 private Canvas canvas;
 private GraphicsContext gc;

 public ChessPane(ChessBoard chessBoard) {
  this.chessBoard = chessBoard;
  setChessPiece();
  canvas = new Canvas(900,900);
  gc = canvas.getGraphicsContext2D();
  draw();
 }

 public void draw(){
  drawBoard();
  drawPiece();
  getChildren().add(canvas);
 }

 public void drawBoard(){
  gc.clearRect(0,0,900,900);
  double x = chessBoard.getStartX();
  double y = chessBoard.getStartY();
  double cell = chessBoard.getCellLength();

  boolean flag = false;
  for(int i=0;i<chessBoard.getRow();i++){
   flag = !flag;
   for(int j=0;j<chessBoard.getCol();j++){
    gc.setFill(flag? Color.valueOf("#EDEDED"):Color.valueOf("CDC5BF"));
    gc.fillRect(x+j*cell,y+i*cell,cell,cell);
    flag = !flag;
   }
  }

  gc.setStroke(Color.GRAY);
  gc.strokeRect(x,y,cell*chessBoard.getCol(),cell*chessBoard.getRow());

 }

 public void drawPiece(){
  double cell = chessBoard.getCellLength();
  chessPieces.forEach( e->{
   if(e.isSelected()){
    gc.setFill(Color.valueOf("#6495ED"));
    gc.fillRect(chessBoard.getStartX()+e.getCol()*cell,
      chessBoard.getStartY()+e.getRow()*cell,
      cell,cell);
   }

   Image image = new Image(e.getType().getUrl());
   gc.drawImage(image,
     chessBoard.getStartX()+10 + e.getCol() * cell,
     chessBoard.getStartY()+10 + e.getRow() * cell,
     cell-20, cell-20);
  });
 }

 //加入棋子
 public void setChessPiece() {
  chessPieces = new HashSet<>();
  chessPieces.add(new Car(PieceType.CARBLACK,0,0));
  chessPieces.add(new Horse(PieceType.HORSEBLACK,1,0));
  chessPieces.add(new Knight(PieceType.KNIGHTBLACK,2,0));
  chessPieces.add(new King(PieceType.KINGBLACK,3,0));
  chessPieces.add(new Queen(PieceType.QUEENBLACK,4,0));
  chessPieces.add(new Knight(PieceType.KNIGHTBLACK,5,0));
  chessPieces.add(new Horse(PieceType.HORSEBLACK,6,0));
  chessPieces.add(new Car(PieceType.CARBLACK,7,0));
  for(int i=0;i<8;i++){
   chessPieces.add(new Soldier(PieceType.SOLDIERBLACK,i,1));
  }

  chessPieces.add(new Car(PieceType.CARWHITE,0,7));
  chessPieces.add(new Horse(PieceType.HORSEWHITE,1,7));
  chessPieces.add(new Knight(PieceType.KNIGHTWHITE,2,7));
  chessPieces.add(new King(PieceType.KINGWHITE,3,7));
  chessPieces.add(new Queen(PieceType.QUEENWHITE,4,7));
  chessPieces.add(new Knight(PieceType.KNIGHTWHITE,5,7));
  chessPieces.add(new Horse(PieceType.HORSEWHITE,6,7));
  chessPieces.add(new Car(PieceType.CARWHITE,7,7));
  for(int i=0;i<8;i++){
   chessPieces.add(new Soldier(PieceType.SOLDIERWHITE,i,6));
  }
 }

 public ChessBoard getChessBoard() {
  return chessBoard;
 }

 public void setChessBoard(ChessBoard chessBoard) {
  this.chessBoard = chessBoard;
 }

 public Set<ChessPiece> getChessPieces() {
  return chessPieces;
 }

 public void setChessPieces(Set<ChessPiece> chessPieces) {
  this.chessPieces = chessPieces;
 }

 public Canvas getCanvas() {
  return canvas;
 }

 public void setCanvas(Canvas canvas) {
  this.canvas = canvas;
 }

 public GraphicsContext getGc() {
  return gc;
 }

 public void setGc(GraphicsContext gc) {
  this.gc = gc;
 }
}

测试

package com.Exercise3;

import com.Exercise3.Controller.PressedAction;
import com.Exercise3.Controller.ReleaseAction;
import com.Exercise3.Controller.ResetAction;
import com.Exercise3.view.ChessBoard;
import com.Exercise3.view.ChessPane;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;

import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

public class Test extends Application {

 public static void main(String[] args) {
  launch(args);
 }

 @Override
 public void start(Stage primaryStage) {

  String MEDIA_URL = "file:/E:/IdeaProjects/Experiment/src/com/Exercise3/music/BackgroundMusic.mp3";
  ChessBoard chessBoard = ChessBoard.getInstance(100,40,40);

  //添加媒体资源

  Media media = new Media(MEDIA_URL);
  MediaPlayer mediaPlayer = new MediaPlayer(media);
  mediaPlayer.setAutoPlay(true);
  mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
  mediaPlayer.play();

  ChessPane pane = new ChessPane(chessBoard);
  pane.setOnMousePressed(new PressedAction(pane,mediaPlayer));

  pane.setOnMouseReleased(new ReleaseAction(pane));

  BorderPane borderPane = new BorderPane();
  borderPane.setCenter(pane);
  HBox hBox = new HBox();
  hBox.setAlignment(Pos.TOP_CENTER);

  Button button = new Button("悔棋");
  button.setOnAction(new ResetAction(pane));

  hBox.getChildren().add(button);
  borderPane.setBottom(hBox);
  Scene scene = new Scene(borderPane,900,900);
  primaryStage.setScene(scene);
  primaryStage.setTitle("国际象棋");
  primaryStage.show();

 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • java实现五子棋小游戏

    java实现五子棋小游戏 package Gomoku; import java.awt.Toolkit; import javax.swing.JFrame; public class GomokuFrame extends JFrame { //定义一个操作面板 OperatorPane op=null; public GomokuFrame() { //设置名称 this.setTitle("五子棋"); //设置窗口大小 this.setSize(510,510); //设置窗

  • Java完美实现2048小游戏

    完美地模仿了2048游戏,是根据网友的一个2048改的. Block.java import javax.swing.*; import java.awt.*; public class Block extends JLabel { private int value; public Block() { value = 0;//初始化值为0 setFont(new Font("font", Font.PLAIN, 40));//设定字体 setBackground(Color.gray

  • Java编写迷宫小游戏

    缘起: 去年(大三上学期)比较喜欢写小游戏,于是想试着写个迷宫试一下. 程序效果: 按下空格显示路径: 思考过程: 迷宫由一个一个格子组成,要求从入口到出口只有一条路径. 想了一下各种数据结构,似乎树是比较合适的,从根节点到每一个子节点都只有一条路径.假设入口是根节点,出口是树中某个子节点,那么,从根节点到该子节点的路径肯定是唯一的. 所以如果能构造一棵树把所有的格子都覆盖到,也就能够做出一个迷宫了. 另外还要求树的父节点和子节点必须是界面上相邻的格子. 在界面显示时,父节点和子节点之间共用的边

  • Java棋类游戏实践之中国象棋

    本文实例讲述了java实现的中国象棋游戏代码,分享给大家供大家参考,具体代码如下 一.实践目的: 1.鼠标点击.拖动等事件的应用与区别 2.棋谱文件的保存与读取 3.完善象棋的规则. 二.实践内容: 中国象棋历史悠久,吸引了无数的人研究,现对中国象棋的对战和实现棋谱的制作做如下的设计和说明,供大家参考学习. 1.机机对弈,红方先手.在符合规则的情况下拖动棋子到目的地,松鼠标落子. 人人对弈图 2.制作棋谱,选择制作棋谱菜单后,对弈开始,并记录了下棋过程. 选择"制作棋谱"菜单 棋谱制作

  • Java制作智能拼图游戏原理及代码

    今天突发奇想,想做一个智能拼图游戏来给哄女友. 需要实现这些功能 第一图片自定义 第二宫格自定义,当然我一开始就想的是3*3 4*4 5*5,没有使用3*5这样的宫格. 第三要实现自动拼图的功能,相信大家知道女人耍游戏都不是很厉害,所以这个自动拼图功能得有. 其他什么暂停.排行就不写了! 现在重点问题出来了 要实现自动拼图功能似乎要求有点高哦!计算机有可不能像人一样只能: 先追究下本质 拼图游戏其实就是排列问题: 排列有这么一个定义:在一个1,2,...,n的排列中,如果一对数的前后位置与大小顺

  • Java实现打飞机小游戏(附完整源码)

    写在前面 技术源于分享,所以今天抽空把自己之前用java做过的小游戏整理贴出来给大家参考学习.java确实不适合写桌面应用,这里只是通过这个游戏让大家理解oop面向对象编程的过程,纯属娱乐.代码写的很简单,也很容易理解,并且注释写的很清楚了,还有问题,自己私下去补课学习. 效果如下 完整代码 敌飞机 import java.util.Random; 敌飞机: 是飞行物,也是敌人 public class Airplane extends FlyingObject implements Enemy

  • java编写贪吃蛇小游戏

    废话不多说,直接奉上代码: Frame.java package snake; import java.awt.Graphics; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import

  • java贪吃蛇游戏编写代码

    本文实例为大家分享了java贪吃蛇游戏展示的具体代码,供大家参考,具体内容如下 1.采用MVC(model.view.control)框架模式 2.包和类的关系树形图为: 3.源码: package com.huai; import Java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.util.HashSet; import java.util.LinkedList; import java.u

  • java编写的简单移动方块小游戏代码

    本文实例讲述了java编写的简单移动方块小游戏代码.分享给大家供大家参考,具体如下: 运行效果截图如下: 第一次用java编写图形化的界面,还是有些青涩..以后继续努力!!具体代码如下: //Little Box Game by AlexYui //Game.java By 1093710210@ HIT import javax.swing.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.*; import

  • java实现的简单猜数字游戏代码

    本文实例讲述了java实现的简单猜数字游戏代码.分享给大家供大家参考. 具体代码如下: 复制代码 代码如下: import java.util.InputMismatchException; import java.util.Scanner; public class Main {         public static void main(String[] args) {                 // 产生一个随机数                 int number = (in

随机推荐