I am currently making the N Queens game and despite the fact that I have managed to generate the graphics that represent the queens. My problem is that I want to add these pieces dynamically since I will let the user select the number of queens he wants to place on the board, my problem arises when I want to add the MouseDragged method to each piece on the board but the problem is that I have only been able to add them manually and I would like to add them dynamically or to know if there is another way to do what I want.
public class OtraPrueba extends JFrame {
JPanel jpanel = (JPanel) this.getContentPane();
JLabel label[] = new JLabel[8];
Border border = BorderFactory.createLineBorder(Color.black, 1);
int N = 0, recX, recY;
public static void main(String[] args) {
OtraPrueba op = new OtraPrueba();
op.setBounds(0, 0, 500, 500);
op.setVisible(true);
op.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public OtraPrueba() {
jpanel.setLayout(null);
jpanel.setBackground(Color.CYAN);
for (int i = 0; i < label.length; i++) {
label[i] = new JLabel();
label[i].setBounds(25 + (50 * i), 25, 30, 30);
label[i].setText("Q" + (i + 1));
label[i].setBorder(border);
label[i].setHorizontalAlignment(SwingConstants.CENTER);
jpanel.add(label[i], null);
}
label[0].addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
@Override
public void mouseDragged(java.awt.event.MouseEvent evt) {
j1MouseDragged(evt);
}
});
label[1].addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
@Override
public void mouseDragged(java.awt.event.MouseEvent evt) {
j2MouseDragged(evt);
}
});
label[2].addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
@Override
public void mouseDragged(java.awt.event.MouseEvent evt) {
j3MouseDragged(evt);
}
});
label[3].addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
@Override
public void mouseDragged(java.awt.event.MouseEvent evt) {
j4MouseDragged(evt);
}
});
label[4].addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
@Override
public void mouseDragged(java.awt.event.MouseEvent evt) {
j5MouseDragged(evt);
}
});
label[5].addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
@Override
public void mouseDragged(java.awt.event.MouseEvent evt) {
j6MouseDragged(evt);
}
});
label[6].addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
@Override
public void mouseDragged(java.awt.event.MouseEvent evt) {
j7MouseDragged(evt);
}
});
label[7].addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
@Override
public void mouseDragged(java.awt.event.MouseEvent evt) {
j8MouseDragged(evt);
}
});
}
public void j1MouseDragged(java.awt.event.MouseEvent evt) {
label[0].setLocation(newPosition(evt)[0], newPosition(evt)[1]);
}
public void j2MouseDragged(java.awt.event.MouseEvent evt) {
label[1].setLocation(newPosition(evt)[0], newPosition(evt)[1]);
}
public void j3MouseDragged(java.awt.event.MouseEvent evt) {
label[2].setLocation(newPosition(evt)[0], newPosition(evt)[1]);
}
public void j4MouseDragged(java.awt.event.MouseEvent evt) {
label[3].setLocation(newPosition(evt)[0], newPosition(evt)[1]);
}
public void j5MouseDragged(java.awt.event.MouseEvent evt) {
label[4].setLocation(newPosition(evt)[0], newPosition(evt)[1]);
}
public void j6MouseDragged(java.awt.event.MouseEvent evt) {
label[5].setLocation(newPosition(evt)[0], newPosition(evt)[1]);
}
public void j7MouseDragged(java.awt.event.MouseEvent evt) {
label[6].setLocation(newPosition(evt)[0], newPosition(evt)[1]);
}
public void j8MouseDragged(java.awt.event.MouseEvent evt) {
label[7].setLocation(newPosition(evt)[0], newPosition(evt)[1]);
}
public int[] newPosition(MouseEvent evt) {
int newX, newY;
if (evt.getXOnScreen() <= 25) {
newX = 25;
} else if (evt.getXOnScreen() <= 75) {
newX = 75;
} else if (evt.getXOnScreen() <= 125) {
newX = 125;
} else if (evt.getXOnScreen() <= 175) {
newX = 175;
} else if (evt.getXOnScreen() <= 225) {
newX = 225;
} else if (evt.getXOnScreen() <= 275) {
newX = 275;
} else if (evt.getXOnScreen() <= 325) {
newX = 325;
} else if (evt.getXOnScreen() <= 375) {
newX = 375;
} else {
newX = 375;
}
if (evt.getYOnScreen() <= 25) {
newY = 25;
} else if (evt.getYOnScreen() <= 75) {
newY = 75;
} else if (evt.getYOnScreen() <= 125) {
newY = 125;
} else if (evt.getYOnScreen() <= 175) {
newY = 175;
} else if (evt.getYOnScreen() <= 225) {
newY = 225;
} else if (evt.getYOnScreen() <= 275) {
newY = 275;
} else if (evt.getYOnScreen() <= 325) {
newY = 325;
} else if (evt.getYOnScreen() <= 375) {
newY = 375;
} else {
newY = 375;
}
recX = newX;
recY = newY;
int retorno[] = {newX, newY};
return retorno;
}
}
You can, initially, apply each listener inside the loop in the same way as dynamically setting the location/size, text, border and alignment of each
JLabel
:After that it
mouseDragged
will have the method that receives the argumentMouseEvent
, but here the difference is that it will only be a single method for any number of components and on top of that it will be able to identify which of those components ( in this caseJLabel
) registered the drag event.With the above you unsubscribe a good handful of lines of code.