0

I have a layout question about my pokerodds calculator. My JFrame exists of a top panel with grid layout and a center panel with border layout. This center panel has added a JLabel with an image in BorderLayout.CENTER and a JPanel in BorderLayout.NORTH. The panel in BorderLayout.NORTH has two JButtons. Now I want to place a JButton on top of the JLabel (the pokertable) but I cant do centerPanel.add(boardCard1.BorderLayout.CENTER); because I already do this for the JLabel. How can I solve this and place the Cards in the center of the table? I have only included the aceClubs Card in the code snippet to provide better readability.

public class OddsCalculator extends JFrame{

JPanel topPanel;
JLabel tablePicture;
JPanel centerPanel;
//ImagePanel panel = new ImagePanel(new ImageIcon(this.getClass().getResource("/images/pokertable.png")).getImage());
JPanel centerPanelNorth;

Card aceClubs;

Card playerOneCardOne;
Card playerOneCardTwo;
Card playerTwoCardOne;
Card playerTwoCardTwo;
Card playerCardSpotTarget;
Card playerCardSpotSender;

Player player1=new Player();
Player player2=new Player();

Card boardCard1=new Card();
Card boardCard2=new Card();
Card boardCard3=new Card();
Card boardCard4=new Card();

Board board=new Board();



public OddsCalculator(){
    initUI();
}


public void initUI() {
   //cardsPanel=new JPanel(new GridLayout(4,13,0,0));
   setLayout(new BorderLayout());
   topPanel = new JPanel(new GridLayout(4,13,0,0)); 
   centerPanel=new JPanel(new BorderLayout()); 
   tablePicture = new JLabel(new ImageIcon(this.getClass().getResource(Constants.POKERTABLE_ICON)));
   centerPanelNorth=new JPanel();

   aceClubs=new Card();

   playerOneCardOne=new Card();
   playerOneCardTwo=new Card();
   playerTwoCardOne=new Card();
   playerTwoCardTwo=new Card();

   boardCard1=new Card();
   boardCard2=new Card();
   boardCard3=new Card();

   board=new Board();

   //setLayout(new FlowLayout(FlowLayout.LEFT));

   topPanel.setPreferredSize(new Dimension(1200,450));//was 1000/600

   getContentPane().add(topPanel,BorderLayout.NORTH);

   //setSize(1000,1600);
   setDefaultCloseOperation(EXIT_ON_CLOSE);


   aceClubs.suit=Constants.CARD_SUIT_CLUBS;
   aceClubs.kind=Constants.CARD_KIND_ACE;
   aceClubs.iconPath=Constants.ACE_CLUBS_ICON;

   aceClubs.setIcon(new javax.swing.ImageIcon(this.getClass().getResource(aceClubs.iconPath)));
   aceClubs.setBorder(null);
   aceClubs.setContentAreaFilled(false);
   aceClubs.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            MouseClicked(evt);
        }
    });
   topPanel.add(aceClubs,BorderLayout.PAGE_START);


   getContentPane().add(centerPanel,BorderLayout.CENTER); //was centerpanel


     //PLAYER 1 CARD SPOTS
   playerOneCardOne.iconPath=Constants.CARD_BACKSIDE;
   playerOneCardOne.setIcon(new javax.swing.ImageIcon(this.getClass().getResource(playerOneCardOne.iconPath)));
   playerOneCardOne.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 5));
   playerOneCardOne.setBorderPainted(false);
   playerOneCardOne.setContentAreaFilled(false);
   playerOneCardOne.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            PlayerOneMouseClicked(evt);
        }
    });
   //centerPanel.add(playerOneCardOne,BorderLayout.WEST); //was centerpanel

   playerOneCardTwo.iconPath=Constants.CARD_BACKSIDE;
   playerOneCardTwo.setIcon(new javax.swing.ImageIcon(this.getClass().getResource(playerOneCardTwo.iconPath)));
   playerOneCardTwo.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 5));
   playerOneCardTwo.setBorderPainted(false);
   playerOneCardTwo.setContentAreaFilled(false);
   playerOneCardTwo.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            PlayerOneMouseClicked(evt);
        }
    });
   //centerPanel.add(playerOneCardTwo,BorderLayout.EAST);


   //BOARD CARD SPOTS
   boardCard1.iconPath=Constants.CARD_BACKSIDE;
   boardCard1.setIcon(new javax.swing.ImageIcon(this.getClass().getResource(boardCard1.iconPath)));
   boardCard1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 5));
   boardCard1.setBorderPainted(false);
   boardCard1.setContentAreaFilled(false);
   boardCard1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            PlayerOneMouseClicked(evt);
        }
    });
   boardCard1.boardPosition=1;
  // centerPanel.add(boardCard1);

   boardCard2.iconPath=Constants.CARD_BACKSIDE;
   boardCard2.setIcon(new javax.swing.ImageIcon(this.getClass().getResource(boardCard2.iconPath)));
   boardCard2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 5));
   boardCard2.setBorderPainted(false);
   boardCard2.setContentAreaFilled(false);
   boardCard2.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            PlayerOneMouseClicked(evt);
        }
    });
   //centerPanel.add(boardCard1,SwingConstants.CENTER);
   boardCard2.boardPosition=2;
   //centerPanel.add(boardCard2);

   boardCard3.iconPath=Constants.CARD_BACKSIDE;
   boardCard3.setIcon(new javax.swing.ImageIcon(this.getClass().getResource(boardCard3.iconPath)));
   boardCard3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 5));
   boardCard3.setBorderPainted(false);
   boardCard3.setContentAreaFilled(false);
   boardCard3.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            PlayerOneMouseClicked(evt);
        }
    });
   //centerPanel.add(boardCard1);
   boardCard3.boardPosition=3;
   //centerPanel.add(boardCard3);

   boardCard4.iconPath=Constants.CARD_BACKSIDE;
   boardCard4.setIcon(new javax.swing.ImageIcon(this.getClass().getResource(boardCard4.iconPath)));
   boardCard4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 5));
   boardCard4.setBorderPainted(false);
   boardCard4.setContentAreaFilled(false);
   boardCard4.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            PlayerOneMouseClicked(evt);
        }
    });
   //centerPanel.add(boardCard1,SwingConstants.CENTER);
   //boardCard4.boardPosition=4      
   //centerPanel.add(boardCard4);

   centerPanel.add(tablePicture,BorderLayout.CENTER); //TABLE
   centerPanel.add(centerPanelNorth,BorderLayout.NORTH);
   centerPanelNorth.add(playerOneCardOne);
   centerPanelNorth.add(playerOneCardTwo);
   //How to add for example boardCard1 on top of the table?

}

 private void MouseClicked(java.awt.event.MouseEvent evt)    {                              

     //JButton b=(JButton)evt.getSource();
     playerCardSpotSender=(Card)evt.getSource();
     System.out.println(playerCardSpotSender.suit+" "+playerCardSpotSender.kind);

     if (playerCardSpotTarget != null && playerCardSpotTarget.isBorderPainted()) {
         playerCardSpotSender.setLocation(playerCardSpotTarget.getLocation());
         System.out.println(playerCardSpotTarget.getLocation());

     }

}   

