Question: Please help to implement the out comment code or rearrange the bleow Java code so it's possible to click and grab the cards and move
Please help to implement the out comment code or rearrange the bleow Java code so it's possible to click and grab the cards and move around them in the panel. Please send back the code with the changes in it in whole. Thanks! import javax.swing.;
import java.awt.;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collections;
class CardGame extends JFrame
private JPanel cardPanel;
private ArrayList deck;
private Card selectedCard;
public CardGame
setTitleSolitaire Card Game";
setSize;
setDefaultCloseOperationJFrameEXITONCLOSE;
deck new ArrayList;
initializeDeck;
cardPanel new JPanel
@Override
protected void paintComponentGraphics g
super.paintComponentg;
int x ;
int y ;
for Card card : deck
if cardisFaceUp
gsetColorColorWHITE;
gfillRectx y;
gsetColorColorBLACK;
gdrawRectx y;
gdrawStringcardgetValue of card.getSuit x y ;
else
gsetColorColorBLUE;
gfillRectx y;
gsetColorColorWHITE;
gdrawRectx y;
x ;
y ;
;
cardPanel.addMouseListenernew MouseAdapter
@Override
public void mouseClickedMouseEvent e
int x ;
int y ;
for Card card : deck
if egetX x && egetX x && egetY y && egetY y
card.setFaceUpcard.isFaceUp;
cardPanel.repaint;
break;
x ;
y ;
;
addcardPanel;
setVisibletrue;
private void initializeDeck
String suits Hearts "Diamonds", "Clubs", "Spades";
String values JQKA;
for String suit : suits
for String value : values
deck.addnew Cardsuit value, false;
Collections.shuffledeck;
public static void mainString args
new CardGame;
public class Card
private String suit;
private String value;
private boolean faceUp;
public CardString suit, String value, boolean faceUp
this.suit suit;
this.value value;
this.faceUp faceUp;
public String getSuit
return suit;
public String getValue
return value;
public boolean isFaceUp
return faceUp;
public void setFaceUpboolean faceUp
this.faceUp faceUp;
cardPanel.addMouseListenernew MouseAdapter
private int xOffset;
private int yOffset;
@Override
public void mousePressedMouseEvent e
int x ;
int y ;
for Card card : deck
if egetX x && egetX x && egetY y && egetY y && card.isFaceUp
selectedCard card;
xOffset egetX x;
yOffset egetY y;
break;
x ;
y ;
@Override
public void mouseDraggedMouseEvent e
if selectedCard null
selectedCard.setXegetX xOffset;
selectedCard.setYegetY yOffset;
cardPanel.repaint;
@Override
public void mouseReleasedMouseEvent e
selectedCard null;
;
public void setXint x
Update the x coordinate of the card
public void setYint y
Update the y coordinate of the card
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
