Docsity

Разработка Игрового Приложения на Java: Создание Шахматной Доски, Курсовые работы из Информатика

Toggle sidebar
bg1
Индивидуальное задание по специальности.
Создать игровое приложение.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
publicclassfrmChessBoardextends JFrame implements MouseListener
{
publicstaticvoid main(String[] args)
{
final frmChessBoard app = new frmChessBoard();
}
// the whole constructor is for setting up the UI of the form
public frmChessBoard()
{
c = getContentPane();
setBounds(100, 100, 470, 495);
setBackground(new Color(204, 204, 204));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Tutorial: How to Draw a Chess Board in Java");
setResizable(false);
c.setLayout(null);
pnlMain.setBounds(3, 3, 460, 460);
pnlMain.setBackground(new Color(255, 255, 255));
pf3
pf4
pf5
pf8
pf9
pfa

Частичный предварительный просмотр текста

Скачай Разработка Игрового Приложения на Java: Создание Шахматной Доски и еще Курсовые работы в формате PDF Информатика только на Docsity!

Индивидуальное задание по специальности. Создать игровое приложение. import java.awt.; import javax.swing.; import java.awt.event.*; publicclass frmChessBoard extends JFrame implements MouseListener { publicstaticvoid main(String[] args) { final frmChessBoard app = new frmChessBoard(); } // the whole constructor is for setting up the UI of the form public frmChessBoard() { c = getContentPane(); setBounds(100, 100, 470, 495); setBackground( new Color(204, 204, 204)); setDefaultCloseOperation( EXIT_ON_CLOSE ); setTitle("Tutorial: How to Draw a Chess Board in Java"); setResizable( false ); c.setLayout( null ); pnlMain.setBounds(3, 3, 460, 460); pnlMain.setBackground( new Color(255, 255, 255));

c.add(pnlMain); this .drawChessBoard(); this .arrangeChessPieces(); show(); } // This method captures the move on the chess board and then make // it happen, logically and physically; also, it sends the move to // the other client publicvoid mouseClicked(MouseEvent e) { if (bMyTurn) { Object source = e.getSource(); JPanel pnlTemp = (JPanel)source; int intX = (pnlTemp.getX()/57); int intY = (pnlTemp.getY()/57); this .boolMoveSelection =! this .boolMoveSelection; if ( this .boolMoveSelection) { this .pntMoveFrom = new Point(intX, intY); if ( this .strChessBoard[ this .pntMoveFrom.y] [ this .pntMoveFrom.x].toString().trim().equals("")) this .boolMoveSelection =! this .boolMoveSelection;

else { JOptionPane. showMessageDialog ( this , "Invalid Move Request.", "Warning", JOptionPane. ERROR_MESSAGE ); this .makeChessPieceDifferent( false ); } } else this .makeChessPieceDifferent( false ); } } } // This method checks if attempted move is valid or not privateboolean isMoveValid() { boolean isMoveValid = true ; return isMoveValid; } // This method makes the selected chess piece looks like selected privatevoid makeChessPieceDifferent( boolean bSelected) { for ( int z = 0; z < this .pnlChessCells[ this .pntMoveFrom.y] [ this .pntMoveFrom.x].getComponentCount(); z++)

if ( this .pnlChessCells[ this .pntMoveFrom.y] [ this .pntMoveFrom.x].getComponent(z).getClass().toString().indexOf("JLabel") > -

{ JLabel lblTemp = (JLabel) this .pnlChessCells[ this .pntMoveFrom.y] [ this .pntMoveFrom.x].getComponent(z); lblTemp.setEnabled(!bSelected); } } // If class level variables Point-From and Point-To are set, // then this method actually moves a piece, if any exists, from // one cell to the other privatevoid moveChessPiece() { for ( int z = 0; z < this .pnlChessCells[ this .pntMoveTo.y] [ this .pntMoveTo.x].getComponentCount(); z++) if ( this .pnlChessCells[ this .pntMoveTo.y] [ this .pntMoveTo.x].getComponent(z).getClass().toString().indexOf("JLabel") > -1) { this .pnlChessCells[ this .pntMoveTo.y][ this .pntMoveTo.x].remove(z); this .pnlChessCells[ this .pntMoveTo.y][ this .pntMoveTo.x].repaint(); } for ( int z = 0; z < this .pnlChessCells[ this .pntMoveFrom.y] [ this .pntMoveFrom.x].getComponentCount(); z++) if ( this .pnlChessCells[ this .pntMoveFrom.y] [ this .pntMoveFrom.x].getComponent(z).getClass().toString().indexOf("JLabel") > -

