I want to create a Pong game with a moving ball and all the stuff. I'm now working on my score Labels that I want to add on each side of the center line. One label for the computer score and an other for the player score. The problem is that my panel is set to the BorderLayout. I don't know how i can add my panel to that location.
here is my constructor code :
* Constructor : PlayPanel.java
*/
// ==============================================
public PlayPanel() {
super(new BorderLayout());
setBackground(Color.DARK_GRAY);
panPlayer1 = new JPanel();
panComputer = new JPanel();
padPlayer1 = new JPanel();
padComputer = new JPanel();
padPlayer1.setPreferredSize(PADPANEL_SIZE);
padComputer.setPreferredSize(PADPANEL_SIZE);
panPlayer1.setBackground(PAN_PLAY);
panComputer.setBackground(PAN_PLAY);
panPlayer1.add(padPlayer1);
panComputer.add(padComputer);
add(panPlayer1, BorderLayout.WEST);
add(panComputer, BorderLayout.EAST);
player1Score.setFont(FONT_SCORE);
ComputerScore.setFont(FONT_SCORE);
// Add them to each side if the line !?!?!?!?!?!?
add(player1Score);
add(ComputerScore);
addMouseMotionListener(this);
panPlayer1.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent arg0) {
setPanPanelWidth(arg0.getComponent().getSize().width);
setPanPanelHeight(arg0.getComponent().getSize().height);
}
});
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent arg0) {
setPlayPanelWidth(arg0.getComponent().getSize().width);
setPlayPanelHeight(arg0.getComponent().getSize().height);
}
});
}
You have two options:
Add them both to a 1 row, 2 column GridLayout
and then add that to BorderLayout.NORTH
of your
main panel
Incorporate them into your playerPanel
and ComputerPanel
objects. (I would also re-case the ComputerPanel
to be computerPanel
).