Tugas - Image Viewer

1. Class OFImage (untuk mengubah gambar)
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class OFImage extends BufferedImage {
    
    public OFImage(BufferedImage image) {
        super(image.getColorModel(), image.copyData(null), image.isAlphaPremultiplied(), null);
    }
    
    //Membuat OFImage dg ukuran tertentu tanpa isi
    public OFImage(int width, int height) {
        super(width, height, TYPE_INT_RGB);
    }
    
    //Menentukan warna suatu pixel dlm gambar
    public void setPixel(int x, int y, Color c) {
        int pixel = c.getRGB();
        setRGB(x,y,pixel);
    }
    
    //Mendapatkan warna dari suatu pixel
    public Color getPixel(int x,int y) {
        int pixel = getRGB(x,y);
        return new Color(pixel);
    }
    
    //Menggelapkan gambar
    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());
            }
        }
    }
    
    //Menerangkan gambar
    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());
            }
        }
    }
    
    //Monokrom
    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);
                }
            }
        }
    }
}

2. Class ImagePanel (untuk memuat gambar dalam panel frame)
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;

//Komponen swing yang dapat menampilkan OFImage
public class ImagePanel extends JComponent {
    
    private int width, height;
    //gambar yang ditampilkan
    
    private OFImage panelImage;
    
    public ImagePanel() {
        width = 360;
        height = 240;
        panelImage = null;
    }
    
    public void setImage(OFImage image) {
        if (image!=null) {
            width = image.getWidth();
            height = image.getHeight();
            panelImage = image;
            repaint();
        }
    }
    
    public void clearImage() {
        Graphics imageGraphics = panelImage.getGraphics();
        imageGraphics.setColor(Color.LIGHT_GRAY);
        imageGraphics.fillRect(0, 0, width, height);
        repaint();
    }
    
    public Dimension getPreferredSize() {
        return new Dimension(width, height);
    }
    
    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);
        }
    }
}

3. Class ImageFileManager (untuk menangani file gambar dari disk)
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;

//utility class untuk load dan save image
public class ImageFileManager {
    private static final String IMAGE_FORMAT = "jpg";
    
    //Read file image dari disk dan mereturn sbg image
    //Jika terdapat masalah maka return null
    //(file tidak ditemukan, tidak dapat di decode, atau read error)
    
    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;
        }
    }
    
    //Write file image ke disk. Format jpg
    public static void saveImage(OFImage image, File file) {
        try {
            ImageIO.write(image, IMAGE_FORMAT, file);
        }
        catch(IOException exc) {
            return;
        }
    }
}

4. Class ImageViewer (untuk menggabungkan semua kelas dan eksekusi)
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;

import java.io.File;

public class ImageViewer {
    private JFrame frame;
    private ImagePanel imagePanel;
    private JLabel filenameLabel;
    private JLabel statusLabel;
    private OFImage currentImage;
    
    private static final String VERSION = "Version 1.0";
    private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
    
    public ImageViewer() {
        currentImage = null;
        makeFrame();
    }
    
    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);
    }
    
    private void makeMenuBar(JFrame f) {
        final int SHORTCUT_MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
        
        JMenuBar menubar = new JMenuBar();
        f.setJMenuBar(menubar);
        
        JMenu menu;
        JMenuItem menuitem;
        
        menu = new JMenu("File");
        menubar.add(menu);
        
        menuitem = new JMenuItem("Open");
        menuitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0, SHORTCUT_MASK));
        menuitem.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    openFile();
                }
            }
        );
        
        menu.add(menuitem);
        
        menuitem = new JMenuItem("Close");
        menuitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0, SHORTCUT_MASK));
        menuitem.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    close();
                }
            }
        );
        
        menu.add(menuitem);
        menu.addSeparator();
        
        menuitem = new JMenuItem("Quit");
        menuitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0, SHORTCUT_MASK));
        menuitem.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    quit();
                }
            }
        );
        menu.add(menuitem);
        
        menu = new JMenu ("Filter");
        menubar.add(menu);
        
        menuitem = new JMenuItem("Darker");
        menuitem.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    makeDarker();
                }
            }
        );
        menu.add(menuitem);
        
        menuitem = new JMenuItem("Lighter");
        menuitem.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    makeLighter();
                }
            }
        );
        menu.add(menuitem);
        
        menuitem = new JMenuItem("Threshold");
        menuitem.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    threshold();
                }
            }
        );
        menu.add(menuitem);
        
        menu = new JMenu("Help");
        menubar.add(menu);
        
        menuitem = new JMenuItem("About ImageViewer...");
        menuitem.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    showAbout();
                }
            }
        );
        menu.add(menuitem);
    }
    
    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, "The file was not in a recognized image file format.","Image Load Error",JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        imagePanel.setImage(currentImage);
        showFilename(selectedFile.getPath());
        showStatus("File loaded.");
        frame.pack();
    }
    
    private void close() {
        currentImage = null;
        imagePanel.clearImage();
        showFilename(null);
    }
    
    private void quit() {
        System.exit(0);
    }
    
    private void makeDarker() {
        if(currentImage != null) {
            currentImage.darker();
            frame.repaint();
            showStatus("Image Darkened.");
        }
        else {
            showStatus("No Image Loaded.");
        }
    }
    
    private void makeLighter() {
        if(currentImage != null) {
            currentImage.lighter();
            frame.repaint();
            showStatus("Image Brightened.");
        }
        else {
            showStatus("No Image Loaded.");
        }
    }
    
    private void threshold() {
        if(currentImage != null) {
            currentImage.threshold();
            frame.repaint();
            showStatus("Applied Threshold");
        }
        else {
            showStatus("No Image Loaded.");
        }
    }
    
    private void showAbout() {
        JOptionPane.showMessageDialog(frame, "ImageViewer\n" + VERSION, "About Image Viewer", JOptionPane.INFORMATION_MESSAGE);
    }
    
    private void showFilename(String filename) {
        if(filename == null) {
            filenameLabel.setText("No File loaded.");
        }
        else {
            filenameLabel.setText("File: " + filename);
        }
    }
    
    private void showStatus(String text) {
        statusLabel.setText(text);
    }
    
    public static void main(String[] args) {
        ImageViewer img = new ImageViewer();
    }
}

Comments