Question: 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(5000). 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 CardPileMiddle(int x, int y, int width, int height, Color color){
this.setBounds(x, y, width, height);
this.width = width;
this.height = height;
this.color = color;
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(color);
g.drawRect(0,0, width, height);
if (card != null){
card.draw(g);
}
}
@Override
public void mouseClicked(MouseEvent e){
}
@Override
public void mousePressed(MouseEvent e){
}
@Override
public void mouseEntered(MouseEvent e){
}
@Override
public void mouseExited(MouseEvent e){
}
@Override
public void mouseMoved(MouseEvent 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<>();
addMouseListener(this);
addMouseMotionListener(this);
clear();
// int width =0;
// int height =0;
CardPileMiddle centerPile = new CardPileMiddle(275,150,150,200, Color.YELLOW){
@Override
public void mouseDragged(MouseEvent e){
}
@Override
public void mouseReleased(MouseEvent e){
}
};
newPile = new CardPile(500,500,0,0, Color.GRAY){
@Override
public boolean dropCardIfContains(Point mousePoint, Card card){
return false;
}
@Override
public void placeCard(Card card){
}
};
this.add(centerPile);
}
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 =250;
int yOffset =5;
int cardWidth =50;
int cardHeight =70;
for (String suit : suits){
for (String rank : ranks){
deck.add(new Card(xOffset, yOffset, cardWidth, cardHeight, Color.BLACK, 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 : deck){
card.draw(g);
}
if (selectedCard != null){
selectedCard.draw(g);
}
for (Card card : throwawayPile){
if (!card.equals(selectedCard)){
card.draw(g);
}
}
if (selectedCard != null){
selectedCard.draw(g);
newPile.draw(g);
}
}
public void moveCardToNewPosition(Card card, int x, int y){
this.card = card;
this.x = x;
this.y = y;
if (card != null){
card.move(x, y);}
}
public void clear(){
if (deck.size()>=52){
deck.subList(0,52).clear();
repaint();
}
}

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