banaandrew

RandomCharacter

Jul 13th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. import javax.swing.JFrame;
  2.  
  3. public class RandomCharactersTest {
  4.  
  5.     public static void main(String[] args)
  6.     {
  7.         RandomCharacters randomCharacters = new RandomCharacters();
  8.        
  9.         randomCharacters.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10.         randomCharacters.setSize(200,200);
  11.         randomCharacters.setVisible(true);
  12.     }
  13. }
  14. /////////////////////
  15.  
  16. import java.awt.Color;
  17. import java.awt.FlowLayout;
  18. import java.security.SecureRandom;
  19.  
  20. import javax.swing.JButton;
  21. import javax.swing.JFrame;
  22. import javax.swing.JLabel;
  23. import javax.swing.JTextField;
  24.  
  25. public class RandomCharacters extends JFrame
  26. {
  27.  
  28.     private JLabel randChar;
  29.     private JTextField tf;
  30.    
  31.     private final JButton button;
  32.    
  33.     public RandomCharacters()
  34.     {
  35.         super("Random Characters");
  36.         setLayout(new FlowLayout());
  37.        
  38.         tf = new JTextField(15);
  39.        
  40.         randChar = new JLabel("fire");
  41.         add(randChar);
  42.        
  43.         button = new JButton("hi");
  44.         add(button);
  45.        
  46.         while(true)
  47.         {
  48.             paintComponent();
  49.         }
  50.     }
  51.    
  52.     public void paintComponent()
  53.     {
  54.         SecureRandom r = new SecureRandom();
  55.        
  56.         switch(r.nextInt(7))
  57.         {
  58.         case 0: randChar.setForeground(Color.RED);
  59.         break; //RED
  60.         case 1: randChar.setForeground(Color.YELLOW);
  61.         break; //YELLOW
  62.         case 2: randChar.setForeground(Color.BLUE);
  63.         break; //BLUE
  64.         case 3: randChar.setForeground(Color.GREEN);
  65.         break; //GREEN
  66.         case 4: randChar.setForeground(Color.CYAN);
  67.         break; //CYAN
  68.         case 5: randChar.setForeground(Color.PINK);
  69.         break; //PINK
  70.         case 6: randChar.setForeground(Color.BLACK);
  71.         //BLACK
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment