Question: Please help with inplement in the Java code function so when you click on a card and click again somewhere else in the panel, the
Please help with inplement in the Java code function so when you click on a card and click again somewhere else in the panel, the card moves there.
class Shape
int x y;
int width, height;
Color color;
public Shapeint x int y int width, int height, Color shapeColor
this.x x;
this.y y;
this.width width;
this.height height;
this.color shapeColor;
public void drawGraphics g
gsetColorcolor;
gfillRectx y width, height;
public boolean containsPoint point
return point.x x && point.x x width && point.y y && point.y y height;
public void moveint dx int dy
x dx;
y dy;
class Card extends Shape
private final String rank;
private final String suit;
private boolean showDenomination;
public Cardint x int y int width, int height, Color shapeColor, String rank, String suit
superx y width, height, shapeColor;
this.rank rank;
this.suit suit;
this.showDenomination false;
@Override
public void drawGraphics g
super.drawg;
if showDenomination
gsetColorColorWHITE;
gdrawStringrank suit, x y ;
public void toggleDenomination
showDenomination showDenomination;
static class DrawingPanel extends JPanel implements MouseListener, MouseMotionListener
private final ArrayList deck;
private Card selectedCard;
private Point lastMousePosition;
public DrawingPanel
deck new ArrayList;
String suits HjRuKlSp;
List allCards getStringssuits;
Collections.shuffleallCards;
int xOffset ;
int yOffset ;
int cardWidth ;
int cardHeight ;
for int i ; i ; i
String cardInfo allCards.geti;
String parts cardInfo.split of ;
String rank parts;
String suit parts;
deck.addnew CardxOffset yOffset, cardWidth, cardHeight, Color.BLUE, rank, suit;
xOffset ;
if i
xOffset ;
yOffset ;
addMouseListenerthis;
addMouseMotionListenerthis;
private static List getStringsString suits
String ranks EssKnQK;
List allCards new ArrayList;
for String suit : suits
for String rank : ranks
allCards.addrank of suit;
return allCards;
@Override
protected void paintComponentGraphics g
super.paintComponentg;
for Card card : deck
card.drawg;
@Override
public void mouseClickedMouseEvent e
Point mousePoint egetPoint;
for int i deck.size; i ; i
if deckgeticontainsmousePoint
deck.getitoggleDenomination;
repaint;
break;
@Override
public void mousePressedMouseEvent e
Point mousePoint egetPoint;
for int i deck.size; i ; i
if deckgeticontainsmousePoint
selectedCard deck.geti;
lastMousePosition mousePoint;
Card temp deck.geti;
deck.removei;
deck.addtemp;
break;
@Override
public void mouseReleasedMouseEvent e
@Override
public void mouseEnteredMouseEvent e
@Override
public void mouseExitedMouseEvent e
@Override
public void mouseDraggedMouseEvent e
if selectedCard null
if selectedCardcontainsegetPoint
int dx egetX lastMousePosition.x;
int dy egetY lastMousePosition.y;
selectedCard.movedx dy;
lastMousePosition egetPoint;
repaint;
@Override
public void mouseMovedMouseEvent e
public void shuffleDeck
Collections.shuffledeck;
repaint;
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
