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