I'm trying to make a chessboard with draggable chess pieces, I added a mouse motion listener, and every time it drags, the chess piece won't move. For example, if I dragged my white pawn to position A4, the pawn won't move and stay in place. Because I added the mouse motion listener and made the JLabel static, it removes the icon for the Jlabel (a black pawn) So adding a motion listener won't work.
My previous code,
import java.awt.EventQueue;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
public class Chessboard {
private JFrame frame;
private static List <String> blackFileLoc = new ArrayList<String>();
private static List <String> whiteFileLoc = new ArrayList<String>();
static int dp=0;
static int wps=0;
static Timer t;
static JLabel whitePawn = new JLabel();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Chessboard window = new Chessboard();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
* @throws IOException
*/
public Chessboard() throws IOException {
initialize();
}
/**
* Initialize the contents of the frame.
* @throws IOException
*/
private void initialize() throws IOException {
addFileLoc();
frame = new JFrame();
frame.getContentPane().setBackground(Color.PINK);
frame.setBounds(0, 0, 551, 440);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setResizable(false);
JPanel panel_2 = new JPanel();
panel_2.setBounds(401, 0, 150, 411);
frame.getContentPane().add(panel_2);
// WHITE PAWN C:\Users\USER\eclipse-workspace\CookieClicker\Images\whitepawn.png
Icon icon;
icon = new ImageIcon("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\12.png");
for (int wp=0; wp<=8*50; wp+=50) {
whitePawn.setIcon(icon);
whitePawn.setBounds(wp,50,50,50);
frame.getContentPane().add(whitePawn);
}
// BLACK PAWN
icon = new ImageIcon("C:\\\\Users\\\\USER\\\\eclipse-workspace\\\\CookieClicker\\\\chess images\\\\White Pawn.png");
for (int dp=0; dp<=8*50;dp+=50) {
JLabel blackPawn = new JLabel(icon);
blackPawn.setBounds(0+dp, 300, 50, 50);
frame.getContentPane().add(blackPawn);
}
for (dp=0; dp<=7;dp++) {
for (int i = 0; i<=8*50;i+=50) {
icon = new ImageIcon(blackFileLoc.get(dp));
JLabel blackPawn = new JLabel(icon);
blackPawn.setBounds(0+i, 0, 50, 50);
if (dp<=6) {
dp+=1;
}
frame.getContentPane().add(blackPawn);
}
}
// white pieces
for (wps=0; wps<=7;wps++) {
for (int i = 0; i<=8*50;i+=50) {
icon = new ImageIcon(whiteFileLoc.get(wps));
JLabel blackPawn = new JLabel(icon);
blackPawn.setBounds(0+i, 350, 50, 50);
if (wps<=6) {
wps+=1;
}
frame.getContentPane().add(blackPawn);
}
}
for (int y = 0; y<=3*100;y+=100) {
for (int x = 0; x<=3*100;x+=100) {
JPanel panel = new JPanel();
panel.setBackground(Color.PINK);
panel.setBounds(0+x, 0+y, 50, 50);
frame.getContentPane().add(panel);
}
for (int i=0; i<=6*50;i+=50) {
JPanel panel_1 = new JPanel();
panel_1.setBackground(Color.GRAY);
panel_1.setBounds(50+i, 0+y, 50, 50);
frame.getContentPane().add(panel_1);
}
}
for (int y = 0; y<=3*100;y+=100) {
for (int x = 0; x<=3*100;x+=100) {
JPanel panel_1 = new JPanel();
panel_1.setBackground(Color.GRAY);
panel_1.setBounds(0+x, 50+y, 50, 50);
frame.getContentPane().add(panel_1);
}
for (int i=0; i<=16*100;i+=100) {
JPanel panel = new JPanel();
panel.setBackground(Color.PINK);
panel.setBounds(50+y, 50+y, 50, 50);
frame.getContentPane().add(panel);
}
}
whitePawn.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
System.out.println("test");
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int) b.getX();
int y = (int) b.getY();
whitePawn.setBounds(x-50,y-50,50,50);
}
});
t = new Timer(1,(ActionEvent e)->{
whitePawn.setBounds(whitePawn.getX(),whitePawn.getY(),whitePawn.getWidth(),whitePawn.getHeight());
});
t.start();
}
public void addFileLoc() {
// first step
// second step
blackFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\Black Rhook.png");
blackFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\Black Knight.png");
blackFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\Black Bishop.png");
blackFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\Black King.png");
blackFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\Black Queen.png");
//this reverse the file loc
for (int i=2;i>=0;i--) {
blackFileLoc.add(blackFileLoc.get(i));
}
whiteFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\White Rhook.png");
whiteFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\White Knight.png");
whiteFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\White Bishop.png");
whiteFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\White Queen.png");
whiteFileLoc.add("C:\\Users\\USER\\eclipse-workspace\\CookieClicker\\chess images\\White King.png");
//this reverse the file loc
for (int i=2;i>=0;i--) {
whiteFileLoc.add(whiteFileLoc.get(i));
}
}
}
Now if we add the mouse motion listener, this is the code the icon won't appear black pawn, Can someone give me a way to make it so the pieces will be draggable?