mbah_bejo

detailPanel

Jan 11th, 2021
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.84 KB | None | 0 0
  1. /**
  2.  *   Class ini memiliki banyak method penting
  3.  *   untuk menjalankan program ini, seperti tombol
  4.  *   laber, slider, file chooser
  5.  *
  6.  *  Author  : Thomas Dwi Awaka
  7.  *  Version : 1
  8.  */
  9.  
  10. package twibboner;
  11.  
  12. import javax.imageio.ImageIO;
  13. import javax.swing.*;
  14. import javax.swing.event.ChangeEvent;
  15. import javax.swing.event.ChangeListener;
  16. import java.awt.*;
  17. import java.awt.GridBagLayout;
  18. import java.awt.event.ActionEvent;
  19. import java.awt.event.ActionListener;
  20. import java.awt.image.BufferedImage;
  21. import java.io.File;
  22. import javax.swing.filechooser.FileFilter;
  23. import java.io.IOException;
  24.  
  25. public class detailPanel extends JPanel{
  26.     private frameUtama frameUtama;
  27.     private display display = new display();
  28.  
  29.     //Komponen
  30.     private JLabel labelPilihGambar = new JLabel("Tidak ada file terpilih");
  31.     private JButton tombolPilihGambar = new JButton("Pilih Foto");
  32.     private JLabel labelPilihTwibbon = new JLabel("Tidak ada file terpilih");
  33.     private JButton tombolPilihTwibbon = new JButton("Pilih Twibbon");
  34.     private JLabel labelSliderOpacity = new JLabel("Opacity");
  35.     private JSlider sliderOpacity = new JSlider(0, 100, 50);
  36.     private JLabel labelSliderUkuran = new JLabel("Ukuran");
  37.     private JSlider sliderUkuran = new JSlider(10, 110, 65);
  38.     private JCheckBox checkBoxTengah = new JCheckBox("Tengah", true);
  39.     private JCheckBox checkBoxAtasKiri = new JCheckBox("Atas Kiri", false);
  40.     private JCheckBox checkBoxAtasKanan = new JCheckBox("Atas Kanan", false);
  41.     private JCheckBox checkBoxBawahKiri = new JCheckBox("Bawah Kiri", false);
  42.     private JCheckBox checkBoxBawahKanan = new JCheckBox("Bawah Kanan", false);
  43.     private JButton tombolApply = new JButton("Apply");
  44.     private JButton tombolSimpan = new JButton("Simpan");
  45.     private JLabel labelTombolSimpan = new JLabel();
  46.  
  47.     static String pesanSistem;
  48.  
  49.     //constructor
  50.     public detailPanel(frameUtama frameUtama) {
  51.         this.frameUtama = frameUtama;
  52.  
  53.         twibbon.ukuran = 100;
  54.         twibbon.opacity = 100;
  55.  
  56.         //action listening
  57.         fileListener(tombolPilihGambar, labelPilihGambar);
  58.         fileListener(tombolPilihTwibbon, labelPilihTwibbon);
  59.         checkBoxListener(checkBoxTengah,checkBoxAtasKanan,
  60.                 checkBoxAtasKiri,checkBoxBawahKanan,checkBoxBawahKiri);
  61.         sliderListener(sliderUkuran);
  62.         sliderListener(sliderOpacity);
  63.         applyListener(tombolApply);
  64.         saveListener(tombolSimpan,labelTombolSimpan);
  65.  
  66.         tambahKomponen();
  67.         pesanSistem = "Masukkan Foto";
  68.         display.draw();
  69.  
  70.     }
  71.  
  72.     /**
  73.      * method ini memakai grid
  74.      * sehingga setiap tombol,label, dan slidernya
  75.      * bisa tersusun rapi
  76.      */
  77.     public void tambahKomponen(){
  78.  
  79.         GridBagConstraints c = new GridBagConstraints();
  80.         setLayout(new GridBagLayout());
  81.  
  82.         // bagian memilih foto
  83.         c.gridy = 0;
  84.         add(tombolPilihGambar, c);
  85.         c.gridy = 1;
  86.         add(labelPilihGambar, c);
  87.  
  88.         // bagian memilih twibbon/logo/watermark
  89.         c.insets = new Insets(20, 0, 0, 0);
  90.         c.gridy = 2;
  91.         add(tombolPilihTwibbon, c);
  92.  
  93.         c.insets = new Insets(1, 0, 0, 0);
  94.         c.gridy = 3;
  95.         add(labelPilihTwibbon, c);
  96.  
  97.         // bagian slider
  98.         c.insets = new Insets(30, 0, 0, 0);
  99.         c.gridy = 4;
  100.         add(labelSliderUkuran, c);
  101.         c.insets = new Insets(5, 0, 0, 0);
  102.         c.gridy = 5;
  103.         add(sliderUkuran, c);
  104.         c.insets = new Insets(10, 0, 0, 0);
  105.         c.gridy = 6;
  106.         add(labelSliderOpacity, c);
  107.         c.insets = new Insets(5, 0, 0, 0);
  108.         c.gridy = 7;
  109.         add(sliderOpacity, c);
  110.  
  111.         // bagian checkbox
  112.         c.gridy = 8;
  113.         add(checkBoxTengah, c);
  114.         c.gridy = 9;
  115.         add(checkBoxAtasKiri, c);
  116.         c.gridy = 10;
  117.         add(checkBoxAtasKanan, c);
  118.         c.gridy = 11;
  119.         add(checkBoxBawahKiri, c);
  120.         c.gridy = 12;
  121.         add(checkBoxBawahKanan, c);
  122.  
  123.         // bagian apply dan simpan
  124.         c.insets = new Insets(10, 0, 0, 0);
  125.         c.gridy = 13;
  126.         add(tombolApply, c);
  127.         c.gridy = 15;
  128.         c.insets = new Insets(10, 0, 0, 0);
  129.         add(tombolSimpan, c);
  130.     }
  131.  
  132.     /**
  133.      *  method untuk
  134.      * @return display ke frame utama
  135.      */
  136.     public display getDisplay() {
  137.         return display;
  138.     }
  139.  
  140.     /**
  141.      * method ini menjalankan method pack dalam Java.AWT.Window
  142.      * untuk menjaga ukuran dari frame yang sudah ditetapkan, seperti setSize()
  143.      */
  144.     public void pack() {
  145.         this.frameUtama.pack();
  146.     }
  147.  
  148.     /**
  149.      * method untuk listener saat tombol melakukan action (ditekan)
  150.      * @param btn jenis tombol
  151.      * @param label jenis label pada tombol
  152.      */
  153.     public void fileListener(JButton btn, JLabel label){
  154.         btn.addActionListener(new ActionListener() {
  155.             @Override
  156.             public void actionPerformed(ActionEvent e) {
  157.                 try {
  158.  
  159.                     // untuk membuka FileDialog yang berfungsi untuk meng-input gambar
  160.                     FileDialog dialog = new FileDialog((Frame) null, "Pilih File untuk dibuka");
  161.                     dialog.setMode(FileDialog.LOAD);
  162.                     dialog.setVisible(true);
  163.  
  164.                     // tipe data yang digunakan adalah .png / .jpg
  165.                     if (dialog.getFile().endsWith(".png") || dialog.getFile().endsWith(".jpg")
  166.                             || dialog.getFile().endsWith(".PNG") || dialog.getFile().endsWith(".JPG")) {
  167.                         label.setText(dialog.getFile());
  168.                         label.setVisible(true);
  169.  
  170.                         //jika tombol yang ditekan adalah tombol untuk meng-input gambar
  171.                         if (btn == tombolPilihGambar) {
  172.                             if(twibbon.twbn!=null) pesanSistem = "Edit Foto";
  173.                             else pesanSistem = "Masukkan Twibbon";
  174.                             display.setGambar(dialog.getDirectory() + dialog.getFile());
  175.                             display.draw();
  176.                             twibbon.update();
  177.                             pack();
  178.                         }
  179.                         //jika tombol yang ditekan adalah tombol untuk meng-input twibbon
  180.                         if (btn == tombolPilihTwibbon) {
  181.                             display.setTwbn(dialog.getDirectory() + dialog.getFile());
  182.                             twibbon.update();
  183.                             pesanSistem = "Edit Foto";
  184.                             display.draw();
  185.                             pack();
  186.                         }
  187.                     }
  188.                     // jika file yang dimasukkan bukan .png / .jpg
  189.                     // akan menampilkan pesan diatas preview
  190.                     else {
  191.                         pesanSistem =("File harus format .png atau .jpg");
  192.                         display.draw();
  193.                     }
  194.                 } catch (NullPointerException nullPointerException) {
  195.                 }
  196.             }
  197.         });
  198.     }
  199.  
  200.     /**
  201.      * Tempat centang box, sebagai pengatur posisi twibbon
  202.      * @param checkBoxTengah     twibbon diposisi tengah
  203.      * @param checkBoxAtasKanan  twibbon diposisi atas kanan
  204.      * @param checkBoxAtasKiri   twibbon diposisi atas kiri
  205.      * @param checkBoxBawahKanan twibbon diposisi bawah kanan
  206.      * @param checkBoxBawahKiri  twibbon diposisi bawah kiri
  207.      */
  208.     public void checkBoxListener(JCheckBox checkBoxTengah,JCheckBox checkBoxAtasKanan,
  209.                                  JCheckBox checkBoxAtasKiri,JCheckBox checkBoxBawahKanan,JCheckBox checkBoxBawahKiri){
  210.         checkBoxTengah.addActionListener(new ActionListener() {
  211.             @Override
  212.             public void actionPerformed(ActionEvent e) {
  213.                 checkBoxAtasKanan.setSelected(false);
  214.                 checkBoxAtasKiri.setSelected(false);
  215.                 checkBoxBawahKiri.setSelected(false);
  216.                 checkBoxBawahKanan.setSelected(false);
  217.  
  218.                 if(!checkBoxTengah.isSelected() && !checkBoxAtasKanan.isSelected()
  219.                 && !checkBoxAtasKiri.isSelected() && !checkBoxBawahKanan.isSelected()
  220.                         && !checkBoxBawahKiri.isSelected())
  221.                     checkBoxTengah.setSelected(true);
  222.  
  223.                 twibbon.posisi = 1;
  224.             }
  225.         });
  226.  
  227.         checkBoxAtasKiri.addActionListener(new ActionListener() {
  228.             @Override
  229.             public void actionPerformed(ActionEvent e) {
  230.                 checkBoxAtasKanan.setSelected(false);
  231.                 checkBoxTengah.setSelected(false);
  232.                 checkBoxBawahKiri.setSelected(false);
  233.                 checkBoxBawahKanan.setSelected(false);
  234.  
  235.                 if(!checkBoxTengah.isSelected() && !checkBoxAtasKanan.isSelected()
  236.                         && !checkBoxAtasKiri.isSelected() && !checkBoxBawahKanan.isSelected()
  237.                         && !checkBoxBawahKiri.isSelected())
  238.                     checkBoxAtasKiri.setSelected(true);
  239.  
  240.                 twibbon.posisi = 2;
  241.             }
  242.         });
  243.  
  244.         checkBoxAtasKanan.addActionListener(new ActionListener() {
  245.             @Override
  246.             public void actionPerformed(ActionEvent e) {
  247.                 checkBoxTengah.setSelected(false);
  248.                 checkBoxAtasKiri.setSelected(false);
  249.                 checkBoxBawahKiri.setSelected(false);
  250.                 checkBoxBawahKanan.setSelected(false);
  251.  
  252.                 if(!checkBoxTengah.isSelected() && !checkBoxAtasKanan.isSelected()
  253.                         && !checkBoxAtasKiri.isSelected() && !checkBoxBawahKanan.isSelected()
  254.                         && !checkBoxBawahKiri.isSelected())
  255.                     checkBoxAtasKanan.setSelected(true);
  256.  
  257.                 twibbon.posisi = 3;
  258.             }
  259.         });
  260.  
  261.         checkBoxBawahKiri.addActionListener(new ActionListener() {
  262.             @Override
  263.             public void actionPerformed(ActionEvent e) {
  264.                 checkBoxAtasKanan.setSelected(false);
  265.                 checkBoxAtasKiri.setSelected(false);
  266.                 checkBoxTengah.setSelected(false);
  267.                 checkBoxBawahKanan.setSelected(false);
  268.  
  269.                 if(!checkBoxTengah.isSelected() && !checkBoxAtasKanan.isSelected()
  270.                         && !checkBoxAtasKiri.isSelected() && !checkBoxBawahKanan.isSelected()
  271.                         && !checkBoxBawahKiri.isSelected())
  272.                     checkBoxBawahKiri.setSelected(true);
  273.  
  274.                 twibbon.posisi = 4;
  275.             }
  276.         });
  277.  
  278.         checkBoxBawahKanan.addActionListener(new ActionListener() {
  279.             @Override
  280.             public void actionPerformed(ActionEvent e) {
  281.                 checkBoxAtasKanan.setSelected(false);
  282.                 checkBoxAtasKiri.setSelected(false);
  283.                 checkBoxTengah.setSelected(false);
  284.                 checkBoxBawahKiri.setSelected(false);
  285.  
  286.                 if(!checkBoxTengah.isSelected() && !checkBoxAtasKanan.isSelected()
  287.                         && !checkBoxAtasKiri.isSelected() && !checkBoxBawahKanan.isSelected()
  288.                         && !checkBoxBawahKiri.isSelected())
  289.                     checkBoxBawahKanan.setSelected(true);
  290.  
  291.                 twibbon.posisi = 5;
  292.             }
  293.         });
  294.     }
  295.  
  296.  
  297.     /**
  298.      * Method ini menggunakan slider untuk mengatur opacity dan ukuran
  299.      * dari twibbon
  300.      * @param slider pengatur tingkat opacity dan ukuran twibbon
  301.      */
  302.     public void sliderListener(JSlider slider){
  303.         slider.addChangeListener(new ChangeListener() {
  304.             @Override
  305.             public void stateChanged(ChangeEvent e) {
  306.                 if(slider == sliderUkuran) twibbon.ukuran = ((JSlider) e.getSource()).getValue();
  307.  
  308.                 if(slider == sliderOpacity) twibbon.opacity = ((JSlider) e.getSource()).getValue();
  309.             }
  310.         });
  311.     }
  312.  
  313.     /**
  314.      * mengaplikasikan setiap perubahan yang dilakukan user
  315.      * pada hasil gambar
  316.      * @param tombol membaca action saat ditekan oleh user
  317.      */
  318.     public void applyListener(JButton tombol){
  319.         tombol.addActionListener(new ActionListener() {
  320.             @Override
  321.             public void actionPerformed(ActionEvent e) {
  322.                 twibbon.update();
  323.                 display.draw();
  324.                 pack();
  325.             }
  326.         });
  327.     }
  328.  
  329.     /**
  330.      * Untuk menjalankan tombol Simpan, dan tentu saja ini untuk menyimpan gambar
  331.      * @param tombol membaca action saat menekan tombol save
  332.      * @param label sebagai penanda jika berhasil disimpan
  333.      */
  334.     public void saveListener(JButton tombol, JLabel label) {
  335.         tombol.addActionListener(new ActionListener() {
  336.             @Override
  337.             public void actionPerformed(ActionEvent e) {
  338.                 if (!pesanSistem.equals("")) {
  339.  
  340.                     BufferedImage bufferedImage = (BufferedImage) twibbon.getGambar();
  341.                    
  342.                     try {
  343.                         // file chooser dipakai untuk menyimpan hasil gambar
  344.                         JFrame parentFrame = new JFrame();
  345.                         JFileChooser fileChooser = new JFileChooser();
  346.                         fileChooser.setFileFilter(new PNGFileFilter());
  347.                         fileChooser.setDialogTitle("Pilih File untuk Menyimpan");
  348.  
  349.                         int userSelection = fileChooser.showSaveDialog(parentFrame);
  350.  
  351.                         //saat sudah memilih lokasi & nama file untuk disimpan
  352.                         if (userSelection == JFileChooser.APPROVE_OPTION) {
  353.                             File outputfile = fileChooser.getSelectedFile();
  354.  
  355.                             if(!outputfile.getName().endsWith(".png"))
  356.                             outputfile = new File(outputfile.getAbsolutePath() +".png");
  357.  
  358.                             //hasil gambar akan di-write dengan format .png, di direktori yang sudah dipilih user
  359.                             ImageIO.write(bufferedImage, "png", outputfile);
  360.                             pesanSistem = outputfile.getName() + " telah tersimpan di "
  361.                                     + outputfile.getAbsolutePath();
  362.                             display.draw();
  363.                         }
  364.  
  365.                     } catch (IOException e1) {
  366.                         pesanSistem = "Gambar Gagal Tersimpan";
  367.                         display.draw();
  368.                     }
  369.                     pack();
  370.                 }
  371.             }
  372.         });
  373.     }
  374.  
  375.     /**
  376.      * Method yang membuat filter file saat file akan disimpan
  377.      * kali ini menggunakan .png karena sesuai kebutuhan
  378.      */
  379.     private static class PNGFileFilter extends FileFilter
  380.     {
  381.         //mengisi format file saat disimpan nanti
  382.         public boolean accept(File file)
  383.         {
  384.  
  385.             return file.getName().toLowerCase().endsWith(".png") || file.isDirectory();
  386.         }
  387.  
  388.         public String getDescription()
  389.         {
  390.             return "PNG image  (*.png) ";
  391.         }
  392.     }
  393. }
Add Comment
Please, Sign In to add comment