Question: Help with add a function in the below Java code, so when click on a card and then click in the panel, the card moves

Help with add a function in the below Java code, so when click on a card and then click in the panel, the card moves to that place. class DrawingPanel extends JPanel implements MouseListener, MouseMotionListener {
private final ArrayList deck;
private final ArrayList throwawayPile;
private Card selectedCard;
private Point lastMousePosition;
private final CardPile newPile;
private Card card;
private int width;
private int height;
public DrawingPanel(){
deck = new ArrayList<>();
throwawayPile = new ArrayList<>();
addMouseListener(this);
addMouseMotionListener(this);
clear();
// int width =0;
// int height =0;
CardPileMiddle centerPile = new CardPileMiddle(275,150,150,200, Color.YELLOW){
@Override
public void mouseDragged(MouseEvent e){
}
@Override
public void mouseReleased(MouseEvent e){
}
};
newPile = new CardPile(500,500,0,0, Color.GRAY){
@Override
public boolean dropCardIfContains(Point mousePoint, Card card){
return false;
}
@Override
public void placeCard(Card card){
}
};
this.add(centerPile);
}
private void initializeDeck(){
deck.clear();
String[] suits ={"Hj","Ru","Kl","Sp"};
String[] ranks ={"Ess","2","3","4","5","6","7","8","9","10","Kn","Q","K"};
int xOffset =250;
int yOffset =5;
int cardWidth =50;
int cardHeight =70;
for (String suit : suits){
for (String rank : ranks){
deck.add(new Card(xOffset, yOffset, cardWidth, cardHeight, Color.BLACK, rank, suit));
}
}
shuffleDeck();
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
for (Card card : deck){
if (!card.equals(selectedCard)){
card.draw(g);
}
}
for (Card card : throwawayPile){
if (!card.equals(selectedCard)){
card.draw(g);
}
}
if (selectedCard != null){
selectedCard.draw(g);
newPile.draw(g);
}
}
public void clear(){
if (deck.size()>=52){
deck.subList(0,52).clear();
repaint();
}
}
public void displayFourCards(){
clear();
initializeDeck();
CardUtil.displayFourCards(deck);
int componentCount = this.getComponentCount();
for (int i =0; i <4; i++){
if ((componentCount - i -1)>=0){
this.setComponentZOrder(this.getComponent(componentCount - i -1), i);
}
}
throwawayPile.clear();
repaint();
}
@Override
public void mouseClicked(MouseEvent e){
Point mousePoint = e.getPoint();
if (newPile.contains(mousePoint)){
newPile.removeTopCard();
repaint();
return;
}
if (selectedCard == null ||!selectedCard.contains(mousePoint)){
for (int i = deck.size()-1; i >=4; i--){
if (deck.get(i).contains(mousePoint)){
Card clickedCard = deck.get(i);
if (!clickedCard.equals(selectedCard)){
selectedCard = clickedCard;
clickedCard.toggleDenomination();
moveCardToThrowawayPile(clickedCard);
deck.remove(i);
//deck.add(clickedCard);
repaint();
break;
}
}
}
}
if (newPile.contains(mousePoint)){
newPile.removeTopCard();
repaint();
return;
}
for (int i = throwawayPile.size()-1; i >=0; i--){
if (throwawayPile.get(i).contains(mousePoint)){
selectedCard = throwawayPile.get(i);
lastMousePosition = mousePoint;
newPile.moveTopCardTo(lastMousePosition);
lastMousePosition = null;
repaint();
return;
}
}
if (lastMousePosition != null){
newPile.moveTopCardTo(lastMousePosition);
lastMousePosition = null;
repaint();
}
if (selectedCard != null){
if (!newPile.contains(e.getPoint())){
selectedCard.move(e.getX()- selectedCard.width /2, e.getY()- selectedCard.height /2);
} else {
newPile.addCard(selectedCard);
throwawayPile.remove(selectedCard);
}
selectedCard = null;
repaint();
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!