lblTemp = new JLabel( this .kingBlack); elseif (strPieceName.equals("PB")) lblTemp = new JLabel( this .pawnBlack); elseif (strPieceName.equals("RW")) lblTemp = new JLabel( this .rookWhite); elseif (strPieceName.equals("BW")) lblTemp = new JLabel( this .bishopWhite); elseif (strPieceName.equals("NW")) lblTemp = new JLabel( this .knightWhite); elseif (strPieceName.equals("QW")) lblTemp = new JLabel( this .queenWhite); elseif (strPieceName.equals("KW")) lblTemp = new JLabel( this .kingWhite); elseif (strPieceName.equals("PW")) lblTemp = new JLabel( this .pawnWhite); else lblTemp = new JLabel(); return lblTemp; } // This method reads strChessBoard two-dimensional array of string // and places chess pieces at their right positions privatevoid arrangeChessPieces() {

for ( int y = 0; y < 8; y++) for ( int x = 0; x < 8; x++) { this .pnlChessCells[y][x].add( this .getPieceObject(strChessBoard[y][x]), BorderLayout. CENTER ); this .pnlChessCells[y][x].validate(); } } Рис.1. privatevoid drawChessBoard() { for ( int y = 0; y < 8; y++) for ( int x = 0; x < 8; x++) { pnlChessCells[y][x] = new JPanel( new BorderLayout());

"PW", "PW", "PW", "PW", "PW", "PW"}, {"RW", "NW", "BW", "QW", "KW",

"BW", "NW", "RW"} };

private ImageIcon rookBlack = new ImageIcon(System. getProperty ("user.dir")

  • "/images/RookBlack.png"); private ImageIcon rookWhite = new ImageIcon(System. getProperty ("user.dir")
  • "/images/RookWhite.png"); private ImageIcon bishopBlack = new ImageIcon(System. getProperty ("user.dir") + "/images/BishopBlack.png"); private ImageIcon bishopWhite = new ImageIcon(System. getProperty ("user.dir") + "/images/BishopWhite.png"); private ImageIcon knightBlack = new ImageIcon(System. getProperty ("user.dir")
  • "/images/KnightBlack.png"); private ImageIcon knightWhite = new ImageIcon(System. getProperty ("user.dir") + "/images/KnightWhite.png"); private ImageIcon kingBlack = new ImageIcon(System. getProperty ("user.dir") + "/images/KingBlack.png"); private ImageIcon kingWhite = new ImageIcon(System. getProperty ("user.dir")
  • "/images/KingWhite.png"); private ImageIcon queenBlack = new ImageIcon(System. getProperty ("user.dir")
  • "/images/QueenBlack.png"); private ImageIcon queenWhite = new ImageIcon(System. getProperty ("user.dir")
  • "/images/QueenWhite.png"); private ImageIcon pawnBlack = new ImageIcon(System. getProperty ("user.dir")
  • "/images/PawnBlack.png"); private ImageIcon pawnWhite = new ImageIcon(System. getProperty ("user.dir")
  • "/images/PawnWhite.png"); privateboolean boolMoveSelection = false , bWhite = true , bMyTurn = true ; private Point pntMoveFrom, pntMoveTo; private Container c; }

Результат: Рис.2.