Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.image.*;
- import javax.swing.*;
- import java.io.File;
- /**
- * ImageViewer sebagai main class dalam aplikasi imageviewer.
- * dengan membuat sebuah GUI dan menginisialisai komponen lainnya
- * untuk memulai aplikasi dengan membuat sebuah object pada class ini
- *
- * @author Yusuf Anfasya
- * @version 1
- */
- public class ImageViewer
- {
- // static fields:
- private static final String VERSION = "Versi 1";
- private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
- // fields:
- private JFrame frame;
- private ImagePanel imagePanel;
- private JLabel filenameLabel;
- private JLabel statusLabel;
- private OFImage currentImage;
- /**
- * membuat imageViewer tampil pada layar
- */
- public ImageViewer()
- {
- currentImage = null;
- makeFrame();
- }
- // ---- implementasi fungsi ----
- /**
- * fungsi Open untuk membuka dan memilih gambar baru
- */
- private void openFile()
- {
- int returnVal = fileChooser.showOpenDialog(frame);
- if(returnVal != JFileChooser.APPROVE_OPTION) {
- return; // apabila batal
- }
- File selectedFile = fileChooser.getSelectedFile();
- currentImage = ImageFileManager.loadImage(selectedFile);
- if(currentImage == null) { // image file was not a valid image
- JOptionPane.showMessageDialog(frame,
- "Format File tidak dapat dikenali.",
- "Gambar ERROR!!",
- JOptionPane.ERROR_MESSAGE);
- return;
- }
- imagePanel.setImage(currentImage);
- showFilename(selectedFile.getPath());
- showStatus("File Terbaca.");
- frame.pack();
- }
- /**
- * Fungsi Close untuk menutup gambar yang ada
- */
- private void close()
- {
- currentImage = null;
- imagePanel.clearImage();
- showFilename(null);
- }
- /**
- * Quit function: quit the application.
- */
- private void quit()
- {
- System.exit(0);
- }
- /**
- * fungsi 'Gelapin'untuk membuat gambar tampak lebih gelap
- */
- private void jadiGelap()
- {
- if(currentImage != null) {
- currentImage.gelapin();
- frame.repaint();
- showStatus("Applied: Sudah Gelap");
- }
- else {
- showStatus("Gambar tidak terbaca.");
- }
- }
- /**
- * fungsi 'Terangin 'untuk membuat gambar tampak lebih terang
- */
- private void jadiTerang()
- {
- if(currentImage != null) {
- currentImage.terangin();
- frame.repaint();
- showStatus("Applied: Sudah Terang");
- }
- else {
- showStatus("Gambar tidak terbaca.");
- }
- }
- /**
- * fungsi 'HitamPutih'untuk membuat gambar tampak lebih gelap
- */
- private void HitamPutih()
- {
- if(currentImage != null) {
- currentImage.HitamPutih();
- frame.repaint();
- showStatus("Applied: Pudar");
- }
- else {
- showStatus("Gambar tidak terbaca.");
- }
- }
- /**
- * Show the 'About...' dialog.
- */
- private void showAbout()
- {
- JOptionPane.showMessageDialog(frame,
- "ImageViewer\n" + VERSION,
- "Tentang ImageViewer",
- JOptionPane.INFORMATION_MESSAGE);
- }
- // ---- support methods ----
- /**
- * menampilkan nama file
- * @param apabila tidak ada
- */
- private void showFilename(String filename)
- {
- if(filename == null) {
- filenameLabel.setText("File Tidak ada");
- }
- else {
- filenameLabel.setText("File: " + filename);
- }
- }
- /**
- * menampilkan pesan status di frame status bar nya
- * @param tulisan status message nya
- */
- private void showStatus(String text)
- {
- statusLabel.setText(text);
- }
- // ---- swing stuff to build the frame and all its components ----
- /**
- * membuat swing frame dan kontennya.
- */
- private void makeFrame()
- {
- frame = new JFrame("ImageViewer");
- makeMenuBar(frame);
- Container contentPane = frame.getContentPane();
- // mengatur spacing nya
- 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);
- // mengatur component dan tampilan
- 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 bar frame utama
- * @param menu bar yang harus ditambahkan
- */
- private void makeMenuBar(JFrame frame)
- {
- final int SHORTCUT_MASK =
- Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
- JMenuBar menubar = new JMenuBar();
- frame.setJMenuBar(menubar);
- JMenu menu;
- JMenuItem item;
- // file menu
- menu = new JMenu("File");
- menubar.add(menu);
- item = new JMenuItem("Open");
- item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, SHORTCUT_MASK));
- item.addActionListener(e -> openFile());
- menu.add(item);
- item = new JMenuItem("Close");
- item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, SHORTCUT_MASK));
- item.addActionListener(e -> close());
- menu.add(item);
- menu.addSeparator();
- item = new JMenuItem("Quit");
- item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
- item.addActionListener(e -> quit());
- menu.add(item);
- // create the Filter menu
- menu = new JMenu("Filter");
- menubar.add(menu);
- item = new JMenuItem("Darker");
- item.addActionListener(e -> jadiGelap());
- menu.add(item);
- item = new JMenuItem("Lighter");
- item.addActionListener(e -> jadiTerang());
- menu.add(item);
- item = new JMenuItem("Threshold");
- item.addActionListener(e -> HitamPutih());
- menu.add(item);
- // menu bantuan
- menu = new JMenu("Help");
- menubar.add(menu);
- item = new JMenuItem("About ImageViewer...");
- item.addActionListener(e -> showAbout());
- menu.add(item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement