所以我有这个测试类,我非常清楚地在我的Deck对象中设置约束,但它最终为null。它为什么这样做?我无法弄清楚
import javax.swing.JFrame;
import java.awt.*;
class DeckTester
{
public DeckTester()
{
JFrame fr = new JFrame();
fr.setDefaultCloseOperation(fr.EXIT_ON_CLOSE);
fr.setSize(640,480);
fr.setLocation(300,200);
CardTable table = new CardTable();
table.setLayout(new FlowLayout());
Stack faceupPile = new Stack(0,-.25,true);
faceupPile.setStackRules(Stack.GRAB_FROM_TOP|Stack.FACEUP);
table.add(faceupPile);
StackRuleConstraints src = new StackRuleConstraints();
src.dropPile = faceupPile;
Deck deck = new Deck();
deck.setStackRules(deck.getStackRules(),src);
table.add(new Deck());
fr.add(table);
fr.setVisible(true);
}
public static void main(String[]args)
{
new DeckTester();
}
}
这是Deck,Stack和StackRuleConstraints类。我有
System.out.println("constraints are "+(ruleConstraints!=null?"not null":"null"));
包含在mouseReleased变量ml和setStackRules方法中。
...
import java.awt.Color;
public class Deck extends Stack
{
public Deck()
{
this(false);
}
public Deck(boolean includeJokers)
{
super(0,-.25,true);
for(int rank = Card.ACE; rank <= Card.KING; rank++)
{
for(int i = 0; i < 4; i++)
{
int suit = (int)Math.pow(2,i);
add(new Card(rank,suit));
}
}
if(includeJokers)
{
add(new Card(Card.JOKER,Card.RED));
add(new Card(Card.JOKER,Card.BLACK));
}
setStackRules(GRAB_FROM_TOP|DRAW_PILE);
shuffle();
}
}
...
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Collections;
import java.util.ArrayList;
import java.awt.Container;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.RenderingHints;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
public class Stack extends Container
{
public static final int GRAB_FROM_TOP = 1;
public static final int GRAB_BY_GROUP = 2;
public static final int ASCENDING = 4;
public static final int DESCENDING = 8;
public static final int SAME_SUITS = 16;
public static final int ALTERNATING_COLORS = 32;
public static final int DRAW_PILE = 64;
public static final int RECEPTACLE = 128;
public static final int FACEDOWN = 256;
public static final int FACEUP = 512;
private int rules = 0;
protected ArrayList<Card> cards = new ArrayList<Card>();
protected boolean accessible;
private double xOffset;
private double yOffset;
private Color outlineColor;
protected static Card selectedCard = null; //BOOKMARK we have to grab cards by the group, thus this will probably need to be a Card array
protected static Card[] groupedCards = null;
private Point clickLoc;
protected static int xOrigin;
protected static int yOrigin;
protected static int mouseX;
protected static int mouseY;
protected static Point cardOrigin;
protected static boolean dragging = false;
protected static double draggedCardXOffset;
protected static double draggedCardYOffset;
private ArrayList<Card> cardsToRemove = new ArrayList<Card>();
protected StackRuleConstraints ruleConstraints = null;
private MouseAdapter ml = new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
if((rules&RECEPTACLE)>0)return;
xOrigin = e.getXOnScreen();
yOrigin = e.getYOnScreen();
for(int i = 0; i < cards.size(); i++)
{
if(cards.get(i).getLocationOnScreen().x<xOrigin
&&xOrigin<cards.get(i).getLocationOnScreen().x+50
&&cards.get(i).getLocationOnScreen().y<yOrigin
&&yOrigin<cards.get(i).getLocationOnScreen().y+70)
{
cardsToRemove.clear();
groupedCards = null;
if(i!=cards.size()-1)
{
if((rules&GRAB_FROM_TOP)>0)continue;
if((rules&GRAB_BY_GROUP)>0&&i!=cards.size()-1)
{
groupedCards = new Card[cards.size()-1-i];
for(int j = i+1; j < cards.size(); j++)
{
groupedCards[j-i-1] = cards.get(j);
cardsToRemove.add(cards.get(j));
}
}
}
if((rules&DRAW_PILE)==0&&!cards.get(i).isSelected())//if the card you clicked is facedown
{
if(i!=cards.size()-1)return; //if it's not the top card, return
else //otherwise flip it faceup
{
cards.get(i).setSelected(true);
repaint();
return;
}
}
cardOrigin = cards.get(i).getLocationOnScreen();
selectedCard = cards.get(i);
draggedCardXOffset = xOffset;
draggedCardYOffset = yOffset;
}
}
}
public void mouseDragged(MouseEvent e)
{
if(!dragging&&selectedCard!=null)
{
remove(selectedCard);
for(Card c: cardsToRemove)System.out.println("moving "+c.toString());
if(cardsToRemove.size()>0)for(int i = 0; i < cardsToRemove.size(); i++){remove(cardsToRemove.get(i));}
dragging = true;
}
mouseX = e.getXOnScreen();
mouseY = e.getYOnScreen();
getParent().repaint();
}
public void mouseReleased(MouseEvent e)
{
System.out.println("constraints are "+(ruleConstraints!=null?"not null":"null"));
if(dragging)ADDING_CARD_TO_STACK:
{
for(Component c: getParent().getComponents())
{
if(c instanceof Stack
&&e.getXOnScreen()>c.getLocationOnScreen().x
&&e.getXOnScreen()<c.getLocationOnScreen().x+c.getWidth()
&&e.getYOnScreen()>c.getLocationOnScreen().y
&&e.getYOnScreen()<c.getLocationOnScreen().y+c.getHeight()
&&selectedCard!=null
&&(((Stack)c).rules&DRAW_PILE)==0)
{
((Stack)c).add(selectedCard);
if(groupedCards!=null)for(Card card: groupedCards)((Stack)c).add(card);
break ADDING_CARD_TO_STACK;
}
}
if(dragging&&selectedCard!=null){add(selectedCard);if(groupedCards!=null)for(Card card: groupedCards)add(card);}
}
else {if((rules&DRAW_PILE)>0&&ruleConstraints!=null){System.out.println("criteria met");if(ruleConstraints.dropPile!=null){remove(selectedCard);ruleConstraints.dropPile.add(selectedCard);}}}
selectedCard = null;
groupedCards = null;
cardsToRemove.clear();
dragging = false;
getParent().repaint();
}
};
public Stack()
{
this(true);
}
public Stack(boolean accessible)
{
this(0,0,accessible);
}
public Stack(double xOffset, double yOffset, boolean accessible)
{
this(new Color(5,250,10),xOffset,yOffset,accessible);
}
public Stack(Color outlineColor, double xOffset, double yOffset, boolean accessible)
{
super();
this.xOffset = xOffset;
this.yOffset = yOffset;
this.accessible = accessible;
this.outlineColor = outlineColor;
setPreferredSize(new Dimension((int)(50+Math.max(2,Math.abs(getXOffset())*cards.size())),(int)(70+Math.max(2,.25*54)+(getYOffset()<0?0:getYOffset()*cards.size()))));
addMouseListener(ml);
addMouseMotionListener(ml);
}
public void setStackRules(int rules)
{
this.rules = rules;
}
public void setStackRules(int rules, StackRuleConstraints constraints)
{
this.rules = rules;
ruleConstraints = constraints;
System.out.println("constraints are "+(ruleConstraints!=null?"not null":"null"));
}
public int getStackRules()
{
return rules;
}
public Color getOutlineColor()
{
return outlineColor;
}
public void setOutlineColor(Color c)
{
outlineColor = c;
repaint();
}
public void setXOffset(double xOffset)
{
this.xOffset = xOffset;
}
public void setYOffset(double yOffset)
{
this.yOffset = yOffset;
}
public double getXOffset()
{
return xOffset;
}
public double getYOffset()
{
return yOffset;
}
public void shuffle()
{
Collections.shuffle(cards);
super.removeAll();
for(int i = 0; i < cards.size();i++)super.add(cards.get(i));
}
public void remove(Card c)
{
cards.remove(c);
super.remove(c);
setPreferredSize(new Dimension((int)(50+Math.max(2,Math.abs(getXOffset())*cards.size())),(int)(70+Math.max(2,.25*54)+(getYOffset()<0?0:getYOffset()*cards.size()))));
setSize(getPreferredSize());
repaint();
}
// public Card drawCard()
// {
// if(cards.size()>0)
// {
// Card c = cards.get(cards.size()-1);
// remove(c);
// cards.remove(cards.size()-1);
// return c;
// }
// else return null;
// }
public void add(Card c)
{
cards.add(c);
super.add(c);
if((rules&FACEUP)>0)c.setSelected(true);
if((rules&FACEDOWN)>0)c.setSelected(false);
setPreferredSize(new Dimension((int)(50+Math.max(2,Math.abs(getXOffset())*cards.size())),(int)(70+Math.max(2,.25*54)+(getYOffset()<0?0:getYOffset()*cards.size()))));
setSize(getPreferredSize());
repaint();
}
@Override
public void paint(Graphics gi)
{
Graphics2D g = (Graphics2D)gi;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(outlineColor);
g.fillRoundRect(0,(int)(.25*54)-2,52,72,10,10);
g.setColor(getParent().getBackground());
g.fillRoundRect(2,(int)(.25*54),48,68,5,5);
double x = 1;
double y = .25*54-1;
for(int i = 0; i < cards.size(); i++)
{
cards.get(i).setLocation((int)x,(int)y);
g.drawImage(cards.get(i).getCurrentImage(),(int)x,(int)y,null);
x+=xOffset;
y+=yOffset;
}
super.paint(g);
}
}
...
public class StackRuleConstraints
{
public Stack dropPile = null;
public StackRuleConstraints()
{
}
}
输出
constraints are not null
constraints are null
答案 0 :(得分:1)
您正在创建一个套牌,但随后在表格中添加一个新套牌:
Deck deck = new Deck();
deck.setStackRules(deck.getStackRules(),src);
table.add(new Deck());
应该是
table.add(deck);