package CHAPTER11;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Lottery extends JFrame {
    private JLabel label;
    private JButton luckyNumbersButton;
    private JButton colorButton;
    private JTextField[] numberFields;

    public Lottery() {
        // Set up layout
        setLayout(new BorderLayout());

        // Top label
        label = new JLabel("Every One a Winner!", SwingConstants.CENTER);
        add(label, BorderLayout.NORTH);

        // Center panel for numbers
        JPanel centerPanel = new JPanel(new GridLayout(2, 3, 5, 5));
        numberFields = new JTextField[6];
        for (int i = 0; i < numberFields.length; i++) {
            numberFields[i] = new JTextField("0");
            numberFields[i].setHorizontalAlignment(SwingConstants.CENTER);
            numberFields[i].setEditable(false);
            centerPanel.add(numberFields[i]);
        }
        add(centerPanel, BorderLayout.CENTER);

        // Bottom panel for buttons
        JPanel bottomPanel = new JPanel();
        luckyNumbersButton = new JButton("Lucky Numbers!");
        colorButton = new JButton("Color");

        bottomPanel.add(luckyNumbersButton);
        bottomPanel.add(colorButton);
        add(bottomPanel, BorderLayout.SOUTH);

        // Button listeners
        luckyNumbersButton.addActionListener(e -> generateLuckyNumbers());
        colorButton.addActionListener(e -> changeColor());
    }

    private void generateLuckyNumbers() {
        for (JTextField field : numberFields) {
            int randomNum = (int) (Math.random() * 50) + 1; // Random numbers between 1 and 50
            field.setText(String.valueOf(randomNum));
        }
    }

    private void changeColor() {
        Color randomColor = new Color((int) (Math.random() * 255),
                                      (int) (Math.random() * 255),
                                      (int) (Math.random() * 255));
        label.setForeground(randomColor);
    }

    public static void main(String[] args) {
        // Create and display the frame
        Lottery frame = new Lottery();
        frame.setTitle("Lottery Frame");
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