private void PlayerOneMouseClicked(java.awt.event.MouseEvent evt){
     //JButton b=(JButton)evt.getSource();
     playerCardSpotTarget=(Card)evt.getSource();

    if(playerCardSpotTarget.isBorderPainted()){
        playerCardSpotTarget.setBorderPainted(false);
    }
    else{
        playerCardSpotTarget.setBorderPainted(true);
    }

}

public static void main(String[] args) {
  OddsCalculator oc=new OddsCalculator();
  oc.setVisible(true);
  oc.pack();

  }
}

Card.java

public class Card extends JButton{
int suit;
int kind;
boolean known;
String iconPath;
Integer boardPosition;
}

Layout at the moment:

enter image description here

**EDIT: ** How can I position the buttons in center with OverlayLayout?

Sybren
  • 1,071
  • 3
  • 17
  • 51
  • 2
    [set LayoutManager to JLabel](http://stackoverflow.com/questions/8575641/how-returns-xxxsize-from-jcomponents-added-to-the-jlabel) – mKorbel Apr 14 '15 at 08:49
  • @mKorbel how to use that in my case? – Sybren Apr 14 '15 at 08:50
  • looks like as by using GBC – mKorbel Apr 14 '15 at 08:51
  • It seems like a question resulting from a solution you gave to another problem. The initial need seems to be that you want to have a background image in the center panel. For this you put a JLabel there, but it's not really convenient. Your problem is probably rather to find a way to paint a background image in a panel, no ? – Gnoupi Apr 14 '15 at 08:52
  • @Gnoupi I read about two solutions: setting the background image in a panel screwed the whole layout. The other way was to use a JLabel as there are no specific components to hold an image. How would you solve it? – Sybren Apr 14 '15 at 08:56
  • @Sybren GridBagLayout == GBC – mKorbel Apr 14 '15 at 09:02
  • I'm not sure how it's screwing the whole layout. See this other question for suggestions: http://stackoverflow.com/questions/19125707/simplest-way-to-set-image-as-jpanel-background – Gnoupi Apr 14 '15 at 09:03
  • I just discovered the existence of the [OverlayLayout](http://docs.oracle.com/javase/7/docs/api/javax/swing/OverlayLayout.html), hm. Could match to your needs. – Gnoupi Apr 14 '15 at 09:05
  • @Gnoupi this is what I mean with the screwed layout gyazo.com/42b22953c0a93e4777e62551d93e3134 – Sybren Apr 14 '15 at 09:05
  • @Gnoupi how to position the buttons in the overlay layout? – Sybren Apr 14 '15 at 10:10
  • @Sybren - now that would be another question to post, I guess. I don't know myself, discovered the class today as well. Seems like it's layered, so it's not there that you deal with it, but with a sub panel. – Gnoupi Apr 14 '15 at 12:30

1 Answers1

0

First of all your question is way too complex with too much code. Your question is about display a button on top of an image. So create a frame with an image and add a button to the image. It should take about 20 lines of code to create a simple program that does this. Then once you understand the basic concept of how to do this you add the changes to your real program.

All the other code you posted is irrelevant to the problem and is completely unnecessary. Learn to simplify your problem when you ask a question.

Now I want to place a JButton on top of the JLabel (the pokertable) but I cant do centerPanel.add(boardCard1.BorderLayout.CENTER); because I already do this for the JLabel.

So you add the button to the label:

label.setLayout( new GridBagLayout() );
label.add(button, new GridBagConstraints());

As long as the component you want to display is smaller than the size of the image on the label the component will be centered in the image.

camickr
  • 321,443
  • 19
  • 166
  • 288