Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Help to add functions in the java code below, a card holder in the middle of the four cards in the panel, so when you
Help to add functions in the java code below, a card holder in the middle
of the four cards in the panel, so when you draw a card from the card pile
up to the right.please see added pic towards the center of the four cards in
the panel, the cards will click into the center of the four cards in the panel. If it's possible to exnted from the below code given, Not possible to send the whole code because Chegg does not take more characters Please let me know if you need more of the program code, if it's possble to communicate over a task. Thanks! import java.awt.;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
abstract class CardPileMiddle extends JPanel implements MouseListener, MouseMotionListener
private final int width;
private final int height;
private Color color;
private Card card;
public CardPileMiddleint x int y int width, int height, Color color
this.setBoundsx y width, height;
this.width width;
this.height height;
this.color color;
this.addMouseListenerthis;
this.addMouseMotionListenerthis;
@Override
public void paintComponentGraphics g
super.paintComponentg;
gsetColorcolor;
gdrawRect width, height;
if card null
card.drawg;
@Override
public void mouseClickedMouseEvent e
@Override
public void mousePressedMouseEvent e
@Override
public void mouseEnteredMouseEvent e
@Override
public void mouseExitedMouseEvent e
@Override
public void mouseMovedMouseEvent e
class DrawingPanel extends JPanel implements MouseListener, MouseMotionListener
private final ArrayList deck;
private final ArrayList throwawayPile;
private Card selectedCard;
private Point lastMousePosition;
private final CardPile newPile;
private Card card;
private int x;
private int y;
private int width;
private int height;
public DrawingPanel
deck new ArrayList;
throwawayPile new ArrayList;
addMouseListenerthis;
addMouseMotionListenerthis;
clear;
int width ;
int height ;
CardPileMiddle centerPile new CardPileMiddle Color.YELLOW
@Override
public void mouseDraggedMouseEvent e
@Override
public void mouseReleasedMouseEvent e
;
newPile new CardPile Color.GRAY
@Override
public boolean dropCardIfContainsPoint mousePoint, Card card
return false;
@Override
public void placeCardCard card
;
this.addcenterPile;
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.BLACK, rank, suit;
shuffleDeck;
@Override
protected void paintComponentGraphics g
super.paintComponentg;
for Card card : deck
if card.equalsselectedCard
card.drawg;
for Card card : deck
card.drawg;
if selectedCard null
selectedCard.drawg;
for Card card : throwawayPile
if card.equalsselectedCard
card.drawg;
if selectedCard null
selectedCard.drawg;
newPile.drawg;
public void moveCardToNewPositionCard card, int x int y
this.card card;
this.x x;
this.y y;
if card null
card.movex y;
public void clear
if decksize
deck.subListclear;
repaint;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started