How to assign action to Image or Button

-1

I'm developing the homepage of a simple game, what I want is to add a button on the screen where when I click on the game to start, follow below my code of how I'm setting up the menu and bringing the background, the code behind the image I put like the button, but I am not able to assign the click action to it, I do not know what would be the best way to do this, I accept ideas.

public Menu(){

        setFocusable(true);
        setDoubleBuffered(true);

        ImageIcon referencia = new ImageIcon("res\images\fundo.png");

        fundo = referencia.getImage();
        emJogo = true; //iniciar jogo
        timer = new Timer(5, this);
        timer.start();

        ImageIcon referencia2 = new ImageIcon("res\images\btn1.png");
        jogar = referencia2.getImage();

    }

    public void paint(Graphics g){  

        Graphics2D graficos = (Graphics2D) g;
        graficos.drawImage(fundo, 0, 0, null);
        graficos.drawImage(jogar, 0, 0, null);

        g.dispose();

    }
    
asked by anonymous 21.10.2015 / 13:14

1 answer

0

Just register an event for your button as the example below and be happy. Most components have the "addActionListener" method to bind the click event.

// Adicionando um evento action ao botão
botao.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {

            // Aqui você escreve qual será a ação do botão ao ser clicado!
            botao.setText(campo.getText());
        }
    });
    
21.10.2015 / 13:55