Drag end Drop Dama

 

 

La classe: ChessBoard

1 package chessboard;
  2 
  3 import java.awt.*;
  4 import java.awt.event.*;
  5 import java.net.URL;
  6 //import java.util.*;
  7 import javax.swing.*;
  8 
  9 public class ChessBoard extends JFrame implements MouseListener, MouseMotionListener
 10 {
 11     JLayeredPane layeredPane;
 12     JPanel chessBoard;
 13     JLabel chessPiece;
 14     int xAdjustment;
 15     int yAdjustment;
 16 
 17     public ChessBoard()
 18     {
 19         Dimension boardSize = new Dimension(600, 600);
 20 
 21         //  Use a Layered Pane for this this application
 22 
 23         layeredPane = new JLayeredPane();
 24         layeredPane.setPreferredSize( boardSize );
 25         layeredPane.addMouseListener( this );
 26         layeredPane.addMouseMotionListener( this );
 27         getContentPane().add(layeredPane);
 28 
 29         //  Add a chess board to the Layered Pane
 30 
 31         chessBoard = new JPanel();
 32         chessBoard.setLayout( new GridLayout(8, 8) );
 33         chessBoard.setPreferredSize( boardSize );
 34         chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);
 35         layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
 36 
 37         //  Build the Chess Board squares
 38 
 39         for (int i = 0; i < 8; i++)
 40         {
 41             for (int j = 0; j < 8; j++)
 42             {
 43                 JPanel square = new JPanel( new BorderLayout() );
 44                 square.setBackground( (i + j) % 2 == 0 ? Color.red : Color.white );
 45                 chessBoard.add( square );
 46             }
 47         }
 48 
 49         // Add a few pieces to the board
 50 
 51         URL imgURL = getClass().getResource("/image/drag03.png");
 52          ImageIcon duke = new ImageIcon(imgURL);
 53         //ImageIcon duke = new ImageIcon("image/drag03.png"); // add an image here
 54 
 55         JLabel piece = new JLabel( duke );
 56         JPanel panel = (JPanel)chessBoard.getComponent( 0 );
 57         panel.add( piece );
 58         piece = new JLabel( duke );
 59         panel = (JPanel)chessBoard.getComponent( 15 );
 60         panel.add( piece );
 61     }
 62 
 63     /*
 64     **  Add the selected chess piece to the dragging layer so it can be moved
 65     */
 66     public void mousePressed(MouseEvent e)
 67     {
 68         chessPiece = null;
 69         Component c =  chessBoard.findComponentAt(e.getX(), e.getY());
 70 
 71         if (c instanceof JPanel) return;
 72 
 73         Point parentLocation = c.getParent().getLocation();
 74         xAdjustment = parentLocation.x - e.getX();
 75         yAdjustment = parentLocation.y - e.getY();
 76         chessPiece = (JLabel)c;
 77         chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
 78 
 79         layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
 80         layeredPane.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
 81     }
 82 
 83     /*
 84     **  Move the chess piece around
 85     */
 86     public void mouseDragged(MouseEvent me)
 87     {
 88         if (chessPiece == null) return;
 89 
 90         //  The drag location should be within the bounds of the chess board
 91 
 92         int x = me.getX() + xAdjustment;
 93         int xMax = layeredPane.getWidth() - chessPiece.getWidth();
 94         x = Math.min(x, xMax);
 95         x = Math.max(x, 0);
 96 
 97         int y = me.getY() + yAdjustment;
 98         int yMax = layeredPane.getHeight() - chessPiece.getHeight();
 99         y = Math.min(y, yMax);
100         y = Math.max(y, 0);
101 
102         chessPiece.setLocation(x, y);
103      }
104 
105     /*
106     **  Drop the chess piece back onto the chess board
107     */
108     public void mouseReleased(MouseEvent e)
109     {
110         layeredPane.setCursor(null);
111 
112         if (chessPiece == null) return;
113 
114         //  Make sure the chess piece is no longer painted on the layered pane
115 
116         chessPiece.setVisible(false);
117         layeredPane.remove(chessPiece);
118         chessPiece.setVisible(true);
119 
120         //  The drop location should be within the bounds of the chess board
121 
122         int xMax = layeredPane.getWidth() - chessPiece.getWidth();
123         int x = Math.min(e.getX(), xMax);
124         x = Math.max(x, 0);
125 
126         int yMax = layeredPane.getHeight() - chessPiece.getHeight();
127         int y = Math.min(e.getY(), yMax);
128         y = Math.max(y, 0);
129 
130         Component c =  chessBoard.findComponentAt(x, y);
131 
132         if (c instanceof JLabel)
133         {
134             Container parent = c.getParent();
135             parent.remove(0);
136             parent.add( chessPiece );
137             parent.validate();
138         }
139         else
140         {
141             Container parent = (Container)c;
142             parent.add( chessPiece );
143             parent.validate();
144         }
145     }
146 
147     public void mouseClicked(MouseEvent e) {}
148     public void mouseMoved(MouseEvent e) {}
149     public void mouseEntered(MouseEvent e) {}
150     public void mouseExited(MouseEvent e) {}
151 
152     public static void main(String[] args)
153     {
154         JFrame frame = new ChessBoard();
155         frame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
156         frame.setResizable( false );
157         frame.pack();
158         frame.setLocationRelativeTo( null );
159         frame.setVisible(true);
160      }
161 }