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 4 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();
addMouseListener(this);
addMouseMotionListener(this);
}
private void initializeDeck(){
deck.clear();
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;
for (String suit : suits){
for (String rank : ranks){
deck.add(new Card(xOffset, yOffset, cardWidth, cardHeight, Color.BLUE, rank, suit));
}
}
shuffleDeck();
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
for (Card card : deck){
if (!card.equals(selectedCard)){
card.draw(g);
}
}
for (Card card : throwawayPile){
if (!card.equals(selectedCard)){
card.draw(g);
}
}
if (selectedCard != null){
selectedCard.draw(g);
}
}
public void clear(){
if (deck.size()>=52){
deck.subList(0,52).clear();
repaint();
}
}
public void displayFourCards(){
clear();
initializeDeck();
CardUtil.displayFourCards(deck);
int componentCount = this.getComponentCount();
for (int i =0; i <4; i++){
if ((componentCount - i -1)>=0){
this.setComponentZOrder(this.getComponent(componentCount - i -1), i);
}
}
throwawayPile.clear();
repaint();
}
@Override
public void mouseClicked(MouseEvent e){
Point mousePoint = e.getPoint();
if (selectedCard == null ||!selectedCard.contains(mousePoint)){
for (int i = deck.size()-1; i >=4; i--){// Corrected the lower bound of the loop to include all cards.
if (deck.get(i).contains(mousePoint)){
Card clickedCard = deck.get(i);
if (!clickedCard.equals(selectedCard)){
selectedCard = clickedCard; // Update the selected card.
clickedCard.toggleDenomination();
moveCardToThrowawayPile(clickedCard);
deck.add(clickedCard);
repaint(); // Repaint to show changes.
break; // Correct use of return after handling the card click.
}
}
}
}
}
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();
selectedCard = null;
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;
}
}
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()){
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;
}
}
}
repaint();
}
@Override
public void mouseReleased(MouseEvent e){
if (selectedCard != null){
selectedCard = null;
}

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