Question: Help to correct the below Java code. The problem with the code is with the 4 cards at start when I run the code, when

Help to correct the below Java code. The problem with the code is
with the 4 cards at start when I run the code, when I drag cards from the throwaway pile I can place a card over one of the 4 cards,
thats perfectly fine. But when I drag one of the 4 cards over a card from the throwaway pile and then click with the mouse cursor again somewhere else the card from the 4 cards will not stay on top over the card from the throwaway pile it goes beneath the card from the throwaway pile. I like to have the 4 cards stay on top also. @Override
public void mouseClicked(MouseEvent e){
Point mousePoint = e.getPoint();
// Check if the top card of the throwaway pile is clicked and select it if so.
if (!throwawayPile.isEmpty() && throwawayPile.get(throwawayPile.size()-1).contains(mousePoint)){
selectedCard = throwawayPile.get(throwawayPile.size()-1);
repaint(); // Repaint to reflect any changes in selection visually.
return; // Early return as we've handled the click on the throwaway pile.
}
// If we reach here, the click wasn't on the throwaway pile's top card.
// Now check if a new card is clicked in the deck or if no card is currently selected.
if (selectedCard == null ||!selectedCard.contains(mousePoint)){
for (int i = deck.size()-1; i >=4; i--){
if (deck.get(i).contains(mousePoint)){
Card clickedCard = deck.get(i);
// No need to check if the clicked card is in the throwaway pile here;
// the requirement seems to focus on deck interaction separate from throwaway pile.
// If the clicked card is not the currently selected card, update the selection
// and possibly move it to the throwaway pile.
if (!clickedCard.equals(selectedCard)){
selectedCard = clickedCard; // Update the selected card.
clickedCard.toggleDenomination();
// Assuming moveCardToThrowawayPile is a method that handles moving a card to
// the throwaway pile and possibly handles other logic like toggling denomination.
moveCardToThrowawayPile(clickedCard);
repaint(); // Repaint to show changes.
// Break as we've found and handled the clicked card.
//break;
return;
}
}
}
}
}
private void moveCardToThrowawayPile(Card card){
if (!throwawayPile.contains(card)){
card.move(getWidth()- card.width -105,8);
throwawayPile.add(card);
}
}
@Override
public void mousePressed(MouseEvent e){
Point mousePoint = e.getPoint();
// Reset selected card to null at the beginning of each press to ensure
// it's cleared from the previous operation.
selectedCard = null;
// Assuming the first four cards are at the start of the deck and are special.
// Check if any of the first four cards are clicked.
boolean cardSelected = false;
for (int i =0; i < Math.min(deck.size(),4); i++){
if (deck.get(i).contains(mousePoint)){
selectedCard = deck.get(i);
lastMousePosition = mousePoint;
cardSelected = true;
break; // Found the card, no need to continue checking.
}
}
// If none of the first four cards were selected, check the rest of the deck.
if (!cardSelected){
for (int i =4; i < deck.size(); i++){
if (deck.get(i).contains(mousePoint)){
selectedCard = deck.get(i);
lastMousePosition = mousePoint;
cardSelected = true;
break; // Found the card, no need to continue checking.
}
}
}
if (!cardSelected && !throwawayPile.isEmpty()){
// Correctly iterating from the top of the pile downwards.
for (int i = throwawayPile.size()-1; i >=0; i--){
if (throwawayPile.get(i).contains(mousePoint)){
// If any card in the throwaway pile is clicked, select that specific card.
selectedCard = throwawayPile.get(i);
lastMousePosition = mousePoint;
break; // Once a card in the throwaway pile is identified, no need to check further.
}
}
}
repaint(); // Call repaint after all conditions are checked.
}

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!