
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;

public class RandomCheckBox {
	
	
	public static ArrayList<Integer> soruSayiListesi = new ArrayList<Integer>();
	public static JLabel etiketSayilar = new JLabel("0");
	public static JCheckBox[] cb = new JCheckBox[25];
	
	public static ArrayList<Integer> RandomSayiUret(int elemanSayisi) {

		ArrayList<Integer> rastgeleSayiListesi = new ArrayList<Integer>();
		
      	for (int i=0 ; i<elemanSayisi ; i++ ) {
		        
			int x = (int) (Math.random()*25)+1;
			rastgeleSayiListesi.add(x);	// 4   9   12  5
	    }
	
		for (int i=0 ; i<elemanSayisi ; i++) {
			
			for (int j=0 ; j<i ; j++) {
				
				if (j == i) {
					
				} else {
					if (rastgeleSayiListesi.get(i) == rastgeleSayiListesi.get(j)) {
						
						// Ayni olan elemani ArrayList'ten siliyoruz.
						// Silinen index'e yeni bir sayi ekliyoruz.
						rastgeleSayiListesi.remove(i);
						int x = (int) (Math.random()*25)+1;
						rastgeleSayiListesi.add(i, x);
						
						// Yeni eklenen sayinin tekrar kontrol edilmesi için 
						// i'yi bir eksiltip tekrar kontrol ediyoruz.
						i=i-1;
					}
				}
			}
		}
		
		return rastgeleSayiListesi;
	}
		
	public RandomCheckBox() {

		JFrame pencere = new JFrame("Rastgele Sayilar Listesi");
		pencere.setLayout(null); 
		
		pencere.add(sayiSec());
		
		pencere.setSize(400, 400);
		pencere.setLocationRelativeTo(null);
		pencere.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		pencere.setVisible(true);
	}

	public static JPanel sayiSec() {
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBackground(new Color(224, 242, 196));
		panel.setBounds(20,20,360,340);
		
		JLabel etiket;
		
		String[] rastgeleSayiAdedi = new String[23];        
	    
		rastgeleSayiAdedi[0] = "3 Elemanlı";
		rastgeleSayiAdedi[1] = "4 Elemanlı";
		rastgeleSayiAdedi[2] = "5 Elemanlı";
		rastgeleSayiAdedi[3] = "6 Elemanlı";
		rastgeleSayiAdedi[4] = "7 Elemanlı";
		rastgeleSayiAdedi[5] = "8 Elemanlı";
		rastgeleSayiAdedi[6] = "9 Elemanlı";
		rastgeleSayiAdedi[7] = "10 Elemanlı";
		rastgeleSayiAdedi[8] = "11 Elemanlı";
		rastgeleSayiAdedi[9] = "12 Elemanlı";
		rastgeleSayiAdedi[10] = "13 Elemanlı";
		rastgeleSayiAdedi[11] = "14 Elemanlı";
		rastgeleSayiAdedi[12] = "15 Elemanlı";
		rastgeleSayiAdedi[13] = "16 Elemanlı";
		rastgeleSayiAdedi[14] = "17 Elemanlı";
		rastgeleSayiAdedi[15] = "18 Elemanlı";
		rastgeleSayiAdedi[16] = "19 Elemanlı";
		rastgeleSayiAdedi[17] = "20 Elemanlı";
		rastgeleSayiAdedi[18] = "21 Elemanlı";
		rastgeleSayiAdedi[19] = "22 Elemanlı";
		rastgeleSayiAdedi[20] = "23 Elemanlı";
		rastgeleSayiAdedi[21] = "24 Elemanlı";
		rastgeleSayiAdedi[22] = "25 Elemanlı";
		
		int x = 15;
		int y = 50;
		int w = 50;
		int h = 20;
		int xf = 70;  	// yataydaki fark 
		int yf = 40;	// dikeydeki fark
		
		etiketSayilar.setHorizontalAlignment(JLabel.CENTER);
		etiketSayilar.setVerticalAlignment(JLabel.CENTER);
		etiketSayilar.setBounds(0,y,360,40);
		
		y = y + yf;
		
		for (int i=0 ; i<25 ; i++) {
			
			cb[i] = new JCheckBox(""+(i+1));
			
			if (!((i == 0)) && i%5 == 0) {
				
				x = 15; 
				y = y + yf;
			
			}

			cb[i].setBounds(x,y,w,h);
			x = x + xf;

		}
		
		JComboBox comboRandom = new JComboBox(rastgeleSayiAdedi);    
	    
		etiket = new JLabel("Rastgele sayi göster : ");
		etiket.setBounds(10,10,200,25);
		
		comboRandom.setBounds(220,10,130,25);
		
		comboRandom.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent event) {
            
            	// Bütün CheckBox'lari false isaretliyoruz.
            	for (int i=0 ; i<25 ; i++) cb[i].setSelected(false);
            	
            	String data = (String) comboRandom.getItemAt(comboRandom.getSelectedIndex());  
				int elemanSayisi = (comboRandom.getSelectedIndex())+3; // en az üç elemanli olmasini istiyoruz.
				
				soruSayiListesi = RandomSayiUret(elemanSayisi);
				etiketSayilar.setText(""+soruSayiListesi);
				
				// Random seçilen sayilarin CheckBox'larini true isaretliyoruz.
				for (int i=0 ; i<soruSayiListesi.size() ; i++) {
					
					cb[soruSayiListesi.get(i)-1].setSelected(true);
					
				}
				
            }
        });
		
		y = y + yf;
		
		JButton buton = new JButton("Göster");
		buton.setBounds(140,y,100,20);
		 
		buton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				
				System.out.println("Buton'a basiildi :) " + soruSayiListesi);
				
			}
			
		});
		 
		
		 panel.add(etiket);
		 panel.add(comboRandom);
		 panel.add(etiketSayilar);
		 
		 for (int i=0 ; i<25 ; i++) panel.add(cb[i]);
		 
		 panel.add(buton);
		
		 return panel;
		
	}
	
}