4 in a row in java

0

I'll see you doing a java program that consists of the typical 4 in a row of a lifetime.

The issue is that I have no idea how to do so that when I press one of the buttons I put in a column, I painted another color to simulate that the card has been inserted, does anyone know the idea of how to do it?

the current code is this:

interface

public class Interfaz {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Marco miMarco = new Marco();
        miMarco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

Marco

class Marco extends JFrame{

    public Marco() {

        setVisible(true);
        setTitle("4 en Raya");
        setSize(700, 620);
        setResizable(false);
        setLocationRelativeTo(null);

        Tablero miTablero = new Tablero();
        add(miTablero);
        miTablero.updateUI();

    }

}

Board

class Tablero extends JPanel{

    private Image tablero;



    public Tablero() {

        try {

            tablero = ImageIO.read(new File("resources/mosaico.png"));

        }catch(IOException e) {

            e.printStackTrace();

        }

        setLayout(new GridLayout(1, 7));

        JButton boton1 = new JButton();
        JButton boton2 = new JButton();
        JButton boton3 = new JButton();
        JButton boton4 = new JButton();
        JButton boton5 = new JButton();
        JButton boton6 = new JButton();
        JButton boton7 = new JButton();

        add(boton1);
        boton1.setContentAreaFilled(false);
        add(boton2);
        boton2.setContentAreaFilled(false);
        add(boton3);
        boton3.setContentAreaFilled(false);
        add(boton4);
        boton4.setContentAreaFilled(false);
        add(boton5);
        boton5.setContentAreaFilled(false);
        add(boton6);
        boton6.setContentAreaFilled(false);
        add(boton7);
        boton7.setContentAreaFilled(false);

    }

    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        g.drawImage(tablero, 0, 0, this);

    }

}
    
asked by D3n3cry 02.01.2018 в 17:36
source

1 answer

0

As you are occupying Swing, I have no knowledge of him; however, you can create EventHandlers or event handlers for when X action occurs, in this case, a button is pressed.

Something like this:

JButton boton1 = new JButton("Boton 1");

...

boton1.addActionListener(new ButtonListener()); 

class ButtonListener implements ActionListener{

    public void actionPerformed(ActionEvent ae){

        if(ae.getActionCommand().equals("Boton 1"))
            boton1.setContentAreaFilled(true);
        else if(ae.getActionCommand().equals("Boton 2"))
            boton1.setContentAreaFilled(true);
            //Y sigues asi con los botones...
    }
}

I repeat that I have never touched Java Swing, so some syntax may be wrong, but take this as a base on which to base yourself, ok? :)

I recommend this video as an example, which I relied on, is in English but, you understand it enough .

Good luck.

    
answered by 03.01.2018 в 03:24