0

GENERAL CODE: ISSUES BELOW WILL REFERENCE CERTAIN CLASSES

public class Board extends JPanel { 

public Board() {
    BoardMethods a = new BoardMethods();//BoardMethods handles logic
    a.printBoard(getBoard());//irrelevant to question, helps determine winner
    JPanel panelLORDY = new JPanel(new FlowLayout());

    //JButton alphaButton = new JButton("Hi");
    //alphaButton.setBounds(213,131,100,100);
    //add(alphaButton);
}

public int[][] getBoard(){
    return boardEvalMatrix;
}
public void paintComponent(Graphics g){
    g.setColor(Color.BLUE);
    g.fillRect(0, 0, 1456, 916);
    Graphics2D newG = (Graphics2D) g;
    newG.setStroke(new BasicStroke(15));
    g.setColor(Color.YELLOW);
    for(int a = 0; a < 6; a++)//rows for board --- rowHeight is 127
        g.drawRect(128, 68 + (a*127), 1200, 127);
    g.setColor(Color.BLACK);
    newG.setStroke(new BasicStroke(8));
    for(int a = 0; a < 6; a++)//columns for board --- columnWidth is 171
        g.drawRect(128 + (a*171), 68, 171, 764);        
    for(int a = 0; a < 6; a++)//give rows black line in middle
        g.drawRect(128, 68 + (a*127), 1200, 127);
    //g.drawString("H", 213, 131); center point     
    //g.drawLine(50,0, 1456, 916); //width 1456 length 916 - school computer monitors
    JButton alphaButton = new JButton("Hi");
    alphaButton.setBounds(213,131,100,100);
    bored.add(alphaButton);

}
}


public class gameBoard extends JPanel {

private Board bored;
private RoundButton[][] buttonArray;//holds all roundButtons on board
private JPanel resetPanel, quitPanel;

public gameBoard() {
    setLayout(new BorderLayout());

    buttonArray = new RoundButton[6][7];
    for(int a = 0; a < buttonArray.length; a++)
        for(int b = 0; b < buttonArray[0].length; b++)


    bored = new Board();
    add(bored, BorderLayout.CENTER);

    resetPanel = new JPanel();
    resetPanel.setLayout(new FlowLayout());
    this.add(resetPanel, BorderLayout.NORTH);
    quitPanel = new JPanel();
    quitPanel.setLayout(new FlowLayout());
    this.add(quitPanel, BorderLayout.SOUTH);

    JButton resetButton = new JButton("Reset");
    resetButton.addActionListener(new resetListener());
    resetPanel.add(resetButton);//resets board
    JButton quitButton = new JButton("Quit");
    quitButton.addActionListener(new quitListener());
    quitPanel.add(quitButton);

    //JButton alphaButton = new JButton("Hi");
    //alphaButton.setBounds(213,131,100,100);
    //bored.add(alphaButton);


}
}

ISSUE 1:

I found this question regarding how to place a JButton at a certain location, however, I have circular appearing JButtons and I need the JButton to be centered on a certain point, in this case, 213,131. I am using the setBounds(int x, int y, int width, int height) method to place the JButton. Is there a way to have the JButton center itself on the coordinate mentioned above, or do I have to play with the numbers to get it where I want? This line can be found in both classes above, as I was testing something and found an issue described in ISSUE 2.

JButton alphaButton = new JButton("Hi");
alphaButton.setBounds(213,131,100,100);
add(alphaButton);

ISSUE 2:

If you look at the class Board, in the paintComponent method I use the setBounds() method to place my JButton. This works and places a button with what I believe are the correct dimensions on the JPanel when the paintComponent() method is called with the top left corner in the correct location. However, when the screen is refreshed, paintComponent() is again called and the number of buttons continues to increase, fun to mess with but not what I want. However, when I move the setBounds method and alphaButton anywhere outside of Board's paintComponent() method, that is, putting it into the class that Board is painted on, inside the Board constructor, the JButton propagation issue ceases, however, the JButton no longer places itself at the correct coordinate point and loses the dimensions that I gave it. How do I fix this?

P.S. You will see the setBounds method several times. In the real code, the setBounds method and the JButton calling it only exist in one class at a time, I entered it here multiple times to show you where I placed it.

Community
  • 1
  • 1
Ungeheuer
  • 1,393
  • 3
  • 17
  • 32

1 Answers1

1

Never use setBounds(), stick to a layout manager.

FlowLayout will automatically center your component. Even better is to use BorderLayout, and set to the CENTER.

Mordechai
  • 15,437
  • 2
  • 41
  • 82
  • JButton alphaButton = new JButton("Hi"); bored.add(alphaButton); alphaButton.setBounds(213,131,100,100); I did this, but no change. pops up above the game board and real tiny and is not square-ular – Ungeheuer Jun 04 '15 at 17:03
  • A better solution for issue 2 would be to not handle calculations in the `paintComponent` method, as this method will be called under a wide variety of common situations. – Vince Jun 04 '15 at 17:05
  • @VinceEmigh there are no calculations besides the one for drawing the line because I was too lazy to use my calculator so I just had java handle it – Ungeheuer Jun 04 '15 at 17:43
  • Vince didn't mean arithmatic, but rather any statement he/she is referring to. – Mordechai Jun 04 '15 at 17:45
  • @MouseEvent so then what is vince reffing? – Ungeheuer Jun 05 '15 at 15:29