-1
I’m making the game for java and when starting the machine will give a random sequence of clicks on 4 buttons and then the user has to press the same machine, but I do not know how to make the machine press the button
package gênius;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.AWTException;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class tela extends JFrame {
//nome do jogador e pontos
int pontos = 0 ;
String usuario = "";
//varieaveis de jogadas
String jjogador = "";
String maquina ="";
// janela de exibição
public void janela(){
JFrame ftela = new JFrame();
ftela.setTitle("Gênius");
ftela.setSize(400, 600);
ftela.setBackground(Color.white);
ftela.setLocationRelativeTo(null);
ftela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ftela.setVisible(true);
//boões de ção
JButton b1 = new JButton("");
b1.setBounds(100, 220, 80, 80);
b1.setVisible(true);
b1.setBackground(Color.BLUE);
JButton b2 = new JButton("");
b2.setBounds(200, 220, 80, 80);
b2.setVisible(true);
b2.setBackground(Color.red);
JButton b3 = new JButton("");
b3.setBounds(100, 320, 80, 80);
b3.setVisible(true);
b3.setBackground(Color.GREEN);
JButton b4 = new JButton("");
b4.setBounds(200, 320, 80, 80);
b4.setVisible(true);
b4.setBackground(Color.YELLOW);
JButton b5 = new JButton("vermalho");
b5.setBounds(200, 420, 80, 80);
b5.setVisible(true);
b5.getColorModel();
b5.setBackground(Color.WHITE);
JPanel p1 = new JPanel();
ftela.add(b1);
ftela.add(b2);
ftela.add(b3);
ftela.add(b4);
ftela.add(b5);
//ações dos botôes
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
play("b1");
jjogador += " "+0;
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
play("b2");
jjogador += " "+1;
}
});
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
play("b3");
jjogador += " "+2;
}
});
b4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
play("b4");
jjogador += " "+3;
}
});
b5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, jjogador);
}
});
}
// coloando som no botão
public void play(String audio){
URL url = getClass().getResource(audio+".wav");
AudioClip audioc = Applet.newAudioClip(url);
audioc.play();
}
public void jlabe (){
JLabel mPontos = new JLabel("pontos ");
mPontos.setText("pontos :"+pontos);
mPontos.setHorizontalAlignment(SwingConstants.CENTER);
}
}
I think I get it, but your approach is wrong, I’m going to try to make an example here in a way that can meet.
– user28595
please, I’m in a bit of a bind because I don’t even know what to use to do this
– Diogo Cipriano
you know how to ?
– Diogo Cipriano
Is there any way you can add what you already asked in the question? Showing from scratch will end up generating a more complex response than necessary for the question, it is easier to try high from its starting point.
– user28595
have yes I’ll add
– Diogo Cipriano