Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.Timer;
- public class SimonGUI extends JFrame implements ActionListener{
- /**
- * @param args
- */
- //creates the buttons needed and a timer
- JButton btn1,btn2,btn3,btn4;
- JButton btnGo;
- Timer t;
- public static void main(String[] args) {
- SimonGUI s = new SimonGUI();
- s.init();
- s.setSize(865, 300);
- s.setTitle("Simon Says");
- s.setVisible(true);
- }
- private void init(){
- t = new Timer(1000, this); //sets up a times that will run for 1 second
- Container pane = this.getContentPane();
- pane.setLayout(null);
- btn1 = new JButton();
- btn1.setBounds(10, 10, 200, 200);
- btn1.setBackground(Color.black);
- pane.add(btn1);
- //you can add the other buttons here
- btnGo = new JButton();
- btnGo.setBounds(10, 220, 830, 40);
- btnGo.setText("GO");
- btnGo.addActionListener(this);
- pane.add(btnGo);
- }
- @Override
- public void actionPerformed(ActionEvent event) {
- if(event.getSource() == btnGo)
- {
- btn1.setBackground(Color.red); // sets the background colour
- repaint(); //updates the screen
- t.start();// starts the timer
- }
- if(event.getSource() ==t){
- btn1.setBackground(Color.black); //resets btn1 to black
- //other buttons should go in here as well
- repaint();
- t.stop(); //stops the timer
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement