






Учись благодаря многочисленным ресурсам, которые есть на 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());
private ImageIcon rookBlack = new ImageIcon(System. getProperty ("user.dir")
Результат: Рис.2.