0

I'm making a Chess-like game in Java Swing (I know Swing isn't the proper way to go about it but I want to attempt it nevertheless) I am having a problem with making the chess pieces show up on the display at the proper paces. Due to the nature of the chess pieces physical positions, I cannot use a layout manager. The code looks something like this (it's admittedly awkwardly designed) :

public class Window extends JFrame {
  private JPanel board;
  public Window() {
    super();
    board = new JPanel();
    JLabelOrganizer jlo = new JLabelOrganizer();
    for (JLabel: JLabelOrganizer) {
      JLabel.setBounds(calcX(), calcY(), width, height);
      board.add(JLabel);
    }
    board.setBounds(x, y, width1, height1);
    board.setLayout(null);
    add(board);
    setLayout(null);
  }
  public class JLabelOrganizer {
    public JLabelOrganizer {
      instantiate Type1 and Type2 JLabel objects and store them
    }
    public class Type1 extends JLabel {
    }
    public class Type2 extends JLabel {
    }
  }
}

The classes Type1 and Type2 represent the pieces. When this runs, the JLabels (Type1 and Type2) do not show up at the correct place as designated from setBounds(). However, the board JPanel holding these pieces is set up at the correct place as designated from the call of its own setBounds(). Does anything have an idea as to why this is happening? Could it be because I'm inheriting from JLabel or the JLabel classes are inner classes? Thanks.

Edit: Forgot to specify that the JLabels only show up in the upper left hand corner of where the JPanel is located no matter what x and y positions I set them to.

  • *I cannot use a layout manager.* - of course you can. In fact it is much easier since the GridLayout does all the work for you. For example: https://stackoverflow.com/a/6811800/131872 – camickr Mar 19 '20 at 02:33
  • @ScaryWombat My bad, those are supposed to be place holders for their x and y values. They are indeed changing (verified by printing them out to console). –  Mar 19 '20 at 02:37
  • @camickr My chess game does not use the standard Cartesian plane. Rather the chess pieces are more like arranged on a flattened dodecahedron kinda. So I cannot use the GridLayout. –  Mar 19 '20 at 02:40
  • 2
    *They are indeed changing* - they are not changing based on the code you posted. The location will be the value of the x/y variables at the time the setBounds() statement is executed. Since you don't change the value, all labels will have the same location. We can't guess what you might be doing in some other part of your code. Post a proper [mre] that demonstrates the problems. – camickr Mar 19 '20 at 02:42
  • Personally, I'd either consider writing my own layout manager to handle this logic or do direct drawing – MadProgrammer Mar 19 '20 at 03:29
  • There's no reason why you can't set the position of a label by using ```setBounds```. I'm puzzled why you are setting the position of the board that way. It should just fill the frame, or if you want it to be a certain size, set its preferred size and then make the frame not realizable. – J Banana Mar 19 '20 at 13:29
  • I agree with @MadProgrammer. I would do direct drawing on a drawing JPanel. Also, be sure your logical board is separate from the drawing of the logical board. – Gilbert Le Blanc Mar 20 '20 at 02:28

1 Answers1

0

I found the answer after looking at another post. My class inheriting JLabel won't show up but if I change the type to JLabel it does

I accidentally overrided JLabel's getX() and getY() for my own purposes. Java Swing uses a component's getX() and getY() which is why the change with setBounds() was not being reflected.

  • Congratulations - you've learnt the importance of a [mcve] - because there's no way any one else would have been able to figure that out – MadProgrammer Mar 20 '20 at 05:28