Friday, November 29, 2013

Chest

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.*;
  4. import javax.swing.*;
  5. public class ChessGameDemo extends JFrame implements MouseListener, MouseMotionListener {
  6. JLayeredPane layeredPane;
  7. JPanel chessBoard;
  8. JLabel chessPiece;
  9. int xAdjustment;
  10. int yAdjustment;
  11. public ChessGameDemo(){
  12. Dimension boardSize = new Dimension(600, 600);
  13. // Use a Layered Pane for this this application
  14. layeredPane = new JLayeredPane();
  15. getContentPane().add(layeredPane);
  16. layeredPane.setPreferredSize(boardSize);
  17. layeredPane.addMouseListener(this);
  18. layeredPane.addMouseMotionListener(this);
  19.  
  20. //Add a chess board to the Layered Pane
  21. chessBoard = new JPanel();
  22. layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
  23. chessBoard.setLayout( new GridLayout(8, 8) );
  24. chessBoard.setPreferredSize( boardSize );
  25. chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);
  26. for (int i = 0; i < 64; i++) {
  27. JPanel square = new JPanel( new BorderLayout() );
  28. chessBoard.add( square );
  29. int row = (i / 8) % 2;
  30. if (row == 0)
  31. square.setBackground( i % 2 == 0 ? Color.blue : Color.white );
  32. else
  33. square.setBackground( i % 2 == 0 ? Color.white : Color.blue );
  34. }
  35. //Add a few pieces to the board
  36. JLabel piece = new JLabel( new ImageIcon("/home/vinod/amarexamples/chess.jpg") );
  37. JPanel panel = (JPanel)chessBoard.getComponent(0);
  38. panel.add(piece);
  39. piece = new JLabel(new ImageIcon("/home/vinod/amarexamples/chess1.jpg"));
  40. panel = (JPanel)chessBoard.getComponent(15);
  41. panel.add(piece);
  42. piece = new JLabel(new ImageIcon("/home/vinod/amarexamples/king.jpg"));
  43. panel = (JPanel)chessBoard.getComponent(16);
  44. panel.add(piece);
  45. piece = new JLabel(new ImageIcon("/home/vinod/amarexamples/camel.jpg"));
  46. panel = (JPanel)chessBoard.getComponent(20);
  47. panel.add(piece);
  48.  
  49. }
  50. public void mousePressed(MouseEvent e){
  51. chessPiece = null;
  52. Component c = chessBoard.findComponentAt(e.getX(), e.getY());
  53. if (c instanceof JPanel)
  54. return;
  55. Point parentLocation = c.getParent().getLocation();
  56. xAdjustment = parentLocation.x - e.getX();
  57. yAdjustment = parentLocation.y - e.getY();
  58. chessPiece = (JLabel)c;
  59. chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
  60. chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
  61. layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
  62. }
  63. //Move the chess piece around
  64. public void mouseDragged(MouseEvent me) {
  65. if (chessPiece == null) return;
  66. chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
  67. }
  68. //Drop the chess piece back onto the chess board
  69. public void mouseReleased(MouseEvent e) {
  70. if(chessPiece == null) return;
  71. chessPiece.setVisible(false);
  72. Component c = chessBoard.findComponentAt(e.getX(), e.getY());
  73. if (c instanceof JLabel){
  74. Container parent = c.getParent();
  75. parent.remove(0);
  76. parent.add( chessPiece );
  77. }
  78. else {
  79. Container parent = (Container)c;
  80. parent.add( chessPiece );
  81. }
  82. chessPiece.setVisible(true);
  83. }
  84. public void mouseClicked(MouseEvent e) {
  85. }
  86. public void mouseMoved(MouseEvent e) {
  87. }
  88. public void mouseEntered(MouseEvent e){
  89. }
  90. public void mouseExited(MouseEvent e) {
  91. }
  92. public static void main(String[] args) {
  93. JFrame frame = new ChessGameDemo();
  94. frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE );
  95. frame.pack();
  96. frame.setResizable(true);
  97. frame.setLocationRelativeTo( null );
  98. frame.setVisible(true);
  99. }
  100. }
  101.  

No comments:

Post a Comment