package arabteam;

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

public class Main {

    public static void main(String[] args) {
        myframe mf = new myframe();
        mf.setDefaultCloseOperation(mf.DO_NOTHING_ON_CLOSE);
        mf.setVisible(true);
        exit e = new exit(mf);
        mf.addWindowListener(e);
    }
}

class myframe extends JFrame {

    public myframe() {
        setTitle("shape");
        setSize(300, 300);
        panel p = new panel();
        add(p);
    }
}

class panel extends JPanel {

    JButton b1;
    JButton b2;
    JButton b3;
    int shape;

    public panel() {
        b1 = new JButton("line");
        b2 = new JButton("rectangle");
        b3 = new JButton("oval");
        add(b1);
        add(b2);
        add(b3);

        b1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                shape = 1;
                repaint();
            }
        });

        b2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                shape = 2;
                repaint();
            }
        });

        b3.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                shape = 3;
                repaint();
            }
        });
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (shape == 1) {
            g.drawLine(10, 10, 100, 100);
        } else if (shape == 2) {
            for (int i = 0; i < 10; i++) {
                g.drawRect(50 + 10 * i, 50 + 10 * i, 50 + 10 * i, 50 + 10 * i);
            }
            repaint();
        } else if (shape == 3) {
            for (int i = 0; i < 10; i++) {
                g.drawOval(50 + 10 * i, 50 + 10 * i, 50 + 10 * i, 50 + 10 * i);
            }
            repaint();

        }
    }
}

class exit extends WindowAdapter {

    public exit(JFrame f) {
        frame = f;
    }

    public void windowClosing(WindowEvent e) {
        int x = JOptionPane.showConfirmDialog(frame, "do you wont to exit", "exit", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
        if (x == JOptionPane.OK_OPTION) {
            System.exit(0);
        } else {
            frame.setLocation(0, 0);
        }
    }
    private JFrame frame;
}
