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 Shape(int 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 draw(Graphics g){
g.setColor(color);
g.fillRect(x, y, width, height);
}
public boolean contains(Point point){
return point.x >= x && point.x <= x + width && point.y >= y && point.y <= y + height;
}
public void move(int dx, int dy){
x += dx;
y += dy;
}
}
class Card extends Shape {
private final String rank;
private final String suit;
private boolean showDenomination;
public Card(int x, int y, int width, int height, Color shapeColor, String rank, String suit){
super(x, y, width, height, shapeColor);
this.rank = rank;
this.suit = suit;
this.showDenomination = false;
}
@Override
public void draw(Graphics g){
super.draw(g);
if (showDenomination){
g.setColor(Color.WHITE);
g.drawString(rank +""+ suit, x +5, y +15);
}
}
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 ={"Hj","Ru","Kl","Sp"};
List allCards = getStrings(suits);
Collections.shuffle(allCards);
int xOffset =50;
int yOffset =50;
int cardWidth =50;
int cardHeight =70;
for (int i =0; i <52; i++){
String cardInfo = allCards.get(i);
String[] parts = cardInfo.split(" of ");
String rank = parts[0];
String suit = parts[1];
deck.add(new Card(xOffset, yOffset, cardWidth, cardHeight, Color.BLUE, rank, suit));
xOffset +=0;
if ((i +1)%13==0){
xOffset =30;
yOffset +=0;
}
}
addMouseListener(this);
addMouseMotionListener(this);
}
private static List getStrings(String[] suits){
String[] ranks ={"Ess","2","3","4","5","6","7","8","9","10","Kn","Q","K"};
List allCards = new ArrayList<>();
for (String suit : suits){
for (String rank : ranks){
allCards.add(rank +" of "+ suit);
}
}
return allCards;
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
for (Card card : deck){
card.draw(g);
}
}
@Override
public void mouseClicked(MouseEvent e){
Point mousePoint = e.getPoint();
for (int i = deck.size()-1; i >=0; i--){
if (deck.get(i).contains(mousePoint)){
deck.get(i).toggleDenomination();
repaint();
break;
}
}
}
@Override
public void mousePressed(MouseEvent e){
Point mousePoint = e.getPoint();
for (int i = deck.size()-1; i >=0; i--){
if (deck.get(i).contains(mousePoint)){
selectedCard = deck.get(i);
lastMousePosition = mousePoint;
Card temp = deck.get(i);
deck.remove(i);
deck.add(temp);
break;
}
}
}
@Override
public void mouseReleased(MouseEvent e){
}
@Override
public void mouseEntered(MouseEvent e){
}
@Override
public void mouseExited(MouseEvent e){
}
@Override
public void mouseDragged(MouseEvent e){
if (selectedCard != null){
if (selectedCard.contains(e.getPoint())){
int dx = e.getX()- lastMousePosition.x;
int dy = e.getY()- lastMousePosition.y;
selectedCard.move(dx, dy);
lastMousePosition = e.getPoint();
repaint();
}
}
}
@Override
public void mouseMoved(MouseEvent e){
}
public void shuffleDeck(){
Collections.shuffle(deck);
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 Programming Questions!