Tugas 7 PBO C Image Viewer

<p>Tugas 7 PBO C Image Viewer</p>

Nama : Dewangga Dharmawan

NRP : 05111940000029

Kelas : PBO C

Dosen : Fajar Baskoro

Diagram Class

Class OFImage

 /**  
  * Class ini mendefinisikan gambar dalam format Object First  
  *  
  * @author (Dewangga Dharmawan)  
  * @version (07/12/2020)  
  */  
 import java.awt.*;  
 import java.awt.image.*;  
 import javax.swing.*;  
 public class OFImage extends BufferedImage  
 {  
   //Membuat salinan gambar object first dari buffer  
   public OFImage(BufferedImage image)  
   {  
     super(image.getColorModel(), image.copyData(null), image.isAlphaPremultiplied(), null);  
   }  
   //Menentukan gambar object first dengan besar tertentu  
   //Denga warna yang bisa diubah  
   public OFImage(int width, int height)  
   {  
     super(width, height, TYPE_INT_RGB);  
   }  
   //Menentukan warna masing-masing pixel dalam gambar  
   public void setPixel(int x, int y, Color col)  
   {  
     int pixel = col.getRGB();  
     setRGB(x, y, pixel);  
   }  
   //Mendapatkan warna dari masing-masing pixel  
   public Color getPixel(int x, int y)  
   {  
     int pixel = getRGB(x, y);  
     return new Color(pixel);  
   }  
   //Mengubah gambar menjadi lebih gelap  
   public void darker()  
   {  
     int height = getHeight();  
     int width = getWidth();  
     for(int y = 0; y < height; y++)  
     for(int x = 0; x < width; x++)  
     setPixel(x, y, getPixel(x, y).darker());  
   }  
   //Mengubah gambar menjadi lebih terang  
   public void lighter()  
   {  
     int height = getHeight();  
     int width = getWidth();  
     for(int y = 0; y < height; y++)  
     for(int x = 0; x < width; x++)  
     setPixel(x, y, getPixel(x, y).brighter());  
   }  
   //Mengubah warna gambar menjadi hitam, putih, atau abu-abu  
   public void threshold()  
   {  
     int height = getHeight();  
     int width = getWidth();  
     for(int y = 0; y < height; y++)  
     for(int x = 0; x < width; x++)  
     {  
       Color pixel = getPixel(x, y);  
       int brightness = (pixel.getRed() + pixel.getBlue() + pixel.getGreen());  
       if(brightness <= 85)  
       setPixel(x, y, Color.BLACK);  
       else if(brightness <= 170)  
       setPixel(x, y, Color.GRAY);  
       else  
       setPixel(x, y, Color.WHITE);  
     }  
   }  
   //Menguah warna menjadi warna lainnya (Opsi custom)  
   public void colorbaru()  
   {  
     int height = getHeight();  
     int width = getWidth();  
     for(int y = 0; y < height; y++)  
     for(int x = 0; x < width; x++)  
     {  
       Color titik = getPixel(x, y);  
       int merah = titik.getRed();  
       int biru = titik.getBlue();  
       int hijau = titik.getGreen();  
       if(merah >= 128)  
       {  
         if(biru >= 128 && hijau >= 128)  
         setPixel(x, y, Color.WHITE);  
         else  
         {  
           if(biru >= 128)  
           setPixel(x, y, Color.MAGENTA);  
           else if(hijau >= 128)  
           setPixel(x, y, Color.ORANGE);  
           else  
           setPixel(x, y, Color.RED);  
         }  
       }  
       else  
       {  
         if(biru >= 128)  
         {  
           if(hijau >= 128)  
           setPixel(x, y, Color.YELLOW);  
           else  
           setPixel(x, y, Color.CYAN);  
         }  
         else  
         {  
           if(hijau >= 128)  
           setPixel(x, y, Color.GREEN);  
           else  
           setPixel(x, y, Color.BLACK);  
         }  
       }  
     }  
   }  
 }  

Class ImagePanel

 /**  
  * Class ini berguna untuk menampilkan gambar object first  
  *  
  * @author (Dewangga Dharmawan)  
  * @version (7/12/2020)  
  */  
 import java.awt.*;  
 import javax.swing.*;  
 import java.awt.image.*;  
 public class ImagePanel extends JComponent  
 {  
   //Panjang dan lebar aplikasi  
   private int width, height;  
   //Buffer untuk peng-edit-an  
   private OFImage panelImage;  
   //Metode untuk membuat gambar kosong  
   //Sebelum ada filenya  
   public ImagePanel()  
   {  
     width = 360;  
     height = 240;  
     panelImage = null;  
   }  
   //Menampilkan gambar yang akan ditampilkan  
   public void setImage(OFImage image)  
   {  
     if(image != null)  
     {  
       width = image.getWidth();  
       height = image.getHeight();  
       panelImage = image;  
       repaint();  
     }  
   }  
   //Menghapus gambar yang ditampilkan  
   public void clearImage()  
   {  
     Graphics imageGraphics = panelImage.getGraphics();  
     imageGraphics.setColor(Color.LIGHT_GRAY);  
     imageGraphics.fillRect(0,0,width,height);  
     repaint();  
   }  
   //Menentukan seberapa besar gambar yang diinginkan  
   public Dimension getPreferredSize()  
   {  
     return new Dimension(width, height);  
   }  
   //Menyalin gambar ke layar  
   public void paintComponent(Graphics g)  
   {  
     Dimension size = getSize();  
     g.clearRect(0,0,size.width, size.height);  
     if(panelImage != null)  
     g.drawImage(panelImage,0,0,null);  
   }  
 }  

Class ImageFileManager

 /**  
  * Class ini digunakan untuk menerima dan menyimpan gambar  
  *  
  * @author (Dewangga Dharmawan)  
  * @version (7/12/2020)  
  */  
 import java.awt.image.*;  
 import javax.imageio.*;  
 import java.io.*;  
 public class ImageFileManager  
 {  
   //Konstan format yang digunakan  
   private static final String IMAGE_FORMAT = "jpg";  
   /*  
    * Membaca gambar yang dipilih dari memori komputer.  
    * Jika tidak ada, maka dikembalikan null  
    */  
   public static OFImage loadImage(File imageFile)  
   {  
     try  
     {  
       BufferedImage image = ImageIO.read(imageFile);  
       if(image == null || (image.getWidth(null)<0))  
       {  
         return null;  
       }  
       return new OFImage(image);  
     }  
     catch(IOException exc){  
     return null;}  
   }  
   //Menuliskan gambar yang dipilih untuk ditampilkan  
   public static void saveImage(OFImage image, File file)  
   {  
     try  
     {  
       ImageIO.write(image, IMAGE_FORMAT, file);  
     }  
     catch(IOException exc)  
     {  
       return;  
     }  
   }  
 }  

Class ImageViewer

 /**  
  * Class ini berguna sebagai main class aplikasi ini  
  *  
  * @author (Dewangga Dharmawan)  
  * @version (8/12/2020)  
  */  
 import java.awt.*;  
 import java.awt.event.*;  
 import java.awt.image.*;  
 import javax.swing.*;  
 import java.io.File;  
 public class ImageViewer  
 {  
   //Deklarasi beberapa variabel konstan dan bebas  
   //Yang digunakan dalam class ini  
   private static final String VERSION = "Version 1.0";  
   private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));  
   private JFrame frame;  
   private ImagePanel imagePanel;  
   private JLabel filenameLabel;  
   private JLabel statusLabel;  
   private OFImage currentImage;  
   //Memulai aplikasinya  
   public ImageViewer()  
   {  
     currentImage = null;  
     makeFrame();  
   }  
   //Membuka file dalam komputer  
   private void openFile()  
   {  
     int returnVal = fileChooser.showOpenDialog(frame);  
     if(returnVal != JFileChooser.APPROVE_OPTION)  
     return;  
     File selectedFile = fileChooser.getSelectedFile();  
     currentImage = ImageFileManager.loadImage(selectedFile);  
     if(currentImage == null)  
     {  
       JOptionPane.showMessageDialog(frame, "File ini bukan dalam bentuk .jpg atau .png", "Image Load Error", JOptionPane.ERROR_MESSAGE);  
       return;  
     }  
     imagePanel.setImage(currentImage);  
     showFilename(selectedFile.getPath());  
     showStatus("File loaded");  
     frame.pack();  
   }  
   //Menutup gambar yang ditampilkan  
   private void close()  
   {  
     currentImage = null;  
     imagePanel.clearImage();  
     showFilename(null);  
   }  
   //Keluar dari aplikasi  
   private void quit()  
   {  
     System.exit(0);  
   }  
   //Menggelapkan gambar  
   private void makeDarker()  
   {  
     if(currentImage != null)  
     {  
       currentImage.darker();  
       frame.repaint();  
       showStatus("Gambar tergelapkan");  
     }  
     else  
     showStatus("Tidak ada gambar");  
   }  
   //Menerangkan gambar  
   private void makeLighter()  
   {  
     if(currentImage != null)  
     {  
       currentImage.lighter();  
       frame.repaint();  
       showStatus("Gambar tercerahkan");  
     }  
     else  
     showStatus("Tidak ada gambar");  
   }  
   //Menerangkan gambar  
   private void threshold()  
   {  
     if(currentImage != null)  
     {  
       currentImage.threshold();  
       frame.repaint();  
       showStatus("Gambar di hitam-putih kan");  
     }  
     else  
     showStatus("Tidak ada gambar");  
   }  
   //Filter Custom kreasi  
   private void makecolorbaru()  
   {  
     if(currentImage != null)  
     {  
       currentImage.colorbaru();  
       frame.repaint();  
       showStatus("Membuat gambar dengan warna baru");  
     }  
     else  
     showStatus("Tidak ada gambar");  
   }  
   //Menampilkan versi aplikasi  
   private void showAbout()  
   {  
     JOptionPane.showMessageDialog(frame, "ImageViewer\n" + VERSION, "Image Viewer, melihat dan mengubah gambar", JOptionPane.INFORMATION_MESSAGE);  
   }  
   //Menampilkan nama file, jika ada  
   private void showFilename(String filename)  
   {  
     if(filename == null)  
     filenameLabel.setText("Tidak ada nama file");  
     else  
     filenameLabel.setText("Nama File: " + filename);  
   }  
   //Menunjukkan pesan status  
   private void showStatus(String text)  
   {  
     statusLabel.setText(text);  
   }  
   //Membuat Swing Frame  
   private void makeFrame()  
   {  
     frame = new JFrame("ImageViewer");  
     makeMenuBar(frame);  
     Container contentPane = frame.getContentPane();  
     contentPane.setLayout(new BorderLayout(6,6));  
     filenameLabel = new JLabel();  
     contentPane.add(filenameLabel, BorderLayout.NORTH);  
     imagePanel = new ImagePanel();  
     contentPane.add(imagePanel, BorderLayout.CENTER);  
     statusLabel = new JLabel(VERSION);  
     contentPane.add(statusLabel, BorderLayout.SOUTH);  
     showFilename(null);  
     frame.pack();  
     Dimension d = Toolkit.getDefaultToolkit().getScreenSize();  
     frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);  
     frame.setVisible(true);  
   }  
   //Membuat frame menu  
   private void makeMenuBar(JFrame frame)  
   {  
     final int SHORTCUT_MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();  
     JMenuBar menubar = new JMenuBar();  
     frame.setJMenuBar(menubar);  
     JMenu menu;  
     JMenuItem item;  
     menu = new JMenu("File");  
     menubar.add(menu);  
     item = new JMenuItem("Buka");  
     item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, SHORTCUT_MASK));  
     item.addActionListener(new ActionListener() {  
       public void actionPerformed(ActionEvent e) {openFile();}});  
     menu.add(item);  
     menu.addSeparator();  
     item = new JMenuItem("Tutup Gambar");  
     item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, SHORTCUT_MASK));  
     item.addActionListener(new ActionListener(){  
       public void actionPerformed(ActionEvent e) {close();}});  
     menu.add(item);  
     menu.addSeparator();  
     item = new JMenuItem("Keluar");  
     item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));  
     item.addActionListener(new ActionListener(){  
       public void actionPerformed(ActionEvent e) {quit();}});  
     menu.add(item);  
     menu = new JMenu("Filter");  
     menubar.add(menu);  
     item = new JMenuItem("Gelapkanlah");  
     item.addActionListener(new ActionListener() {  
       public void actionPerformed(ActionEvent e) {makeDarker();}});  
     menu.add(item);  
     item = new JMenuItem("Terangkanlah");  
     item.addActionListener(new ActionListener() {  
       public void actionPerformed(ActionEvent e) {makeLighter();}});  
     menu.add(item);  
     item = new JMenuItem("Hitam-putihkan");  
     item.addActionListener(new ActionListener() {  
       public void actionPerformed(ActionEvent e) {threshold();}});  
     menu.add(item);  
     item = new JMenuItem("Color Baru (Press me)");  
     item.addActionListener(new ActionListener() {  
       public void actionPerformed(ActionEvent e) {makecolorbaru();}});  
     menu.add(item);  
     menu = new JMenu("Bantuan");  
     menubar.add(menu);  
     item = new JMenuItem("Tentang ImageViewer");  
     item.addActionListener(new ActionListener() {  
       public void actionPerformed(ActionEvent e) {showAbout();}});  
     menu.add(item);  
   }  
 }  

Gambar percobaan

Penerangan

Penggelapan

Hitam-putih (Threshold)

Custom (Colorbaru)

Komentar

Postingan populer dari blog ini

Tugas 5 PBO C Tech Support System

Tugas 10 PBO C Text Editor

Tugas 3 PBO C Jam Masjid