Tugas PBO 14 GUI & Panel Login
Nama : Afel Allaric Absor
NRP : 5025231140
Kelas : PBO (A)
Tugas 14 GUI Image Viewer & Panel Login
1). Image Viewer
- Isi file ImageViewer.java :
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
/**
* ImageViewer is the main class of the image viewer application. It builds and
* displays the application GUI and initializes all other components.
*
* To start the application, create an object of this class.
*
* @author Michael Kolling and David J Barnes
* @version 1.0
*/
public class ImageViewer {
// Static fields
private static final String VERSION = "Version 1.0";
private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
// Instance fields
private JFrame frame;
private ImagePanel imagePanel;
private JLabel filenameLabel;
private JLabel statusLabel;
private OFImage currentImage;
/**
* Main constructor. Create an ImageViewer and show it on screen.
*/
public ImageViewer() {
makeFrame();
}
/**
* Open a file dialog to load an image file.
*/
private void openFile() {
int returnVal = fileChooser.showOpenDialog(frame);
if (returnVal != JFileChooser.APPROVE_OPTION) {
return; // cancelled
}
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();
}
/**
* Close the currently loaded image.
*/
private void close() {
currentImage = null;
imagePanel.clearImage();
showFilename(null);
showStatus("Image closed.");
}
/**
* Quit the application.
*/
private void quit() {
System.exit(0);
}
/**
* Apply a "darker" filter to the image.
*/
private void makeDarker() {
if (currentImage != null) {
currentImage.darker();
frame.repaint();
showStatus("Applied: darker");
} else {
showStatus("No image loaded.");
}
}
/**
* Apply a "lighter" filter to the image.
*/
private void makeLighter() {
if (currentImage != null) {
currentImage.lighter();
frame.repaint();
showStatus("Applied: lighter");
} else {
showStatus("No image loaded.");
}
}
/**
* Apply a "threshold" filter to the image.
*/
private void threshold() {
if (currentImage != null) {
currentImage.threshold();
frame.repaint();
showStatus("Applied: threshold");
} else {
showStatus("No image loaded.");
}
}
/**
* Show an "About" dialog.
*/
private void showAbout() {
JOptionPane.showMessageDialog(frame,
"ImageViewer\n" + VERSION,
"About ImageViewer",
JOptionPane.INFORMATION_MESSAGE);
}
// ---- Support Methods ----
/**
* Display a file name on the filename label.
*
* @param filename The file name to be displayed.
*/
private void showFilename(String filename) {
if (filename == null) {
filenameLabel.setText("No file displayed.");
} else {
filenameLabel.setText("File: " + filename);
}
}
/**
* Display a status message in the status bar.
*
* @param text The status message to be displayed.
*/
private void showStatus(String text) {
statusLabel.setText(text);
}
// ---- GUI Construction ----
/**
* Create the Swing frame and its content.
*/
private void makeFrame() {
frame = new JFrame("ImageViewer");
makeMenuBar(frame);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout(6, 6));
filenameLabel = new JLabel("No file displayed.");
contentPane.add(filenameLabel, BorderLayout.NORTH);
imagePanel = new ImagePanel();
contentPane.add(imagePanel, BorderLayout.CENTER);
statusLabel = new JLabel(VERSION);
contentPane.add(statusLabel, BorderLayout.SOUTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width / 2 - frame.getWidth() / 2, d.height / 2 - frame.getHeight() / 2);
frame.setVisible(true);
}
/**
* Create the main frame's menu bar.
*
* @param frame The frame that the menu bar should be added to.
*/
private void makeMenuBar(JFrame frame) {
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, ActionEvent.CTRL_MASK));
item.addActionListener(e -> openFile());
menu.add(item);
item = new JMenuItem("Close");
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));
item.addActionListener(e -> close());
menu.add(item);
menu.addSeparator();
item = new JMenuItem("Quit");
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
item.addActionListener(e -> quit());
menu.add(item);
// Filter menu
menu = new JMenu("Filter");
menubar.add(menu);
item = new JMenuItem("Darker");
item.addActionListener(e -> makeDarker());
menu.add(item);
item = new JMenuItem("Lighter");
item.addActionListener(e -> makeLighter());
menu.add(item);
item = new JMenuItem("Threshold");
item.addActionListener(e -> threshold());
menu.add(item);
// Help menu
menu = new JMenu("Help");
menubar.add(menu);
item = new JMenuItem("About ImageViewer...");
item.addActionListener(e -> showAbout());
menu.add(item);
}
public static void main(String[] args) {
new ImageViewer();
}
}
- Isi file ImagePanel.java :
import javax.swing.*;
import java.awt.*;
public class ImagePanel extends JPanel {
private OFImage image;
public void setImage(OFImage image) {
this.image = image;
repaint();
}
public void clearImage() {
this.image = null;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
}
}
}
- Isi file ImageFileManager.java :
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
/**
* ImageFileManager is a utility class with static methods to load and save images.
*
* The files on disk can be in JPG or PNG image format. For files written
* by this class, the format is determined by the constant IMAGE_FORMAT.
*
* @author Michael Kolling and David J Barnes
* @version 2.0
*/
public class ImageFileManager {
// A constant for the image format that this writer uses for writing.
// Available formats are "jpg" and "png".
private static final String IMAGE_FORMAT = "jpg";
/**
* Read an image file from disk and return it as an OFImage object.
* This method can read JPG and PNG file formats.
* In case of any problem (e.g., the file does not exist, is in an undecodable format,
* or any other read error), this method returns null.
*
* @param imageFile The image file to be loaded.
* @return The OFImage object or null if it could not be read.
*/
public static OFImage loadImage(File imageFile) {
try {
BufferedImage image = ImageIO.read(imageFile);
if (image == null) {
// Could not load the image - probably an invalid file format
return null;
}
return new OFImage(image);
} catch (IOException exc) {
// Return null if an error occurs while reading the image
return null;
}
}
/**
* Write an OFImage object to disk. The file format is determined by IMAGE_FORMAT.
* In case of any problem, the method silently returns.
*
* @param image The OFImage object to be saved.
* @param file The file to save to.
*/
public static void saveImage(OFImage image, File file) {
try {
ImageIO.write(image, IMAGE_FORMAT, file);
} catch (IOException exc) {
// Silently handle any errors during image saving
}
}
}
- Isi File OFImage.java :
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
/**
* OFImage is a class that defines an image in OF (Objects First) format.
* It extends BufferedImage to add additional functionality for manipulating images.
*
* @author Michael Kolling and David J. Barnes
* @version 1.1
*/
public class OFImage extends BufferedImage {
/**
* Create an OFImage copied from a BufferedImage.
*
* @param image The image to copy.
*/
public OFImage(BufferedImage image) {
super(image.getColorModel(), image.copyData(null),
image.isAlphaPremultiplied(), null);
}
/**
* Create an OFImage with specified size and unspecified content.
*
* @param width The width of the image.
* @param height The height of the image.
*/
public OFImage(int width, int height) {
super(width, height, TYPE_INT_RGB);
}
/**
* Set a given pixel of this image to a specified color.
* The color is represented as an (r,g,b) value.
*
* @param x The x position of the pixel.
* @param y The y position of the pixel.
* @param col The color of the pixel.
*/
public void setPixel(int x, int y, Color col) {
int pixel = col.getRGB();
setRGB(x, y, pixel);
}
/**
* Get the color value at a specified pixel position.
*
* @param x The x position of the pixel.
* @param y The y position of the pixel.
* @return The color of the pixel at the given position.
*/
public Color getPixel(int x, int y) {
int pixel = getRGB(x, y);
return new Color(pixel);
}
/**
* Make this image a bit darker.
*/
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());
}
}
}
/**
* Make this image a bit lighter.
*/
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());
}
}
}
/**
* Perform a three-level threshold operation.
* Repaint the image with only three color values: white, gray, and black.
*/
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()) / 3;
if (brightness <= 85) {
setPixel(x, y, Color.BLACK);
} else if (brightness <= 170) {
setPixel(x, y, Color.GRAY);
} else {
setPixel(x, y, Color.WHITE);
}
}
}
}
}
- Hasil Kode saat dijalankan :
2). Panel Login
- Isi file LoginPanel.java :
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginPanel {
public static void main(String[] args) {
JFrame frame = new JFrame("Login Panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 200);
frame.setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
frame.add(panel);
JLabel usernameLabel = new JLabel("Username:");
JLabel passwordLabel = new JLabel("Password:");
JTextField usernameField = new JTextField(15);
JPasswordField passwordField = new JPasswordField(15);
JButton loginButton = new JButton("Login");
JLabel messageLabel = new JLabel("");
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(usernameLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
panel.add(usernameField, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
panel.add(passwordLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
panel.add(passwordField, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
panel.add(loginButton, gbc);
gbc.gridx = 1;
gbc.gridy = 3;
panel.add(messageLabel, gbc);
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (username.equals("admin") && password.equals("1234")) {
messageLabel.setText("Login berhasil! Selamat datang, " + username);
messageLabel.setForeground(Color.GREEN);
} else {
messageLabel.setText("Username atau password salah!");
messageLabel.setForeground(Color.RED);
}
}
});
frame.setVisible(true);
}
}
- Hasil dari kode saat dijalankan :
- Link repository github berisi kode : https://github.com/afelallaric/Tugas-PBO-14-GUI.git



Komentar
Posting Komentar