Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.57 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.Random;
  5.  
  6. public class Genius extends JFrame{
  7.  
  8.     private final int windowTall=700;
  9.     private final int windowWide=700;
  10.     private boolean Blue = false;
  11.     private boolean Red = false;
  12.     private boolean Green = false;
  13.     private boolean Yellow = false;
  14.     private JButton[] ColorButton;    
  15.     private JPanel[] BtnLayout;
  16.     private JButton sequencer = new JButton();
  17.     Dimension btnSize = new Dimension (125, 125);
  18.    
  19.  
  20.     public Genius(){
  21.         setTitle("Genius - Alpha 1.0");
  22.         setSize(windowWide, windowTall);
  23.        
  24.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25.      
  26.         ColorButton= new JButton[4];
  27.         BtnLayout= new JPanel[5];
  28.      
  29.         for(int w=0; w<5; w++){
  30.             BtnLayout[w]=new JPanel();
  31.         }
  32.        
  33.        sequencer = new JButton("Start/Reset");    
  34.  
  35.         for (int x=0; x<4; x++){
  36.             ColorButton[x]= new JButton();
  37.             ColorButton[x].setPreferredSize(btnSize);
  38.             ColorButton[x].addActionListener(new ColorButtonListener());
  39.         }
  40.  
  41.         BtnLayout[0].add(ColorButton[0]);
  42.         BtnLayout[1].add(ColorButton[1]);
  43.         BtnLayout[2].add(ColorButton[2]);
  44.         BtnLayout[3].add(ColorButton[3]);
  45.         BtnLayout[4].add(sequencer);
  46.      
  47.         add(BtnLayout[0], BorderLayout.NORTH);
  48.         add(BtnLayout[1], BorderLayout.WEST);
  49.         add(BtnLayout[2], BorderLayout.SOUTH);
  50.         add(BtnLayout[3], BorderLayout.EAST);
  51.         add(BtnLayout[4], BorderLayout.CENTER);
  52.    
  53.         ColorButton[0].setBackground(Color.blue);  
  54.         ColorButton[1].setBackground(Color.red);
  55.         ColorButton[2].setBackground(Color.green);
  56.         ColorButton[3].setBackground(Color.yellow);
  57.  
  58.         sequencer.addActionListener(new sequencerListener());
  59.         pack();
  60.         setVisible(true);
  61.         setResizable(false);
  62.     }
  63.    
  64.    
  65.     private class sequencerListener implements ActionListener{
  66.         public void actionPerformed(ActionEvent e){
  67.  
  68.             String nivelS;
  69.             int nivel;
  70.             nivelS = JOptionPane.showInputDialog ("Qual nível?");  
  71.             nivel = Integer.parseInt(nivelS);
  72.             int tam, cont = 0;
  73.             boolean nivelCorreto = true;
  74.  
  75.             do{
  76.                 switch (nivel){
  77.                     case 1:
  78.                         tam = 8;
  79.                         break;
  80.                     case 2:
  81.                         tam = 14;
  82.                         break;
  83.                     case 3:
  84.                         tam = 20;
  85.                         break;
  86.                     case 4:
  87.                         tam = 31;
  88.                         break;
  89.                     default:
  90.                         JOptionPane.showMessageDialog (null, "Nivel inválido!");
  91.                         nivelCorreto = false;
  92.                 }
  93.             } while (nivelCorreto);
  94.  
  95.             int sequence[]=new int[31];
  96.             Random rand=new Random();        
  97.             Thread thread = new ThreadBasica1();
  98.  
  99.             for (int r = 0; r < tam; r++){
  100.  
  101.                 sequence[r] = rand.nextInt(4)+1;
  102.                 cont ++;
  103.                 for (int rs = 0; rs < cont; rs++){
  104.  
  105.                     if(sequence[r]==1){
  106.                              
  107.                         ColorButton[0].setBackground(Color.black);
  108.                         ColorButton[1].setBackground(Color.red);
  109.                         ColorButton[2].setBackground(Color.green);
  110.                         ColorButton[3].setBackground(Color.yellow);
  111.            
  112.             ////////////////////
  113.                         thread.start();
  114.             //////////////
  115.                         ColorButton[0].setBackground(Color.blue);              
  116.                
  117.                            
  118.                     }
  119.                     if(sequence[r]==2)
  120.                     {
  121.                                                  
  122.                         ColorButton[0].setBackground(Color.blue);
  123.                         ColorButton[1].setBackground(Color.black);
  124.                         ColorButton[2].setBackground(Color.green);
  125.                         ColorButton[3].setBackground(Color.yellow);
  126.                                    
  127.                            
  128.                     }
  129.                     if(sequence[r]==3)
  130.                     {
  131.                        
  132.                         ColorButton[0].setBackground(Color.blue);
  133.                         ColorButton[1].setBackground(Color.red);
  134.                         ColorButton[2].setBackground(Color.black);
  135.                         ColorButton[3].setBackground(Color.yellow);  
  136.                          
  137.        
  138.                            
  139.                     }
  140.                     if (sequence[r]==4)
  141.                     {
  142.                            
  143.                            
  144.                         ColorButton[0].setBackground(Color.blue);
  145.                         ColorButton[1].setBackground(Color.red);
  146.                         ColorButton[2].setBackground(Color.green);
  147.                         ColorButton[3].setBackground(Color.black);
  148.                     }                      
  149.                 }
  150.                        
  151.             }
  152.         }
  153.            
  154.     }
  155.    
  156.     private class ColorButtonListener implements ActionListener {
  157.         public void actionPerformed(ActionEvent e){
  158.            
  159.             for(int x=0; x<10;x++){
  160.  
  161.                 if(e.getSource() == ColorButton[0]){
  162.                     Blue=true;
  163.                
  164.                     ColorButton[0].setBackground(Color.black);
  165.                     ColorButton[1].setBackground(Color.red);
  166.                     ColorButton[2].setBackground(Color.green);
  167.                     ColorButton[3].setBackground(Color.yellow);
  168.  
  169.                     e.setSource(0);
  170.                
  171.                     Blue=false;
  172.                 }
  173.            
  174.                 else if(e.getSource() == ColorButton[1]){        
  175.                     Red=true;
  176.  
  177.                     ColorButton[0].setBackground(Color.blue);
  178.                     ColorButton[1].setBackground(Color.black);
  179.                     ColorButton[2].setBackground(Color.green);
  180.                     ColorButton[3].setBackground(Color.yellow);
  181.                     e.setSource(0);
  182.                
  183.                     Red = false;
  184.                 }
  185.            
  186.                 else if(e.getSource() == ColorButton[2]){
  187.                     Green=true;
  188.  
  189.                     ColorButton[0].setBackground(Color.blue);
  190.                     ColorButton[1].setBackground(Color.red);
  191.                     ColorButton[2].setBackground(Color.black);
  192.                     ColorButton[3].setBackground(Color.yellow);
  193.                     e.setSource(0);
  194.                
  195.                     Green = false;
  196.                 }
  197.            
  198.                 else if(e.getSource() == ColorButton[3]){
  199.                     Yellow=true;
  200.                
  201.                     ColorButton[0].setBackground(Color.blue);
  202.                     ColorButton[1].setBackground(Color.red);
  203.                     ColorButton[2].setBackground(Color.green);
  204.                     ColorButton[3].setBackground(Color.black);
  205.                     e.setSource(0);
  206.                
  207.                     Yellow = false;
  208.                 }
  209.             }
  210.         }
  211.     }
  212.     public class ThreadBasica1 extends Thread {
  213.         public void run() {
  214.    
  215.         // troca a cor do botão
  216.  
  217.         try
  218.         {
  219.             Thread.sleep(100);
  220.         }  
  221.         catch(Exception e)
  222.         {
  223.             e.printStackTrace();
  224.         }
  225.  
  226.         //troca a cor do botao denovo.
  227.         }
  228.     }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement