-3

I am making a guessing grid game in java using a few buttons. The program uses the java gridlayout layout and when a button is clicked I want it to add its GridLayout position (not x & y) into the array.

Here is a strip of the code where the cell position finder will be needed.

    public void actionPerformed(ActionEvent e)
    {


      if(placementLimit < 5){

       //placementlimit is limit to buttons clicked

        JButton clickedButton = (JButton)e.getSource(); 

        clickedButton.setBackground(Color.RED);
        /* something like: int pos =  clickedButton.getGridPos
         *  arrayplacedpositions.add(pos);
        */
        placementLimit++;
   } 

I did not make a JPanel so the whole project is here:

        /* Guessing game GoldMiner By Alexander Smirnov
 * A Computer Game where you hide and place gold.
 */ 
package standard;

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Arrays;


import javax.swing.*;

public class game extends JPanel
{
    JButton buttons[] = new JButton[64];
    int placementLimit = 0;
    ArrayList goldPos = new ArrayList();


    public game()
    {
        setLayout(new GridLayout(8,8));
        initializebuttons();
    }


    public void initializebuttons()
    {
        for(int i = 0; i <= 63; i++)
        {
            buttons[i] = new JButton();
            buttons[i].setText("");
            buttons[i].addActionListener(new buttonListener());

            add(buttons[i]);
        }
    }



    private class buttonListener implements ActionListener
    {

        public void actionPerformed(ActionEvent e)
        {


            if(placementLimit < 5){
            JButton clickedButton = (JButton)e.getSource(); 
            clickedButton.setIcon(new ImageIcon("/Users/Administrator/Desktop/gold2.jpg"));
            clickedButton.setBackground(Color.RED);
            JPanel.getComponents();
            placementLimit++;
       } else {


           JOptionPane.showMessageDialog(null, "You have placed the maximum amount of gold!");

            } 

            }






    }
    public static void main(String[] args)
    {
        JFrame window = new JFrame("GoldMiner");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().add(new game());
        window.setBounds(300,200,300,300);
        Container c = window.getContentPane();

        window.setVisible(true);
    }
}
A Smirnov
  • 5
  • 8
  • If this is not a duplicate, please edit your question to include a [mcve] that shows your revised approach. – trashgod Dec 04 '16 at 10:02

1 Answers1

2

It's not the easiest thing to do... Likely it would be more useful to hold your JButtons in a 2D array, or better hold a map of JButton to position.

If you really must discern the gridcell from the button, call

JPanel.getComponents() (or whatever the parent container is), and find your Button in there. Then do the math.

say you find it at index 6.

Then since your grid is x by y divide by x to get the row, and mod by x to get the column.

so say the grid ix 4x4

6/4 is 1, so you are in the row with index 1 (second row)

6 % 4 is 2, so you are in the column with index 2 (third column)

MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
  • Can you please give a code example. – A Smirnov Dec 04 '16 at 03:53
  • Also, my button was formed using a for loop so I can't manually call each button. – A Smirnov Dec 04 '16 at 03:55
  • (1+) for good advice. Also, I'm not sure if it would be a kludge, but the grid locations could be stuffed into the JButton at creation using the `putClientProperty(...)` method. – Hovercraft Full Of Eels Dec 04 '16 at 03:59
  • Or create an ActionListener (or better AbstractAction) class, perhaps an inner class, that accepts the grid location, and set that into the button. – Hovercraft Full Of Eels Dec 04 '16 at 04:01
  • I am a n00b so can you please explain what a grid/row/column/side index is? Also, I don't have a JPanel. – A Smirnov Dec 04 '16 at 04:29
  • @ASmirnov you have a Container of some sort. call the getComponents() on that. You have an 8x8 grid of buttons. so find the button in the array returned from above. note the index. then do index / 8 to get the row and index % 8 to get the column. – MeBigFatGuy Dec 04 '16 at 04:38
  • *"Also, I don't have a JPanel."* You really should at least glance at the code you used from Alexander Smirnov. It clearly states.. `public class game extends JPanel` and stop asking for code. SO is not a code generation machine. – Andrew Thompson Dec 04 '16 at 05:09
  • Is there some kind of online tutorial for this? – A Smirnov Dec 04 '16 at 16:07
  • When I implement the math to my program it always shows 7, 7 on the coordinates, please help! – A Smirnov Dec 04 '16 at 17:20