import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.TitledBorder;

public class GaleriJRadioButton {

	public static JFrame frame = new JFrame("JRadioButton Gallery | Java Blackboard [093]");
	
	public static final String RESIM_DOSYASI = "src" + File.separator +"images";  // src/images -> .jpg
    
    public static JLabel resimLabel = new JLabel();;
    
    public static List<JRadioButton> raidoButonlar;
    public static ButtonGroup buttonGroup;

    public GaleriJRadioButton() {
    	    	
    	frame.setLayout(null); 
    	
        butonPaneli();
        resmiGuncelle();
        
        frame.setSize(720,390);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
    }

    public static void butonPaneli() {
       
    	buttonGroup = new ButtonGroup();
    	raidoButonlar = new ArrayList<>();

        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setLayout(null);
        buttonsPanel.setBackground(new Color(101, 125, 140));
        buttonsPanel.setBounds(20,10,230,342);
        
        frame.add(buttonsPanel);
        
        int x = 10;
        int y = 10;
        int w = 50;
        int h = 50;
        int xFark = 55;
        int yFark = 70;

        for (int i = 1 ; i<= 20; i++) {
           
        	JRadioButton button = new JRadioButton(String.valueOf(i));
            
        	button.setOpaque(false);
        	button.setForeground(Color.white);
        	button.setBounds(x,y,w,h); x = x + xFark;
        	
        	if(i%4 == 0) {
        		
        		x = 10;
        		y = y + yFark;
        	}
        	
        	buttonGroup.add(button);
        	buttonsPanel.add(button);
        	raidoButonlar.add(button);
        	
        	button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
            
                	resmiGuncelle();
                
                }
            });
        
        }
    }

    public static void resmiGuncelle() {
    	
    	JPanel resimPaneli = new JPanel();
    	resimPaneli.setLayout(null);
    	resimPaneli.setBackground(new Color(63, 85, 89));
    	resimPaneli.setBounds(260,10,446,342);
    	
    	frame.add(resimPaneli);
    	
        int secilenRadioButon = -1; //-1 durumunda seçili buton yok
        
        for (int i = 0; i < raidoButonlar.size(); i++) {
        
        	if (raidoButonlar.get(i).isSelected()) {
            
        		secilenRadioButon = i;
                
        		break;
            }
        }
        
        if (secilenRadioButon == -1) {
          
        	// Hiçbir buton seçili değilse, ilk butonu seçili yap
            
        	raidoButonlar.get(0).setSelected(true);
        	secilenRadioButon = 0;
        }
        
        String resimDosyaAdi = RESIM_DOSYASI + File.separator + (secilenRadioButon + 1) + ".jpg";
        
        ImageIcon resim = new ImageIcon(resimDosyaAdi);
        
        TitledBorder dosyaAdiBaslik = BorderFactory.createTitledBorder("Dosya : " + (secilenRadioButon+1) + ".JPG");
        dosyaAdiBaslik.setTitleFont(new Font("Helvetica", Font.BOLD, 13));
        dosyaAdiBaslik.setTitleColor(Color.WHITE);
        
    	resimPaneli.setBorder(dosyaAdiBaslik);
        
        resimLabel.setIcon(resim);
        resimLabel.setBounds(15,15,416,312);
        resimPaneli.add(resimLabel);
    }

}
