0

I'm relatively new to java and have been working on a simple Blackjack program using Java Swing, but I'm having trouble getting setAlignmentX to work in OverlayLayout.

Basically the idea is to have two cards generate on the left side of the play area (the red rectangle), then add another card (on top and to the right of the previous cards) when the player clicks "Draw".

Picture Link because I'm too new to Stack Overflow

I try to do this by adding a card (JLabel with image icon) with a .setAlignmentX that is greater than the previous card.

However, there are two problems. The first is that when a single card is generated, it is able to stick all the way to the left of the rectangle. However, when two cards are generated, they both generate further to the right, away from the side.

The second is that the new card is adding itself below, and to the left, of the previous cards (instead of above and to the right). Furthermore, as per the two cards, the whole chunk is moving rightwards on every draw.

Another Picture Link

Here is the relevant code:

ArrayList<JLabel> tuCardsPanelList = new ArrayList<>();
...
for(int i = 0; i < 9; i++){
    tuCardsPanelList.add(new JLabel());
}
tuCardsPanel = new JPanel(new BorderLayout());
tuCardsPanel.setBackground(new Color(130, 30, 40));
tuCardsPanel.setBorder(new LineBorder(Color.yellow));
tuCardsPanel.setPreferredSize(new Dimension(400, 200));
tuCardsPanel.setLayout(new OverlayLayout(tuCardsPanel));
...
//display first two cards
ImageIcon image = new ImageIcon(getClass().getResource(activePlayer.hand.get(0).imageLocation));
tuCardsPanelList.get(0).setIcon(new ImageIcon(image.getImage().getScaledInstance(121, 185, Image.SCALE_SMOOTH)));
tuCardsPanelList.get(0).setPreferredSize(new Dimension(121, 185));
tuCardsPanelList.get(0).setAlignmentX(0.0f);//SETALIGNMENTX HERE
tuCardsPanelList.get(0).setAlignmentY(0.5f);
ImageIcon image1 = new ImageIcon(getClass().getResource(activePlayer.hand.get(1).imageLocation));
tuCardsPanelList.get(1).setIcon(new ImageIcon(image1.getImage().getScaledInstance(121, 185, Image.SCALE_SMOOTH)));
tuCardsPanelList.get(1).setPreferredSize(new Dimension(121, 185));
tuCardsPanelList.get(1).setAlignmentX(0.16f); //SETALIGNMENTX HERE
tuCardsPanelList.get(1).setAlignmentY(0.5f);
tuCardsPanel.add(tuCardsPanelList.get(0));
tuCardsPanel.add(tuCardsPanelList.get(1));
...
//when "Draw" button is pressed
activePlayer.drawFrom(deck);
tuLabel3.setText("Total: " + activePlayer.valueOfHand());
int currentNewCard = activePlayer.hand.size() - 1;
ImageIcon image = new ImageIcon(getClass().getResource(activePlayer.hand.get(currentNewCard).imageLocation));
tuCardsPanelList.get(currentNewCard).setIcon(new ImageIcon(image.getImage().getScaledInstance(121, 185, Image.SCALE_SMOOTH)));
tuCardsPanelList.get(currentNewCard).setPreferredSize(new Dimension(121, 185));
tuCardsPanelList.get(currentNewCard).setAlignmentY(0.5f);
tuCardsPanelList.get(currentNewCard).setAlignmentX(0.32f); //SETALIGNMENTX HERE

If it helps, one really weird thing I found was that the second card (with .setAlignmentX(0.16f)) is actually on the left of the first card (which has a .setAlignmentX(0f)!)

I think they are all symptoms of a property of .setAlignmentX or OverlayLayout that I'm misusing, but I just can't figure it out. Google didn't help either.

Any help will be greatly appreciated! :)

  • *I think they are all symptoms of a property of .setAlignmentX* - yes, that is not the way OverlayLayout was designed to work. You want a layout manager that allows you to set a pixel spacing between the cards so the spacing is consistent. See: https://stackoverflow.com/questions/32910225/make-components-overlap-in-flowlayout/32910293#32910293 for a couple of suggestions – camickr May 21 '20 at 13:43
  • The negative FlowLayout didn't work because the first image also had a negative gap and hence went out of the frame...I used Layered Panes in the end (how did I miss that?) and it worked. Thanks for the help anyway! – Nicholas Ho May 22 '20 at 05:34
  • *I used Layered Panes* - maybe I don't understand the question, but it sounds like you are trying to use a layered pane as a layout manager, which means you are duplicating layout code somewhere in your application. *The negative FlowLayout didn't work* - there were two suggestions in the link. The second suggestion is a custom layout manager that, if I understand the question, does exactly what you want. – camickr May 22 '20 at 13:53

0 Answers0