Question: Help to add a function in the below Java method so the display of 4 cards stays on top over the cards from the throwaway
Help to add a function in the below Java method so the display of cards stays on top over the cards from the throwaway pile when one of the four cards is dragged and placed over a card from the throwaway pile in the panel and also when a throwaway card dragged back it will stay on top as the top card. class DrawingPanel extends JPanel implements MouseListener, MouseMotionListener
private final ArrayList deck;
private final ArrayList throwawayPile;
private Card selectedCard;
private Point lastMousePosition;
public DrawingPanel
deck new ArrayList;
throwawayPile new ArrayList;
initializeDeck;
addMouseListenerthis;
addMouseMotionListenerthis;
private void initializeDeck
deck.clear;
String suits HjRuKlSp;
String ranks EssKnQK;
int xOffset ;
int yOffset ;
int cardWidth ;
int cardHeight ;
for String suit : suits
for String rank : ranks
deck.addnew CardxOffset yOffset, cardWidth, cardHeight, Color.BLUE, rank, suit;
shuffleDeck;
@Override
protected void paintComponentGraphics g
super.paintComponentg;
for Card card : deck
if card.equalsselectedCard
card.drawg;
for Card card : throwawayPile
if card.equalsselectedCard
card.drawg;
if selectedCard null
selectedCard.drawg;
public void clear
if decksize
deck.subListclear;
repaint;
public void displayFourCards
clear;
initializeDeck;
CardUtil.displayFourCardsdeck;
int componentCount this.getComponentCount;
for int i ; i ; i
if componentCount i
this.setComponentZOrderthisgetComponentcomponentCount i i;
throwawayPile.clear;
repaint;
@Override
public void mouseClickedMouseEvent e
Point mousePoint egetPoint;
if selectedCard null selectedCard.containsmousePoint
for int i deck.size; i ; i Corrected the lower bound of the loop to include all cards.
if deckgeticontainsmousePoint
Card clickedCard deck.geti;
if clickedCard.equalsselectedCard
selectedCard clickedCard; Update the selected card.
clickedCard.toggleDenomination;
moveCardToThrowawayPileclickedCard;
deck.addclickedCard;
repaint; Repaint to show changes.
break; Correct use of return after handling the card click.
private void moveCardToThrowawayPileCard card
if throwawayPile.containscard
card.movegetWidth card.width ;
throwawayPile.addcard;
@Override
public void mousePressedMouseEvent e
Point mousePoint egetPoint;
selectedCard null;
boolean cardSelected false;
for int i ; i Math.mindecksize; i
if deckgeticontainsmousePoint
selectedCard deck.geti;
lastMousePosition mousePoint;
cardSelected true;
break;
if cardSelected
for int i ; i deck.size; i
if deckgeticontainsmousePoint
selectedCard deck.geti;
lastMousePosition mousePoint;
cardSelected true;
break; Found the card, no need to continue checking.
if cardSelected && throwawayPile.isEmpty
for int i throwawayPile.size; i ; i
if throwawayPilegeticontainsmousePoint
If any card in the throwaway pile is clicked, select that specific card.
selectedCard throwawayPile.geti;
lastMousePosition mousePoint;
break;
repaint;
@Override
public void mouseReleasedMouseEvent e
if selectedCard null
selectedCard null;
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
