Nama : Alif Nurrohman
NRP : 5025231057
Link Code : https://github.com/alifnurrohmans/tugas14pbo
Explanation
1). ImageViewer.java
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;
setupFrame();
}
public void actionPerformed(ActionEvent event) {
System.out.println("Menu item" + event.getActionCommand());
}
private void openFile() {
int res = fileChooser.showOpenDialog(frame);
if (res != JFileChooser.APPROVE_OPTION) {
return;
}
File selectedFile = fileChooser.getSelectedFile();
currentImage = ImageFileManager.loadImage(selectedFile);
if (currentImage == null) {
JOptionPane.showMessageDialog(frame, "The file was not in a recognisable image file format.", "Image Load Error",
JOptionPane.ERROR_MESSAGE);
return;
}
imagePanel.setImage(currentImage);
showFilename(selectedFile.getPath());
showStatus("File loaded successfully.");
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 lightened.");
} else {
showStatus("No image loaded.");
}
}
private void threshold() {
if (currentImage != null) {
currentImage.threshold();
frame.repaint();
showStatus("Image thresholded.");
} else {
showStatus("No image loaded.");
}
}
private void showAbout() {
JOptionPane.showMessageDialog(frame, "Image Viewer\n" + VERSION, "About ImageViewer",
JOptionPane.INFORMATION_MESSAGE);
}
private void showFilename(String filename) {
if (filename == null) {
filenameLabel.setText("No file displayed.");
} else {
filenameLabel.setText("File: " + filename);
}
}
private void showStatus(String messsage) {
statusLabel.setText(messsage);
}
private void setupFrame() {
frame = new JFrame("ImageViewer");
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);
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 setupMenuBar(JFrame frame) {
final int SHORTCUT_MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx();
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu menu;
JMenuItem item;
menu = new JMenu("File");
menuBar.add(menu);
item = new JMenuItem("Open");
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, SHORTCUT_MASK));
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openFile();
}
});
menu.add(item);
item = new JMenuItem("Close");
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, SHORTCUT_MASK));
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
close();
}
});
menu.add(item);
item = new JMenuItem("Quit");
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("Darker");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
makeDarker();
}
});
menu.add(item);
item = new JMenuItem("Lighter");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
makeLighter();
}
});
menu.add(item);
item = new JMenuItem("Threshold");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
threshold();
}
});
menu.add(item);
menu = new JMenu("Help");
menuBar.add(menu);
item = new JMenuItem("About");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showAbout();
}
});
menu.add(item);
}
public static void main(String[] args) {
new ImageViewer();
}
}
Kelas ini adalah antarmuka utama aplikasi. Fungsinya mencakup :
Menyediakan GUI untuk membuka, menampilkan, memodifikasi, dan menyimpan gambar.
Mengintegrasikan menu untuk fungsi File (open, close, quit) dan Filter (darker, lighter, threshold).
Berinteraksi dengan kelas lain untuk memuat gambar ke GUI dan menerapkan filter.
2). ImagePanel.java
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() {
Graphics imageGraphics = panelImage.getGraphics();
imageGraphics.setColor(Color.LIGHT_GRAY);
imageGraphics.fillRect(0, 0, getWidth(), getHeight());
repaint();
}
public Dimension getPreferredSize() {
return new Dimension(800, 600);
}
public 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());
}
}
}
Kelas ini bertanggung jawab untuk menampilkan gambar pada GUI :
Digunakan oleh ImageViewer sebagai komponen GUI yang menampilkan gambar.
Mendukung metode untuk mengganti gambar (melalui setImage) atau membersihkannya (melalui clearImage).
Menggunakan metode Swing untuk mengatur ukuran dan memperbarui tampilan panel
4). ImageFileManager.java
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 image = ImageIO.read(imageFile);
if (image == null || image.getWidth(null) < 0) {
return null;
}
return new OFImage(image);
} catch (Exception e) {
return null;
}
}
public static void saveImage(OFImage image, File file) {
try {
ImageIO.write(image, IMAGE_FORMAT, file);
} catch (IOException exception) {
return;
}
}
}
Kelas ImageFileManager
Kelas ini adalah manajer file gambar :
Membaca (memuat) gambar dari file dengan memanfaatkan ImageIO.
Menyimpan gambar yang dimodifikasi kembali ke file.
Menyediakan format standar gambar (dalam kode ini, jpg) untuk penyimpanan.
5). OFImage.java
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 col) {
int pixel = col.getRGB();
setRGB(x, y, pixel);
}
public Color getPixel(int x, int y) {
int pixel = getRGB(x, y);
return new Color(pixel);
}
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());
}
}
}
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());
}
}
}
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);
}
}
}
}
}
Kelas OFImage
Kelas ini adalah kelas gambar yang diperluas, diturunkan dari BufferedImage:
Memberikan cara mudah untuk mengakses dan memodifikasi piksel gambar secara langsung.
Menambahkan fitur manipulasi warna seperti darker (menggelapkan), lighter (mencerahkan), dan threshold (mengubah gambar ke skala hitam-putih berbasis intensitas).
Digunakan oleh ImageViewer untuk memanipulasi gambar yang ditampilkan.
Diagram Class
Implementasi
1). Login Panel
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Arrays;
public class Login {
String username = "alif";
char[] password = "alif".toCharArray();
String message = " ";
JTextField inputUsername;
JPasswordField inputPassword;
public void setupPanel() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel labelUsername = new JLabel("Username: ");
JLabel labelPassword = new JLabel("Password:");
inputUsername = new JTextField(20);
inputPassword = new JPasswordField(20);
JButton loginButton = new JButton("Login");
loginButton.addActionListener(new LoginListener());
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new CancelListener());
panel.add(labelUsername);
panel.add(inputUsername);
panel.add(labelPassword);
panel.add(inputPassword);
panel.add(loginButton);
panel.add(cancelButton);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.setSize(300, 300);
frame.setVisible(true);
}
public class LoginListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (username.equals(inputUsername.getText()) && Arrays.equals(password, inputPassword.getPassword())) {
message = "Login successful!";
} else {
message = "Login failed!";
}
JOptionPane.showMessageDialog(null, message);
}
}
public class CancelListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
inputUsername.setText("");
inputPassword.setText("");
inputUsername.requestFocus();
}
}
public static void main(String[] args) {
Login login = new Login();
login.setupPanel();
}
}
Hubungan Antar Kelas :
ImageViewer adalah kelas inti yang memanfaatkan ImagePanel untuk menampilkan gambar, ImageFileManager untuk mengelola file, dan OFImage untuk manipulasi gambar.
ImagePanel bekerja sebagai antarmuka visual yang menampilkan gambar dari OFImage.
ImageFileManager bertanggung jawab atas integrasi antara aplikasi dan sistem file, sementara OFImage menyediakan mekanisme manipulasi gambar
Login Panel
Program di atas adalah aplikasi login sederhana berbasis Java Swing dengan fitur berikut:
Input Login :
Username : Field untuk memasukkan username.
Password : Menggunakan JPasswordField agar lebih aman.
Validasi :
Kredensial diverifikasi dengan nilai hardcoded:
Username : alif
Password : alif
Menampilkan pesan "Login Granted!" jika valid, dan "Login Denied" jika tidak.
Tombol :
Login : Memeriksa kredensial dan menampilkan pesan hasil validasi.
Cancel : Menghapus input username dan password, lalu mengembalikan fokus ke kolom username.
Antarmuka :
Menggunakan GridLayout untuk tata letak sederhana.
Panel menampilkan input, label, dan tombol secara terstruktur.
Implementasi
Apabila username & login tidak sesuai maka terdapat pesan
Komentar
Posting Komentar