Question: Please help to make the shuffle method work as it is now only the suits are shuffled, like to have all 4 suits shuffles with

Please help to make the shuffle method work as it is now only the suits are shuffled, like to have all 4 suits shuffles with each other. Please send the code back with the changes in it in whole. Thanks! class DrawingPanel extends JPanel implements MouseListener, MouseMotionListener {
private final ArrayList deck; // List to store the deck of cards
private Card selectedCard; // Currently selected card
private Point lastMousePosition; // Last mouse position used for dragging
public DrawingPanel(){
deck = new ArrayList<>();
// Creating a standard deck of cards
String[] suits ={"Hj","Ru","Kl","Sp"};
String[] ranks ={"Ess","2","3","4","5","6","7","8","9","10","Kn","Q","K"};
int xOffset =50;
int yOffset =50;
int cardWidth =50;
int cardHeight =70;
// Create cards for each suit and rank
for (String suit : suits){
for (String rank : ranks){
deck.add(new Card(xOffset, yOffset, cardWidth, cardHeight, Color.BLUE, rank, suit));
xOffset +=20; // Adjusting x position for the next card
}
xOffset =50; // Resetting x position for the next suit
yOffset +=0; // Adjusting y position for the next suit
}
// Register mouse listeners
addMouseListener(this);
addMouseMotionListener(this);
}
// Method to paint the panel
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
for (Card card : deck){
card.draw(g); // Draw each card in the deck
}
}
// MouseListener methods
@Override
public void mouseClicked(MouseEvent e){}
@Override
public void mousePressed(MouseEvent e){
Point mousePoint = e.getPoint();
// Find the topmost card that contains the mouse click
for (int i = deck.size()-1; i >=0; i--){
if (deck.get(i).contains(mousePoint)){
selectedCard = deck.get(i); // Set the selected card
lastMousePosition = mousePoint; // Store the mouse position for dragging
Card temp = deck.get(i);
deck.remove(i); // Remove the card from its current position
deck.add(temp); // Add the card to the end of the list (topmost position)
break;
}
}
}
@Override
public void mouseReleased(MouseEvent e){}
@Override
public void mouseEntered(MouseEvent e){}
@Override
public void mouseExited(MouseEvent e){}
// MouseMotionListener methods
@Override
public void mouseDragged(MouseEvent e){
if (selectedCard != null){
if (selectedCard.contains(e.getPoint())){
// Calculate the movement distance
int dx = e.getX()- lastMousePosition.x;
int dy = e.getY()- lastMousePosition.y;
// Move the selected card
selectedCard.move(dx, dy);
lastMousePosition = e.getPoint(); // Update the last mouse position
repaint(); // Repaint the panel to reflect the changes
}
}
}
@Override
public void mouseMoved(MouseEvent e){}
// Method to shuffle the deck
public void shuffleDeck(){
Collections.shuffle(deck); // Shuffle the deck
repaint(); // Repaint the panel to reflect the shuffled deck
}
}

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 Accounting Questions!