我正在尝试制作蜘蛛纸牌,并一直坚持能够移动多张牌。我的方法是在移动时将所有卡片移动到arraylist中,然后在释放鼠标后将它们添加到正确的堆栈中。我不确定为什么这不起作用,可能是图形错误? 这是我的代码:
package spidersolitaire;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.applet.*;
import javax.swing.*;
public class Display extends javax.swing.JApplet implements ActionListener, MouseListener, MouseMotionListener{
private Deck deck;
private Timer t= new Timer(1, this);
ArrayList<Card> onTable[]= new ArrayList[10];
boolean isFaceUp[][] = new boolean[7][10];
Card toBeDealt[][]= new Card[5][10]; //50 total cards
private Position [][]cardCoordinates= new Position[10][50];
private Card tempCard= null;
int tempCardIndexA;
int tempCardIndexB;
private int tempX;
private int tempY;
int numberOfDeals=0;
int numberOfDealsDisplay=5;
ArrayList<Card> moveableStack= new ArrayList<Card>();
boolean isMoveableStack= false;
boolean isPlaced= false;
public Display(){
Object []values= {"EASY", "HARD"};
int input= JOptionPane.showOptionDialog(null, "CHOOSE DIFFICULTY", "DIFFICULTY", 0, 0, null, values, 0);
deck= new Deck(input);
deck.assignCards(onTable, toBeDealt, isFaceUp);
int x=10;
int y=10;
for(int r=0; r<10; r++){
x+=105;
y=10;
for(int c=0; c<50; c++){
y+=20;
cardCoordinates[r][c]= new Position(x,y);
}
}
for(ArrayList<Card> e: onTable)System.out.println(e);
addMouseMotionListener(this);
addMouseListener(this);
}
public void init(){
this.setSize(new java.awt.Dimension(1270, 900)); //1270 900
t.start();
}
public void paint(Graphics g){
g.setColor(new Color(150,150,50));
g.fillRect(0, 0, 1270, 900);
g.setColor(Color.BLACK);
g.drawImage(getImage(getDocumentBase(), "backofcard.jpg"), 1070, 700, this); //deal card graphics
g.setFont(new Font("TimesRoman", Font.PLAIN, 30));
g.drawString("Deals Left: " + numberOfDealsDisplay, 990, 820);
int drawAtX=10;
int drawAtY=10;
for(int col=0; col<10; col++){ //displays all cards on table
drawAtX+=105;
drawAtY=10;
for(int row=0; row<onTable[col].size(); row++){
drawAtY+=20;
if(row<5){
if(isFaceUp[row][col])g.drawImage(getImage(getDocumentBase(), onTable[col].get(row).getImageName()), drawAtX, drawAtY, this);
else g.drawImage(getImage(getDocumentBase(),"backofcard.jpg"), drawAtX, drawAtY, this);
}
else g.drawImage(getImage(getDocumentBase(), onTable[col].get(row).getImageName()), drawAtX, drawAtY, this);
}
}
if(isMoveableStack){
//System.out.println(isMoveableStack);
int yx= tempY;
for(int s= 0; s<moveableStack.size(); s++){
g.drawImage(getImage(getDocumentBase(), moveableStack.get(s).getImageName()), tempX-20, yx, this);
yx+=20;
}
isMoveableStack=false;
}
try{g.drawImage(getImage(getDocumentBase(), tempCard.getImageName()), tempX-20, tempY-20, this);}//moving cards, catches exception thrown before tempCard is set
catch(NullPointerException e){
};
}
public void setMoveableStack(int Ax, int By){
int cardsAdded=0;
System.out.println(Ax + ", " + By);
for(int b= By; b<onTable[Ax].size();b++){
if(By==b)moveableStack.add(onTable[Ax].remove(By));
if(onTable[Ax].get(b).getRank()+1==moveableStack.get(cardsAdded).getRank()&&onTable[Ax].get(b).getSuit()==onTable[Ax].get(b-1).getSuit()){
moveableStack.add(onTable[Ax].remove(By));
cardsAdded++;
}
else break;
isMoveableStack=true;
}
repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
if(e.getX()>1070&&e.getX()<1140&&e.getY()>700&&e.getY()<795){
if(numberOfDeals<5){
System.out.println();
for(int i=0; i<onTable.length;i++){
onTable[i].add(toBeDealt[numberOfDeals][i]);
System.out.println(toBeDealt[numberOfDeals][i]);
}
}
numberOfDeals++;
if(numberOfDealsDisplay>0) numberOfDealsDisplay--;
repaint();
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent e) {
for(int a=0; a<10; a++){
for(int l=0; l<onTable[a].size(); l++){
if(e.getX()>cardCoordinates[a][l].getCordX()&&e.getX()<cardCoordinates[a][l].getCordX()+71 //finds what card user clicks on based on Position cardCoordinates[][] and sets to tempCard
&& e.getY()>cardCoordinates[a][l].getCordY()&&e.getY()<cardCoordinates[a][l].getCordY()+20){
if(l>6){
tempCard=onTable[a].remove(l);
setMoveableStack(a, l);
}
else if(l<7&&isFaceUp[l][a]){
tempCard=onTable[a].remove(l);
setMoveableStack(a, l);
}
tempX= cardCoordinates[a][l].getCordX();
tempY= cardCoordinates[a][l].getCordY();
tempCardIndexA=a; //the arraylist/stack
tempCardIndexB=l; //the index of the arraylist
}
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
for(int i=0; i<cardCoordinates.length; i++){
if(e.getX()>cardCoordinates[i][0].getCordX()&&e.getX()<cardCoordinates[i][0].getCordX()+80&&(onTable[i].get(onTable[i].size()-1).getRank()==tempCard.getRank()+1||(onTable[i].size()==0))){
for(int c= 0; c<moveableStack.size(); c++){
onTable[i].add(moveableStack.remove(0));
}
isMoveableStack=false;
if(tempCardIndexB>1)
isFaceUp[tempCardIndexB-1]
[tempCardIndexA]=true;
}
else isPlaced=false;
}
if(!isPlaced){
for(int c= 0; c<moveableStack.size(); c++){
onTable[tempCardIndexA].add(moveableStack.remove(0));
}
isPlaced=true;
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
}
@Override
public void mouseDragged(MouseEvent e) {
tempX= e.getX();
tempY=e.getY();
repaint();
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
以及其他课程(如果需要):
package spidersolitaire;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Card {
private int suit; //0 for spades, 1 for hearts
private int rank;
private String CARD_IMAGE_NAME;
public Card(int s, int r){
rank=r;
suit=s;
setImage();
}
public int getSuit(){
return suit;
}
public int getRank(){
return rank;
}
public String getImageName(){
return this.CARD_IMAGE_NAME;
}
public String toString(){
if(suit==0){
if(rank==11)return "Jack of Spades";
if(rank==1)return "Ace of Spades";
if(rank==12)return "Queen of Spades";
if(rank==13)return "King of Spades";
return rank + " of Spades";
}
if(suit==1){
if(rank==11)return "Jack of Hearts";
if(rank==1)return "Ace of Hearts";
if(rank==12)return "Queen of Hearts";
if(rank==13)return "King of Hearts";
return rank + " of Hearts";
}
return "";
}
public void setImage(){
if(suit==0){
if(rank==1)CARD_IMAGE_NAME= "spadesace.jpg";
if(rank==2)CARD_IMAGE_NAME= "spades2.png";
if(rank==3)CARD_IMAGE_NAME= "spades3.jpg";
if(rank==4)CARD_IMAGE_NAME= "spades4.jpg";
if(rank==5)CARD_IMAGE_NAME= "spades5.jpg";
if(rank==6)CARD_IMAGE_NAME= "spades6.jpg";
if(rank==7)CARD_IMAGE_NAME= "spades7.jpg";
if(rank==8)CARD_IMAGE_NAME= "spades8.jpg";
if(rank==9)CARD_IMAGE_NAME= "spades9.jpg";
if(rank==10)CARD_IMAGE_NAME= "spades10.png";
if(rank==11)CARD_IMAGE_NAME= "spadesj.jpg";
if(rank==12)CARD_IMAGE_NAME= "spadesq.jpg";
if(rank==13)CARD_IMAGE_NAME= "spadesk.jpg";
}
if(suit==1){
if(rank==1)CARD_IMAGE_NAME= "heartsa.png";
if(rank==2)CARD_IMAGE_NAME= "hearts2.png";
if(rank==3)CARD_IMAGE_NAME= "hearts3.png";
if(rank==4)CARD_IMAGE_NAME= "hearts4.png";
if(rank==5)CARD_IMAGE_NAME= "hearts5.png";
if(rank==6)CARD_IMAGE_NAME= "hearts6.png";
if(rank==7)CARD_IMAGE_NAME= "hearts7.png";
if(rank==8)CARD_IMAGE_NAME= "hearts8.png";
if(rank==9)CARD_IMAGE_NAME= "hearts9.png";
if(rank==10)CARD_IMAGE_NAME= "hearts10.png";
if(rank==11)CARD_IMAGE_NAME= "heartsj.png";
if(rank==12)CARD_IMAGE_NAME= "heartsq.png";
if(rank==13)CARD_IMAGE_NAME= "heartsk.png";
}
}}
package spidersolitaire;
import java.awt.Image;
import java.util.ArrayList;
public class Deck {
private ArrayList<Card> cardList= new ArrayList<Card>(104);
private int difficulty;
public Deck(int dif){ //makes a deck of 104 cards
difficulty= dif;
int temp=-1;
if(dif==0)temp=3; //changes stopping point to switch suits if necessary
if(dif==1)temp=1;
for(int i=0; i <= temp; i++){
for(int r=1; r<=13; r++){
cardList.add(new Card(0, r)); //adds 2 or 4 sets of black cards depending on difficulty selected
}
}
if(temp==1){
for(int i=0; i <= temp; i++){
for(int r=1; r<=13; r++){
cardList.add(new Card(1, r)); //adds two sets of red cards if difficulty is hard
}
}
}
for(int i=0; i<52; i++)cardList.add(cardList.get(i)); //copies the first half of the deck to make 104 total cards
this.shuffle();
}
public void assignCards(ArrayList[] cards, Card [][] tbd, boolean [][] faceUp){ //cards on the table, cards to be dealt, and booleans if the card on table is face up
for(int c= 0; c<cards.length; c++)cards[c]= new ArrayList<Card>();
int num=0;
for(int r=0; r<7; r++){
for(int c=0; c<10; c++){
faceUp[r][c]=false;
if(num<54){
cards[c].add(cardList.get(num));}//puts cards in correct arraylist on the table
if(num>43)faceUp[r][c]=true;
num++;
}
}
num=54;
for(int r=0; r<5; r++){
for(int c=0; c<10; c++){
if(num<104);
tbd[r][c]=cardList.get(num);
num++;
}
}
}
public void shuffle(){
int random= (int)(Math.random()*10)+3;
for(int x= 0; x<=random; x++){ //shuffles a random number of times
ArrayList<Card> halfOne= new ArrayList<Card>();
ArrayList<Card> halfTwo= new ArrayList<Card>();
int half=cardList.size()/2;
for(int index=0; index<half;index++){
halfOne.add(this.cardList.get(index));
}
for(int index=half; index<cardList.size(); index++){
halfTwo.add(cardList.get(index));
}
ArrayList<Card> shuffled= new ArrayList<Card>();
for(int i=0; i<half; i++){
shuffled.add(halfTwo.get(0));
shuffled.add(halfOne.get(0));
halfTwo.remove(0);
halfOne.remove(0);
}
if(cardList.size()%2==1)shuffled.add(halfTwo.get(0));
this.cardList=shuffled;
}
}
public void printDeck(){
System.out.println(cardList);
}
}
package spidersolitaire;
public class Position {
private int XPos;
private int YPos;
public Position(int x, int y){
XPos= x;
YPos=y;
}
public int getCordX(){
return XPos;
}
public int getCordY(){
return YPos;
}
}