BetinaUKTC

Guess-the-color

Jul 20th, 2020
1,404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.22 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.Random;
  6.  
  7. public class GUI implements ActionListener {
  8.  
  9.     JFrame frame;
  10.     JPanel panel;
  11.     JLabel score;
  12.     JButton buttonPink;
  13.     JButton buttonBlue;
  14.     JButton buttonCyan;
  15.     JButton buttonGray;
  16.     JButton buttonMagenta;
  17.     JButton buttonOrange;
  18.     JButton buttonGreen;
  19.     JButton buttonRed;
  20.     JButton buttonWhite;
  21.     static int n;
  22.     Random rand = new Random();
  23.     int points;
  24.  
  25.     public void getColor() {
  26.         n = rand.nextInt(9) + 1;
  27.         switch (n) {
  28.             case 1:
  29.                 panel.setBackground(Color.PINK);
  30.                 break;
  31.             case 2:
  32.                 panel.setBackground(Color.BLUE);
  33.                 break;
  34.             case 3:
  35.                 panel.setBackground(Color.CYAN);
  36.                 break;
  37.             case 4:
  38.                 panel.setBackground(Color.GRAY);
  39.                 break;
  40.             case 5:
  41.                 panel.setBackground(Color.MAGENTA);
  42.                 break;
  43.             case 6:
  44.                 panel.setBackground(Color.ORANGE);
  45.                 break;
  46.             case 7:
  47.                 panel.setBackground(Color.GREEN);
  48.                 break;
  49.             case 8:
  50.                 panel.setBackground(Color.RED);
  51.                 break;
  52.             case 9:
  53.                 panel.setBackground(Color.WHITE);
  54.                 break;
  55.             default:
  56.                 break;
  57.         }
  58.     }
  59.  
  60.     public GUI() {
  61.         points = 0;
  62.         frame = new JFrame();
  63.         panel = new JPanel();
  64.  
  65.         frame.setSize(660, 500);
  66.         frame.setLocation(50, 300);
  67.  
  68.         buttonPink = new JButton(new AbstractAction("PINK") {
  69.             @Override
  70.             public void actionPerformed(ActionEvent e) { //FOR PINK BUTTON
  71.                 getColor();
  72.                 if (n == 1) {
  73.                     points++;
  74.                     score.setText("Score: " + points);
  75.                 }
  76.             }
  77.         });// 1 button
  78.         buttonBlue = new JButton(new AbstractAction("BLUE") {
  79.             @Override
  80.             public void actionPerformed(ActionEvent e) { //FOR BLUE BUTTON
  81.                 getColor();
  82.                 if (n == 2) {
  83.                     points++;
  84.                     score.setText("Score: " + points);
  85.                 }
  86.             }
  87.         });// 2 button
  88.         buttonCyan = new JButton(new AbstractAction("CYAN") {
  89.             @Override
  90.             public void actionPerformed(ActionEvent e) { //FOR CYAN BUTTON
  91.                 getColor();
  92.                 if (n == 3) {
  93.                     points++;
  94.                     score.setText("Score: " + points);
  95.                 }
  96.             }
  97.         });// 3 button
  98.         buttonGray = new JButton(new AbstractAction("GREY") {
  99.             @Override
  100.             public void actionPerformed(ActionEvent e) { //FOR GREY BUTTON
  101.                 getColor();
  102.                 if (n == 4) {
  103.                     points++;
  104.                     score.setText("Score: " + points);
  105.                 }
  106.             }
  107.         });// 4 button
  108.         buttonMagenta = new JButton(new AbstractAction("MAGENTA") {
  109.             @Override
  110.             public void actionPerformed(ActionEvent e) { //FOR MAGENTA BUTTON
  111.                 getColor();
  112.                 if (n == 5) {
  113.                     points++;
  114.                     score.setText("Score: " + points);
  115.                 }
  116.             }
  117.         });// 5 button
  118.         buttonOrange = new JButton(new AbstractAction("ORANGE") {
  119.             @Override
  120.             public void actionPerformed(ActionEvent e) { //FOR ORANGE BUTTON
  121.                 getColor();
  122.                 if (n == 6) {
  123.                     points++;
  124.                     score.setText("Score: " + points);
  125.                 }
  126.             }
  127.         });// 6 button
  128.         buttonGreen = new JButton(new AbstractAction("GREEN") {
  129.             @Override
  130.             public void actionPerformed(ActionEvent e) { //FOR GREEN BUTTON
  131.                 getColor();
  132.                 if (n == 7) {
  133.                     points++;
  134.                     score.setText("Score: " + points);
  135.                 }
  136.             }
  137.         });// 7 button
  138.         buttonRed = new JButton(new AbstractAction("RED") {
  139.             @Override
  140.             public void actionPerformed(ActionEvent e) { //FOR RED BUTTON
  141.                 getColor();
  142.                 if (n == 8) {
  143.                     points++;
  144.                     score.setText("Score: " + points);
  145.                 }
  146.             }
  147.         });// 8 button
  148.         buttonWhite = new JButton(new AbstractAction("WHITE") {
  149.             @Override
  150.             public void actionPerformed(ActionEvent e) { //FOR BLACK BUTTON
  151.                 getColor();
  152.                 if (n == 9) {
  153.                     points++;
  154.                     score.setText("Score: " + points);
  155.                 }
  156.             }
  157.         });// 9 button
  158.  
  159.         panel.setLayout(null);
  160.         panel.setBackground(Color.getHSBColor(91,120,120));
  161.  
  162.         score = new JLabel("Score: " + points);
  163.         score.setSize(80, 55);
  164.         score.setFont(new Font("Serif", Font.BOLD, 20));
  165.         score.setLocation(540, 5);
  166.         panel.add(score);
  167.  
  168.         JLabel title = new JLabel("What color");
  169.         title.setSize(150,55);
  170.         title.setFont(new Font("Serif", Font.BOLD, 30));
  171.         title.setLocation(240,5);
  172.         panel.add(title);
  173.         JLabel title2 = new JLabel("will come next?");
  174.         title2.setSize(200,55);
  175.         title2.setFont(new Font("Serif", Font.BOLD, 30));
  176.         title2.setLocation(215,30);
  177.         panel.add(title2);
  178.  
  179.         //1 button 1 row
  180.         buttonPink.setSize(120, 70);
  181.         buttonPink.setFont(new Font("Italic", Font.ITALIC, 15));
  182.         buttonPink.setBackground(Color.WHITE);
  183.         buttonPink.setLocation(100, 120);
  184.         //2 button 1 row
  185.         buttonBlue.setSize(120, 70);
  186.         buttonBlue.setFont(new Font("Italic", Font.ITALIC, 15));
  187.         buttonBlue.setBackground(Color.WHITE);
  188.         buttonBlue.setLocation(270, 120);
  189.         //3 button 1 row
  190.         buttonCyan.setSize(120, 70);
  191.         buttonCyan.setFont(new Font("Italic", Font.ITALIC, 15));
  192.         buttonCyan.setBackground(Color.WHITE);
  193.         buttonCyan.setLocation(440, 120);
  194.  
  195.  
  196.         //1 button 2 row
  197.         buttonGray.setSize(120, 70);
  198.         buttonGray.setFont(new Font("Italic", Font.ITALIC, 15));
  199.         buttonGray.setBackground(Color.WHITE);
  200.         buttonGray.setLocation(100, 240);
  201.         //2 button 2 row
  202.         buttonMagenta.setSize(120, 70);
  203.         buttonMagenta.setFont(new Font("Italic", Font.ITALIC, 15));
  204.         buttonMagenta.setBackground(Color.WHITE);
  205.         buttonMagenta.setLocation(270, 240);
  206.         //3 button 2 row
  207.         buttonOrange.setSize(120, 70);
  208.         buttonOrange.setFont(new Font("Italic", Font.ITALIC, 15));
  209.         buttonOrange.setBackground(Color.WHITE);
  210.         buttonOrange.setLocation(440, 240);
  211.  
  212.  
  213.         //1 button 3 row
  214.         buttonGreen.setSize(120, 70);
  215.         buttonGreen.setFont(new Font("Italic", Font.ITALIC, 15));
  216.         buttonGreen.setBackground(Color.WHITE);
  217.         buttonGreen.setLocation(100, 360);
  218.         //2 button 3 row
  219.         buttonRed.setSize(120, 70);
  220.         buttonRed.setFont(new Font("Italic", Font.ITALIC, 15));
  221.         buttonRed.setBackground(Color.WHITE);
  222.         buttonRed.setLocation(270, 360);
  223.         //3 button 3 row
  224.         buttonWhite.setSize(120, 70);
  225.         buttonWhite.setFont(new Font("Italic", Font.ITALIC, 15));
  226.         buttonWhite.setBackground(Color.WHITE);
  227.         buttonWhite.setLocation(440, 360);
  228.  
  229.         panel.add(buttonPink);
  230.         panel.add(buttonBlue);
  231.         panel.add(buttonCyan);
  232.         panel.add(buttonGray);
  233.         panel.add(buttonMagenta);
  234.         panel.add(buttonOrange);
  235.         panel.add(buttonGreen);
  236.         panel.add(buttonRed);
  237.         panel.add(buttonWhite);
  238.  
  239.         frame.add(panel);
  240.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  241.         frame.setTitle("GUI");
  242.         frame.setVisible(true);
  243.     }
  244.  
  245.     public static void main(String[] args) {
  246.         new GUI();
  247.     }
  248.  
  249.     @Override
  250.     public void actionPerformed(ActionEvent e) {
  251.  
  252.     }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment