Reputation: 798
The following is a method in a "Deck" object that instantiates and manipulates an array of Card objects for various card games that I might write. This particular method accepts a JFrame and an x and y coordinate as parameters, then creates a JLayeredPane at that location on that JFrame in order to display the cards in the deck. Everything works well, except when it displays the cards the JLayeredPane does not respect the Z index defined by zOrd and the cards appear in the reverse order that I wish them to.
I have a JLayeredPane on the same Jframe that I am passing to this method that I placed there at design time, and I use very similar code to iterate through the deck and display them there and it works fine. So I think there is something about my JLayeredPane that I am not doing or setting at runtime that it needs in order to respect the z index, but I can't figure out what.
Any help is greatly appreciated.
public void display(JFrame DisplayFrame, Integer x, Integer y) {
int xPos = 0;
int zOrd = 0;
JPanel contentPane = (JPanel)DisplayFrame.getContentPane();
JLayeredPane lp = new JLayeredPane();
lp.setLayout(new FlowLayout());
Integer height = _cards[0].getCardImage().getHeight();
Integer width = _cards.length * 15 + _cards[0].getCardImage().getWidth() - 15;
lp.setBounds(x, y, width, height);
contentPane.add(lp);
for (Card _card : _cards) {
JLabel cardLabel = new JLabel();
cardLabel.setIcon(new ImageIcon(_card.getCardImage()));
cardLabel.setBounds(xPos, 0, _card.getCardImage().getWidth(), _card.getCardImage().getHeight());
lp.add(cardLabel, zOrd);
xPos+=15;
zOrd++;
}
}
Upvotes: 1
Views: 624
Reputation: 798
Posting the answer in case someone stumbles across this question in a search.
In the process of troubleshooting I wanted to add the zOrd to the text of each label to track each card's z index. In order to do this I changed the variable type of zOrd from int to Integer so I could call the .toString() method on it. When I did this, it worked. At first I thought that adding text to the label was what made the difference, but it wasn't. I removed the .setText() method and used my code exactly as written with the exception that zOrd is declared as an Integer vice an int, and it works. To deepen the mystery, the javaDoc for the JLayeredPane.add() method even says that the parameters are supposed to be (component cmpnt, int i).
EDIT: I am new to JAVA, so to be perfectly clear, when I refer to "javaDoc" above, I am perhaps using a misnomer. I see in writing my own classes that remarks beginning with /** placed immediately before a method with expose themselves as intellisense, at least in netbeans (perhaps other IDEs?). So the intellisense for the add() method in the JLayeredPane object calls for (component cmpnt, int i) as parameters. Since the intellisense is the first resource I go to for an explanation of a method and its parameters, I find this confusing. Perhaps it is an oversight, or perhaps there is something I do not understand about this object and method. Either way, passing it as an "Integer" works.
Things that make you go hmmmmm.
Perhaps someone very familiar with the JLayeredPane object can explain the reasoning behind this. I certainly can't.
Upvotes: 1
Reputation: 324118
Read the section from the Swing tutorial on How to Use Layered Pane for a working example. Lower numbered layers are painted before higher number layers.
Instead if using layered panes, maybe a better solution to use a layout manager that supports overlapping of components. Check out the Overlap Layout for a layout manager that supports this.
Each card is appearing beneath previous card
lp.setLayout(new FlowLayout());
You are not using the layered pane properly. When you use a FlowLayout, components are painted in reverse order that the component is added to a panel. Maybe you can just get rid of the set layout statement.
In any case I've given you two solutions.
Upvotes: 0