Tugas Pertemuan 14 Pemrograman Berbasis Objek (PBO) A 2024

Nama : Amadeo Yesa

NRP : 5025231160

Kelas : A

Tahun : 2024


Pada pertemuan 14, diberikan dua buah studi kasus untuk implementasi GUI.


1. Implementasi frame windows user login dan password


Class Login
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Login {
    public static void main(String[] args) {
        
        JFrame frame = new JFrame("User Login");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 250);
        frame.setLayout(null);
        
        JLabel usernameLabel = new JLabel("Username:");
        usernameLabel.setBounds(50, 50, 80, 25);
        frame.add(usernameLabel);
        
        JLabel passwordLabel = new JLabel("Password:");
        passwordLabel.setBounds(50, 90, 80, 25);
        frame.add(passwordLabel);
        
        JTextField usernameField = new JTextField();
        usernameField.setBounds(150, 50, 165, 25);
        frame.add(usernameField);
        
        JPasswordField passwordField = new JPasswordField();
        passwordField.setBounds(150, 90, 165, 25);
        frame.add(passwordField);
        
        JButton loginButton = new JButton("Login");
        loginButton.setBounds(150, 130, 80, 25);
        frame.add(loginButton);
        
        JLabel resultLabel = new JLabel("");
        resultLabel.setBounds(50, 170, 300, 25);
        frame.add(resultLabel);
        
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                
                if ("admin".equals(username) && "password".equals(password)) {
                    resultLabel.setText("Login successful!");
                    resultLabel.setForeground(Color.GREEN);
                } else {
                    resultLabel.setText("Invalid username or password.");
                    resultLabel.setForeground(Color.RED);
                }
            }
        });
        
        frame.setVisible(true);
    }
}


Hasil




2. Implementasi ImageViewer


Class ImageViewer
import java.awt.*;  
import java.awt.event.*;  
import javax.swing.*;  
import java.io.File;  

public class ImageViewer {  
    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;  

    public ImageViewer() {  
        currentImage = null;  
        initializeFrame();  
    }  

    private void openFile() {  
        int result = fileChooser.showOpenDialog(frame);  

        if (result != JFileChooser.APPROVE_OPTION) {  
            return;  
        }  

        File selectedFile = fileChooser.getSelectedFile();  
        currentImage = ImageFileManager.loadImage(selectedFile);  

        if (currentImage == null) {  
            JOptionPane.showMessageDialog(frame, 
                "The file format is not supported.", 
                "Image Load Error", 
                JOptionPane.ERROR_MESSAGE
            );  
            return;  
        }  

        imagePanel.setImage(currentImage);  
        updateFilename(selectedFile.getPath());  
        updateStatus("File loaded successfully.");  
        frame.pack();  
    }  

    private void closeImage() {  
        currentImage = null;  
        imagePanel.clearImage();  
        updateFilename(null);  
    }  

    private void quitApplication() {  
        System.exit(0);  
    }  

    private void applyDarkerFilter() {  
        if (currentImage != null) {  
            currentImage.darker();  
            frame.repaint();  
            updateStatus("Image darkened.");  
        } else {  
            updateStatus("No image loaded.");  
        }  
    }  

    private void applyLighterFilter() {  
        if (currentImage != null) {  
            currentImage.lighter();  
            frame.repaint();  
            updateStatus("Image lightened.");  
        } else {  
            updateStatus("No image loaded.");  
        }  
    }  

    private void applyThresholdFilter() {  
        if (currentImage != null) {  
            currentImage.threshold();  
            frame.repaint();  
            updateStatus("Image thresholded.");  
        } else {  
            updateStatus("No image loaded.");  
        }  
    }  

    private void showAboutDialog() {  
        JOptionPane.showMessageDialog(frame, 
            "Image Viewer\n" + VERSION, 
            "About Image Viewer", 
            JOptionPane.INFORMATION_MESSAGE
        );  
    }  

    private void updateFilename(String filename) {  
        if (filename == null) {  
            filenameLabel.setText("No file displayed.");  
        } else {  
            filenameLabel.setText("File: " + filename);  
        }  
    }  

    private void updateStatus(String message) {  
        statusLabel.setText(message);  
    }  

    private void initializeFrame() {  
        frame = new JFrame("Image Viewer");  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

        setupMenuBar(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);  

        updateFilename(null);  
        frame.pack();  

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
        frame.setLocation(screenSize.width / 2 - frame.getWidth() / 2, 
                          screenSize.height / 2 - frame.getHeight() / 2);  

        frame.setVisible(true);  
    }  

    private void setupMenuBar(JFrame frame) {  
        final int SHORTCUT_MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx();  

        JMenuBar menuBar = new JMenuBar();  
        frame.setJMenuBar(menuBar);  

        JMenu fileMenu = new JMenu("File");  
        menuBar.add(fileMenu);  

        addMenuItem(fileMenu, "Open", KeyEvent.VK_O, SHORTCUT_MASK, e -> openFile());  
        addMenuItem(fileMenu, "Close", KeyEvent.VK_W, SHORTCUT_MASK, e -> closeImage());  
        addMenuItem(fileMenu, "Quit", KeyEvent.VK_Q, SHORTCUT_MASK, e -> quitApplication());  

        JMenu filterMenu = new JMenu("Filter");  
        menuBar.add(filterMenu);  

        addMenuItem(filterMenu, "Darker", 0, 0, e -> applyDarkerFilter());  
        addMenuItem(filterMenu, "Lighter", 0, 0, e -> applyLighterFilter());  
        addMenuItem(filterMenu, "Threshold", 0, 0, e -> applyThresholdFilter());  

        JMenu helpMenu = new JMenu("Help");  
        menuBar.add(helpMenu);  

        addMenuItem(helpMenu, "About", 0, 0, e -> showAboutDialog());  
    }  

