Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.Random;
- public class Genius extends JFrame{
- private final int windowTall=700;
- private final int windowWide=700;
- private boolean Blue = false;
- private boolean Red = false;
- private boolean Green = false;
- private boolean Yellow = false;
- private JButton[] ColorButton;
- private JPanel[] BtnLayout;
- private JButton sequencer = new JButton();
- Dimension btnSize = new Dimension (125, 125);
- public Genius(){
- setTitle("Genius - Alpha 1.0");
- setSize(windowWide, windowTall);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- ColorButton= new JButton[4];
- BtnLayout= new JPanel[5];
- for(int w=0; w<5; w++){
- BtnLayout[w]=new JPanel();
- }
- sequencer = new JButton("Start/Reset");
- for (int x=0; x<4; x++){
- ColorButton[x]= new JButton();
- ColorButton[x].setPreferredSize(btnSize);
- ColorButton[x].addActionListener(new ColorButtonListener());
- }
- BtnLayout[0].add(ColorButton[0]);
- BtnLayout[1].add(ColorButton[1]);
- BtnLayout[2].add(ColorButton[2]);
- BtnLayout[3].add(ColorButton[3]);
- BtnLayout[4].add(sequencer);
- add(BtnLayout[0], BorderLayout.NORTH);
- add(BtnLayout[1], BorderLayout.WEST);
- add(BtnLayout[2], BorderLayout.SOUTH);
- add(BtnLayout[3], BorderLayout.EAST);
- add(BtnLayout[4], BorderLayout.CENTER);
- ColorButton[0].setBackground(Color.blue);
- ColorButton[1].setBackground(Color.red);
- ColorButton[2].setBackground(Color.green);
- ColorButton[3].setBackground(Color.yellow);
- sequencer.addActionListener(new sequencerListener());
- pack();
- setVisible(true);
- setResizable(false);
- }
- private class sequencerListener implements ActionListener{
- public void actionPerformed(ActionEvent e){
- String nivelS;
- int nivel;
- nivelS = JOptionPane.showInputDialog ("Qual nível?");
- nivel = Integer.parseInt(nivelS);
- int tam, cont = 0;
- boolean nivelCorreto = true;
- do{
- switch (nivel){
- case 1:
- tam = 8;
- break;
- case 2:
- tam = 14;
- break;
- case 3:
- tam = 20;
- break;
- case 4:
- tam = 31;
- break;
- default:
- JOptionPane.showMessageDialog (null, "Nivel inválido!");
- nivelCorreto = false;
- }
- } while (nivelCorreto);
- int sequence[]=new int[31];
- Random rand=new Random();
- Thread thread = new ThreadBasica1();
- for (int r = 0; r < tam; r++){
- sequence[r] = rand.nextInt(4)+1;
- cont ++;
- for (int rs = 0; rs < cont; rs++){
- if(sequence[r]==1){
- ColorButton[0].setBackground(Color.black);
- ColorButton[1].setBackground(Color.red);
- ColorButton[2].setBackground(Color.green);
- ColorButton[3].setBackground(Color.yellow);
- ////////////////////
- thread.start();
- //////////////
- ColorButton[0].setBackground(Color.blue);
- }
- if(sequence[r]==2)
- {
- ColorButton[0].setBackground(Color.blue);
- ColorButton[1].setBackground(Color.black);
- ColorButton[2].setBackground(Color.green);
- ColorButton[3].setBackground(Color.yellow);
- }
- if(sequence[r]==3)
- {
- ColorButton[0].setBackground(Color.blue);
- ColorButton[1].setBackground(Color.red);
- ColorButton[2].setBackground(Color.black);
- ColorButton[3].setBackground(Color.yellow);
- }
- if (sequence[r]==4)
- {
- ColorButton[0].setBackground(Color.blue);
- ColorButton[1].setBackground(Color.red);
- ColorButton[2].setBackground(Color.green);
- ColorButton[3].setBackground(Color.black);
- }
- }
- }
- }
- }
- private class ColorButtonListener implements ActionListener {
- public void actionPerformed(ActionEvent e){
- for(int x=0; x<10;x++){
- if(e.getSource() == ColorButton[0]){
- Blue=true;
- ColorButton[0].setBackground(Color.black);
- ColorButton[1].setBackground(Color.red);
- ColorButton[2].setBackground(Color.green);
- ColorButton[3].setBackground(Color.yellow);
- e.setSource(0);
- Blue=false;
- }
- else if(e.getSource() == ColorButton[1]){
- Red=true;
- ColorButton[0].setBackground(Color.blue);
- ColorButton[1].setBackground(Color.black);
- ColorButton[2].setBackground(Color.green);
- ColorButton[3].setBackground(Color.yellow);
- e.setSource(0);
- Red = false;
- }
- else if(e.getSource() == ColorButton[2]){
- Green=true;
- ColorButton[0].setBackground(Color.blue);
- ColorButton[1].setBackground(Color.red);
- ColorButton[2].setBackground(Color.black);
- ColorButton[3].setBackground(Color.yellow);
- e.setSource(0);
- Green = false;
- }
- else if(e.getSource() == ColorButton[3]){
- Yellow=true;
- ColorButton[0].setBackground(Color.blue);
- ColorButton[1].setBackground(Color.red);
- ColorButton[2].setBackground(Color.green);
- ColorButton[3].setBackground(Color.black);
- e.setSource(0);
- Yellow = false;
- }
- }
- }
- }
- public class ThreadBasica1 extends Thread {
- public void run() {
- // troca a cor do botão
- try
- {
- Thread.sleep(100);
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- //troca a cor do botao denovo.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement