import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

public class FrameJPEGKaydet {
	
	public static String masaustuYolunuAl() {
	    
		// Kullanıcının masaüstü klasörünün yolunu get() metodu ile aliyoruz.
	   
	    Path masaustuYolu = Paths.get(System.getProperty("user.home"), "Desktop");
	    
	    // Daha sonra, bulunan yolu String olarak dönüştürmek için toString() yöntemini kullanırız.
	    String desktopPathString = masaustuYolu.toString();
	    
	    // Son olarak, bulunan masaüstü yolunu döndürürüz.

	    return desktopPathString;
	  }
	
  
	public static JLabel resimEkle(int w, int h) throws IOException {
		
		// Rastgele bir resmi aliyoruz istenen olcude
		URL imageUrl = new URL("https://picsum.photos/"+w+"/"+h);
		Image resim = ImageIO.read(imageUrl);
		
		// Resmi frame'e ekliyoruz
		JLabel label = new JLabel(new ImageIcon(resim));
		label.setBounds(0,0,w,h);
		
		return label;
	}
	
	public FrameJPEGKaydet() throws IOException {
	  
	  // Frame olusturuyoruz
	  JFrame frame = new JFrame();
	  frame.setSize(400, 400);
	  frame.setLayout(null);
	  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	  frame.setLocationRelativeTo(null);
	  
	  JPanel panel = new JPanel();
	  panel.setLayout(null);
	  panel.setBounds(0,0,400,400);
	  
	  panel.add(resimEkle(400,400));
	
	  // Sağ tıklama menüsü olusturuyoruz.
      JPopupMenu menu = new JPopupMenu();
      
      JMenuItem saveAsJpeg = new JMenuItem("Save as JPEG");
      JMenuItem saveAsPng = new JMenuItem("Save as PNG");
      JMenuItem menuArasi = new JMenuItem("----------");
      JMenuItem yeniResim1 = new JMenuItem("Yeni Resim (400x400 px)");
      JMenuItem yeniResim2 = new JMenuItem("Yeni Resim (800x600 px)");
      JMenuItem yeniResim3 = new JMenuItem("Yeni Resim (416x312 px)");

      saveAsJpeg.addActionListener(new ActionListener() {
    	  public void actionPerformed(ActionEvent e) {
   
    		  // Resmi jpeg olarak kaydetme
    		  
    		  try {
    			
    			  BufferedImage bufferedImage = new BufferedImage(panel.getWidth(), panel.getHeight(), 
    					  BufferedImage.TYPE_INT_RGB);
         
    			  Graphics g = bufferedImage.getGraphics();
                  
    			  panel.paint(g);
    			  
    			  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-d HH-mm-ss");
    			  
    			  Date date = new Date();
                  
    			  //Screenshot from 2022-12-18 11-03-32
    			  ImageIO.write(bufferedImage, "jpeg", new File(masaustuYolunuAl()+"/Kida/resim-"+dateFormat.format(date)+".jpg"));
    			  
    		  } catch (Exception ex) {
          
    			  ex.printStackTrace();
       
    		  }
    		  
    	  }
    	  
      });
   
      saveAsPng.addActionListener(new ActionListener() {
    	  public void actionPerformed(ActionEvent e) {
   
    		  // Resmi png olarak kaydetme
    		  
    		  try {
    			
    			  BufferedImage bufferedImage = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_RGB);
         
    			  Graphics g = bufferedImage.getGraphics();
                  
    			  panel.paint(g);
    			  
    			  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-d HH-mm-ss");
    			  
    			  Date date = new Date();
                  
    			  //Resim kayit dosya ismini ayarliyoruz: resim-2022-12-18 11-03-32.jpg
    			  ImageIO.write(bufferedImage, "png", new File(masaustuYolunuAl()+"/Kida/resim-"+dateFormat.format(date)+".png"));
    			  
    		  } catch (Exception ex) {
          
    			  ex.printStackTrace();
       
    		  }
    		  
    	  }
    	  
      });
   
      
      yeniResim1.addActionListener(new ActionListener() {
    	 public void actionPerformed(ActionEvent event) {
    		 
    		 panel.removeAll();
    		
    		 try {
				panel.add(resimEkle(400,400));
			} catch (IOException e) {
				e.printStackTrace();
			}
    	     
    		 frame.setSize(400,400);
    		 panel.setSize(400,400);

    		 frame.repaint();

    	 }
      });
      
      yeniResim2.addActionListener(new ActionListener() {
     	 public void actionPerformed(ActionEvent event) {
     		 
     		 panel.removeAll();
     		
     		 try {
 				panel.add(resimEkle(800,600));
 			} catch (IOException e) {
 				e.printStackTrace();
 			}
     	     
    		 frame.setSize(800,600);
    		 panel.setSize(800,600);
    		 
     		 frame.repaint();

     	 }
       });
      
      yeniResim3.addActionListener(new ActionListener() {
      	 public void actionPerformed(ActionEvent event) {
      		 
      		 panel.removeAll();
      		
      		 try {
  				panel.add(resimEkle(416,312));
  			} catch (IOException e) {
  				e.printStackTrace();
  			}
      	     
     		 frame.setSize(416,312);
    		 panel.setSize(416,312);
    		 
      		 frame.repaint();

      	 }
        });
       
      
      menu.add(saveAsJpeg);
      menu.add(saveAsPng);
      menu.add(menuArasi);
      menu.add(yeniResim1);
      menu.add(yeniResim2);
      menu.add(yeniResim3);

      // Sağ tıklama menüsünü ekleyin
      panel.setComponentPopupMenu(menu);

      frame.add(panel);
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      
  }
}






