[text] Color Game

Viewer

copydownloadembedprintName: Color Game
  1. package color_game;
  2.  
  3. import java.awt.EventQueue;
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6. import javax.swing.JOptionPane;
  7. import java.awt.BorderLayout;
  8. import java.awt.Color;
  9. import java.awt.Font;
  10. import javax.swing.JTextField;
  11. import javax.swing.JButton;
  12. import javax.swing.SwingConstants;
  13. import java.awt.event.ActionListener;
  14. import java.util.Random;
  15. import java.awt.event.ActionEvent;
  16.  
  17. public class Color_Game {
  18.  
  19.     private JFrame frame;
  20.     int score=0,lives=0,n=0,n1=0,temp=0;
  21.     Color[] c= {Color.white,Color.blue,Color.green,Color.orange,Color.yellow,Color.black,Color.pink,Color.red};
  22.     String[] cs= {"WHITE","BlUE","GREEN","ORANGE","YELLOW","BLACK","PINK","RED"};
  23.     private JTextField textField;
  24.     String tempstr="";
  25.     /**
  26.     * Launch the application.
  27.      * @param args
  28.     */
  29.   
  30.     public static void main(String[] args) {
  31.        EventQueue.invokeLater(() -> {
  32.            try {
  33.                Color_Game window = new Color_Game();
  34.                window.frame.setVisible(true);
  35.            } catch (Exception e) {
  36.                e.printStackTrace();
  37.            }
  38.        });
  39.   }
  40.  
  41.   /**
  42.    * Create the application.
  43.    */
  44.   public Color_Game() {
  45.     initialize();
  46.   }
  47.  
  48.   /**
  49.    * Initialize the contents of the frame.
  50.    */
  51.   private void initialize() {
  52.     frame = new JFrame();
  53.     frame.setBounds(200, 100, 420, 400);
  54.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  55.     frame.getContentPane().setLayout(null);
  56.     
  57.     JLabel lblWelcomeToColor = new JLabel("Welcome to Color Game!!");
  58.     lblWelcomeToColor.setFont(new Font("Dialog", Font.BOLD, 20));
  59.     lblWelcomeToColor.setBounds(87, 12, 321, 50);
  60.     frame.getContentPane().add(lblWelcomeToColor);
  61.     
  62.     JLabel lblScore = new JLabel("Score:");
  63.     lblScore.setBounds(24, 60, 70, 15);
  64.     frame.getContentPane().add(lblScore);
  65.     
  66.     JLabel label = new JLabel("0");
  67.     label.setBounds(72, 60, 70, 15);
  68.     frame.getContentPane().add(label);
  69.     
  70.     JLabel lblLives = new JLabel("Lives:");
  71.     lblLives.setBounds(323, 48, 50, 41);
  72.     frame.getContentPane().add(lblLives);
  73.     
  74.     JLabel label_1 = new JLabel("0");
  75.     label_1.setBounds(360, 54, 50, 29);
  76.     frame.getContentPane().add(label_1);
  77.     
  78.     JLabel lblColor = new JLabel("Color");
  79.     lblColor.setHorizontalAlignment(SwingConstants.CENTER);
  80.     lblColor.setFont(new Font("Dialog", Font.BOLD, 30));
  81.     lblColor.setBounds(76, 85, 264, 92);
  82.     frame.getContentPane().add(lblColor);
  83.     
  84.     textField = new JTextField();
  85.     textField.setBounds(101, 202, 216, 19);
  86.     frame.getContentPane().add(textField);
  87.     textField.setColumns(10);
  88.     
  89.     JButton btnSubmit = new JButton("Submit");
  90.     btnSubmit.addActionListener(new ActionListenerImpl(label, label_1, lblColor));
  91.     btnSubmit.setBounds(150, 233, 117, 25);
  92.     frame.getContentPane().add(btnSubmit);
  93.     
  94.     JLabel lblToStartOr = new JLabel("To Start or Restart the game enter START and SUBMIT IT ");
  95.     lblToStartOr.setBounds(24, 74, 414, 15);
  96.     frame.getContentPane().add(lblToStartOr);
  97.     
  98.     JLabel lblEnterTheCorrect = new JLabel("Enter the correct COLOR of the text");
  99.     lblEnterTheCorrect.setHorizontalAlignment(SwingConstants.CENTER);
  100.     lblEnterTheCorrect.setBounds(35, 178, 349, 15);
  101.     frame.getContentPane().add(lblEnterTheCorrect);
  102.     }
  103.  
  104.     private class ActionListenerImpl implements ActionListener {
  105.  
  106.         private final JLabel label;
  107.         private final JLabel label_1;
  108.         private final JLabel lblColor;
  109.  
  110.         public ActionListenerImpl(JLabel label, JLabel label_1, JLabel lblColor) {
  111.             this.label = label;
  112.             this.label_1 = label_1;
  113.             this.lblColor = lblColor;
  114.         }
  115.  
  116.         @Override
  117.         public void actionPerformed(ActionEvent arg0) {
  118.             Random random=new Random();
  119.             String t=textField.getText().toLowerCase();
  120.             //System.out.println(t);
  121.             int tempn=n;
  122.             //System.out.println(cs[tempn].toLowerCase());
  123.             if(temp>12 || lives==0 || tempstr.isEmpty()) {
  124.                 if(tempstr.isEmpty()&&!t.equals("start")){
  125.                     JOptionPane.showMessageDialog(frame,"Please Start the game by typing start","Alert",JOptionPane.WARNING_MESSAGE);}
  126.                 
  127.                 if(!tempstr.isEmpty()){
  128.                     
  129.                     String status="";
  130.                     if(score>9) {
  131.                         status="Winner";
  132.                     }
  133.                     else {
  134.                         status="losser";
  135.                     }
  136.                     JOptionPane.showMessageDialog(frame,"Game Over!\n"+status+" Your Score"+String.valueOf(score)+"\n Restart Your game by typing Start in Text Field","Alert",JOptionPane.WARNING_MESSAGE);}
  137.             }
  138.             if(t.equals("start")) {
  139.                 score=0;temp=0;
  140.                 lives=3;
  141.                 tempstr="start";
  142.                 label.setText(String.valueOf(score));
  143.                 label_1.setText(String.valueOf(lives));
  144.                 n=random.nextInt(c.length-1);
  145.                 n1=random.nextInt(cs.length-1);
  146.                 lblColor.setText(cs[n1]);
  147.                 lblColor.setForeground(c[n]);
  148.             }
  149.             else if(tempstr.equals("start")&&lives!=0) {
  150.                 if(t.equals(cs[tempn].toLowerCase())) {
  151.                     score++;
  152.                     temp++;
  153.                     label.setText(String.valueOf(score));
  154.                     n=random.nextInt(c.length-1);
  155.                     n1=random.nextInt(cs.length-1);
  156.                     lblColor.setText(cs[n1]);
  157.                     lblColor.setForeground(c[n]);
  158.                 }
  159.                 else {
  160.                     lives--;
  161.                     temp++;
  162.                     label_1.setText(String.valueOf(lives));
  163.                     n=random.nextInt(c.length-1);
  164.                     n1=random.nextInt(cs.length-1);
  165.                     lblColor.setText(cs[n1]);
  166.                     lblColor.setForeground(c[n]);
  167.                     
  168.                 }
  169.             }
  170.         }
  171.     }
  172.     
  173. }
  174.  

Editor

You can edit this paste and save as new:


File Description
  • Color Game
  • Paste Code
  • 28 Mar-2024
  • 6.05 Kb
You can Share it: