📄 PaneliResimKaydet.java .java dosyası

⬇️ İndir
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 PaneliResimKaydet { public static String resimKayitKlasoru = "/Kida/"; // Klasor ismi... public static String masaustuYolu() { // Bu method, sistemde bulunan kullanıcının masaüstü klasörünün yolunu döndürür. Path masaustuYolu = Paths.get(System.getProperty("user.home"), "Desktop"); // Bulunan yolu String'e dönüştürüyoruz. String masaustuYoluString = masaustuYolu.toString(); return masaustuYoluString; } public static JLabel resimEkle(int w, int h) throws IOException { // Rastgele bir resim yukluyoruz. // Bu linkteki web sayfasi belirtilen genislik ve yukseklikte resim verir! URL imageUrl = new URL("https://picsum.photos/"+w+"/"+h); Image image = ImageIO.read(imageUrl); // Resmi label'e ekliyoruz JLabel label = new JLabel(new ImageIcon(image)); return label; } PaneliResimKaydet() throws IOException { JFrame frame = new JFrame(); frame.setSize(400, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); JPanel panel = new JPanel(); panel.add(resimEkle(400,400)); // Sağ tıklama menüsünü olusturuyoruz. JPopupMenu sagKlikMenu = new JPopupMenu(); JMenuItem saveAsJpeg = new JMenuItem("Save as JPEG"); JMenuItem saveAsPng = new JMenuItem("Save as PNG"); JMenuItem menuCizgi = new JMenuItem("------------"); JMenuItem newPicture = new JMenuItem("New Picture (400x400 px)"); JMenuItem newPicture2 = new JMenuItem("New Picture (800x600 px)"); saveAsJpeg.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // Resmi JPG olarak kaydetme try { // BufferedImage ile resmifrmae.den aliyoruz. BufferedImage bufferedImage = new BufferedImage(frame.getWidth(), frame.getHeight(), BufferedImage.TYPE_INT_RGB); // Graphics kutuphanesi yardimiyla grafik ture ceviriyoruz. Graphics g = bufferedImage.getGraphics(); // Su an bellekte resmimiz hazir. frame.paint(g); //Dosya ismini hazirliyoruz. //Dosya ismini su sekilde olmali : resim-2022-12-18 11-03-32.jpg // Once tarih kismini hazirlayalim SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-d HH-mm-ss"); Date date = new Date(); // Onune resim- ibaresini ekleyelim. String dosyaIsmi = "resim-" + dateFormat.format(date); ImageIO.write(bufferedImage, "jpeg", new File(masaustuYolu()+resimKayitKlasoru+dosyaIsmi+".jpg")); } catch (Exception ex) { // Herhangi bir hata olura buradan aliyoruz hata mesajini ex.printStackTrace(); } } }); saveAsPng.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // Resmi PNG olarak kaydetme try { // BufferedImage ile resmifrmae.den aliyoruz. BufferedImage bufferedImage = new BufferedImage(frame.getWidth(), frame.getHeight(), BufferedImage.TYPE_INT_RGB); // Graphics kutuphanesi yardimiyla grafik ture ceviriyoruz. Graphics g = bufferedImage.getGraphics(); // Su an bellekte resmimiz hazir. frame.paint(g); //Dosya ismini hazirliyoruz. //Dosya ismini su sekilde olmali : resim-2022-12-18 11-03-32.jpg // Once tarih kismini hazirlayalim SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-d HH-mm-ss"); Date date = new Date(); // Onune resim- ibaresini ekleyelim. String dosyaIsmi = "resim-" + dateFormat.format(date); ImageIO.write(bufferedImage, "png", new File(masaustuYolu()+resimKayitKlasoru+dosyaIsmi+".png")); } catch (Exception ex) { // Herhangi bir hata olura buradan aliyoruz hata mesajini ex.printStackTrace(); } } }); newPicture.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); frame.repaint(); } }); newPicture2.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); frame.repaint(); } }); // PopupMenu'ye ekliyoruz item'larimizi. sagKlikMenu.add(saveAsJpeg); sagKlikMenu.add(saveAsPng); sagKlikMenu.add(menuCizgi); sagKlikMenu.add(newPicture); sagKlikMenu.add(newPicture2); // Sağ tıklama menüsünü (PopupMenu) JPanel'e ekliyoruz panel.setComponentPopupMenu(sagKlikMenu); frame.add(panel); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }