Answered step by step
Verified Expert Solution
Link Copied!

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(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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Introduction To Databases And Data Warehouses

Authors: Nenad Jukic, Susan Vrbsky, Svetlozar Nestorov

1st Edition

1943153191, 978-1943153190

More Books

Students also viewed these Databases questions

Question

=+19.4. Consider weak convergence in LP((0, 1], @, ).

Answered: 1 week ago

Question

A fiduciary responsibility is ultimately based on what trait

Answered: 1 week ago