Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help to make the method moveCardToNewPosition works, so it'possible to move card by clicking on the cards and then click in the panel and the

Help to make the method moveCardToNewPosition works, so it'possible to move card by clicking on the cards and then click in the panel and the card moves to that place. Thanks!
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);}
}

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_2

Step: 3

blur-text-image_3

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

Understanding Databases Concepts And Practice

Authors: Suzanne W Dietrich

1st Edition

1119827949, 9781119827948

More Books

Students also viewed these Databases questions

Question

What is the typical class size?

Answered: 1 week ago