    private void addMenuItem(JMenu menu, String title, int keyEvent, int mask, ActionListener action) {  
        JMenuItem item = new JMenuItem(title);  
        if (keyEvent > 0) {  
            item.setAccelerator(KeyStroke.getKeyStroke(keyEvent, mask));  
        }  
        item.addActionListener(action);  
        menu.add(item);  
    }  

    public static void main(String[] args) {  
        new ImageViewer();  
    }  
}

Class ImagePanel
import java.awt.*;  
import javax.swing.*;  

public class ImagePanel extends JComponent {  
    private OFImage panelImage;  

    public ImagePanel() {  
        panelImage = null;  
    }  

    public void setImage(OFImage image) {  
        if (image != null) {  
            panelImage = image;  
            repaint();  
        }  
    }  

    public void clearImage() {  
        if (panelImage != null) {
            Graphics imageGraphics = panelImage.getGraphics();  
            imageGraphics.setColor(Color.LIGHT_GRAY);  
            imageGraphics.fillRect(0, 0, getWidth(), getHeight());  
            repaint();  
        }
    }  

    @Override
    public Dimension getPreferredSize() {  
        return new Dimension(800, 600);  
    }  

    @Override
    protected void paintComponent(Graphics g) {  
        super.paintComponent(g);  

        if (panelImage != null) {  
            int panelWidth = getWidth();  
            int panelHeight = getHeight();  
            int imageWidth = panelImage.getWidth();  
            int imageHeight = panelImage.getHeight();  

            double imageAspectRatio = (double) imageWidth / imageHeight;  
            double panelAspectRatio = (double) panelWidth / panelHeight;  

            int drawWidth, drawHeight;  
            int xOffset = 0, yOffset = 0;  

            if (panelAspectRatio > imageAspectRatio) {  
                drawHeight = panelHeight;  
                drawWidth = (int) (drawHeight * imageAspectRatio);  
                xOffset = (panelWidth - drawWidth) / 2;
            } else {  
                drawWidth = panelWidth;  
                drawHeight = (int) (drawWidth / imageAspectRatio);  
                yOffset = (panelHeight - drawHeight) / 2;
            }  

            g.drawImage(panelImage, xOffset, yOffset, drawWidth, drawHeight, null);  
        } else {  
            g.setColor(Color.LIGHT_GRAY);  
            g.fillRect(0, 0, getWidth(), getHeight());  
        }  
    }  
}

Class OFImage
import java.awt.*;  
import java.awt.image.*;  

public class OFImage extends BufferedImage {  

    public OFImage(BufferedImage image) {  
        super(image.getColorModel(), image.copyData(null), image.isAlphaPremultiplied(), null);  
    }  

    public OFImage(int width, int height) {  
        super(width, height, TYPE_INT_RGB);  
    }  

    public void setPixel(int x, int y, Color color) {  
        int rgbValue = color.getRGB();  
        setRGB(x, y, rgbValue);  
    }  

    public Color getPixel(int x, int y) {  
        int rgbValue = getRGB(x, y);  
        return new Color(rgbValue);  
    }  

    public void darker() {  
        int width = getWidth();  
        int height = getHeight();  

        for (int y = 0; y < height; y++) {  
            for (int x = 0; x < width; x++) {  
                setPixel(x, y, getPixel(x, y).darker());  
            }  
        }  
    }  

    public void lighter() {  
        int width = getWidth();  
        int height = getHeight();  

        for (int y = 0; y < height; y++) {  
            for (int x = 0; x < width; x++) {  
                setPixel(x, y, getPixel(x, y).brighter());  
            }  
        }  
    }  

    public void threshold() {  
        int width = getWidth();  
        int height = getHeight();  

        for (int y = 0; y < height; y++) {  
            for (int x = 0; x < width; x++) {  
                Color pixelColor = getPixel(x, y);  
                int brightness = pixelColor.getRed() + pixelColor.getGreen() + pixelColor.getBlue();  

                if (brightness <= 85) {  
                    setPixel(x, y, Color.BLACK);  
                } else if (brightness <= 170) {  
                    setPixel(x, y, Color.GRAY);  
                } else {  
                    setPixel(x, y, Color.WHITE);  
                }  
            }  
        }  
    }  
}

Class ImageFileManager
import java.awt.image.BufferedImage;  
import java.io.File;  
import java.io.IOException;  
import javax.imageio.ImageIO;  

public class ImageFileManager {  
    private static final String IMAGE_FORMAT = "jpg";

    public static OFImage loadImage(File imageFile) {  
        try {  
            BufferedImage bufferedImage = ImageIO.read(imageFile);  

            if (bufferedImage == null || bufferedImage.getWidth() <= 0) {  
                return null;  
            }  

            return new OFImage(bufferedImage);  
        } catch (Exception e) {  
            return null;  
        }  
    }  

    public static void saveImage(OFImage image, File file) {  
        try {  
            ImageIO.write(image, IMAGE_FORMAT, file);  
        } catch (IOException e) {  

        }  
    }  
}

Hasil




Source Code:






Comments

Popular posts from this blog

Evaluasi Tengah Semester Pemrograman Berbasis Objek (PBO) A 2024

Tugas Pertemuan 7 Pemrograman Berbasis Objek (PBO) A 2024

Evaluasi Akhir Semester Pemrograman Berbasis Objek (PBO) A 2024