Tugas 6 (Implemen GUI)
1. Image Viewer
Kelas Image Viewer
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.io.File;
public class ImageViewer
{
// instance variables - replace the example below with your own
private static final String VERSION = "Version 1.0";
private static JFileChooser fileChooser = new JFileChooser (System.getPropert("user.dir"));
private JFrame frame;
private ImagePanel imagePanel;
private JLabel filenameLabel;
private JLabel statusLabel;
private OFImage currentImage;
public ImageViewer()
{
currentImage = null;
makeFrame();
}
private void openFile()
{
int returnVal= fileChooser.showOpenDialog(frame);
if(returnVal != JFileChooser.getSelectedFile())
{
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("Applied: darker");
}
else
{
showStatus("No image loaded.");
}
}
private void makeLighter()
{
if(currentImage != null)
{
currentImage.lighter();
frame.repaint();
showStatus("Applied: lighter");
}
else
{
showStatus("No image loaded.");
}
}
private void threshold()
{
if(currentImage != null)
{
currentImagethreshold();
frame.repaint();
showStatus("Applied: threshold");
}
else
{
showStatus("No image loaded.");
}
}
private void showAbout()
{
JOptionPane.showMessageDialog(frame, "ImageViewer\n" + VERSION, "About ImageViewer", JOptionPane.INFORMATION_MESSAGE);
}
private void showfilename(String filename)
{
if(filename == null)
{
filename.Label.setText("No File Displayed.");
}
else
{
filenameLabel.setText("File: " + filename);
}
}
private void showStatus(String text)
{
statusLabel.setText(text);
}
private void makeFrame()
{
frame = new JFrame("ImageViewer");
makeMenuBar(frame);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout(6, 6));
contentPane.add(filenameLabel, BorderLayout.NORTH);
imagePanel = new ImagePanel();
contentPane.add(statusLabel, BorderLayout.CENTER);
statusLabel = new JLabel(VERSION);
contentPane.add(imagePanel, 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 frame)
{
final int SHORTCUT_MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMaks();
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.getStroke(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);
menu.addSeparator();
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 JMenuUtem("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 ImageViewer...");
item.addActionListener(new ActionListener() | public void actionPerformed(ActionEvent e) | showAbout(); | |);
menu.add(item);
}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.io.File;
public class ImageViewer
{
// instance variables - replace the example below with your own
private static final String VERSION = "Version 1.0";
private static JFileChooser fileChooser = new JFileChooser (System.getPropert("user.dir"));
private JFrame frame;
private ImagePanel imagePanel;
private JLabel filenameLabel;
private JLabel statusLabel;
private OFImage currentImage;
public ImageViewer()
{
currentImage = null;
makeFrame();
}
private void openFile()
{
int returnVal= fileChooser.showOpenDialog(frame);
if(returnVal != JFileChooser.getSelectedFile())
{
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("Applied: darker");
}
else
{
showStatus("No image loaded.");
}
}
private void makeLighter()
{
if(currentImage != null)
{
currentImage.lighter();
frame.repaint();
showStatus("Applied: lighter");
}
else
{
showStatus("No image loaded.");
}
}
private void threshold()
{
if(currentImage != null)
{
currentImagethreshold();
frame.repaint();
showStatus("Applied: threshold");
}
else
{
showStatus("No image loaded.");
}
}
private void showAbout()
{
JOptionPane.showMessageDialog(frame, "ImageViewer\n" + VERSION, "About ImageViewer", JOptionPane.INFORMATION_MESSAGE);
}
private void showfilename(String filename)
{
if(filename == null)
{
filename.Label.setText("No File Displayed.");
}
else
{
filenameLabel.setText("File: " + filename);
}
}
private void showStatus(String text)
{
statusLabel.setText(text);
}
private void makeFrame()
{
frame = new JFrame("ImageViewer");
makeMenuBar(frame);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout(6, 6));
contentPane.add(filenameLabel, BorderLayout.NORTH);
imagePanel = new ImagePanel();
contentPane.add(statusLabel, BorderLayout.CENTER);
statusLabel = new JLabel(VERSION);
contentPane.add(imagePanel, 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 frame)
{
final int SHORTCUT_MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMaks();
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.getStroke(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);
menu.addSeparator();
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 JMenuUtem("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 ImageViewer...");
item.addActionListener(new ActionListener() | public void actionPerformed(ActionEvent e) | showAbout(); | |);
menu.add(item);
}
}
Kelas Image Panel
Kelas Image File Manager
Kelas OF Image
2. Implementasi Login
Kelas Login
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class login
{
String Username = "james 3302";
String Password = "pass";
String msg = " ";
public static void main(String[] args)
{
login gui = new login();
gui.go();
}
public void go ()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel lblUsername = new JLabel("Username:");
JLabel lblPassword = new JLabel("Password:");
JTextField txtUsername = new JTextField(20);
JTextField txtPassword = new JTextField(20);
JButton btnLogin = new JButton ("Login");
btnLogin.addActionListener (new LoginListener());
JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new CancelListener());
panel.add(lblUsername);
panel.add(txtUsername);
panel.add(lblPassword);
panel.add(txtPassword);
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setSize(300,300);
frame.setVisible(true);
}
}
Kelas Login Listener
public class LoginListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(Username.equals(txtUsername.getText()))
{
if(Password.equals(txtPassword.getText()))
{
msg = "Login Granted!";
}
else
{
msg = "Login Denied";
}
JOptionPane.showMessageDialog(null,msg);
}
}
}
Kelas Cancel Listener
public class CancelListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
txtUsername.setText = " ";
txtPassword.setText = "";
txtUsername.requestFocus();
}
}
2. Implementasi Login
Kelas Login
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class login
{
String Username = "james 3302";
String Password = "pass";
String msg = " ";
public static void main(String[] args)
{
login gui = new login();
gui.go();
}
public void go ()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel lblUsername = new JLabel("Username:");
JLabel lblPassword = new JLabel("Password:");
JTextField txtUsername = new JTextField(20);
JTextField txtPassword = new JTextField(20);
JButton btnLogin = new JButton ("Login");
btnLogin.addActionListener (new LoginListener());
JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new CancelListener());
panel.add(lblUsername);
panel.add(txtUsername);
panel.add(lblPassword);
panel.add(txtPassword);
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setSize(300,300);
frame.setVisible(true);
}
}
Kelas Login Listener
public class LoginListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(Username.equals(txtUsername.getText()))
{
if(Password.equals(txtPassword.getText()))
{
msg = "Login Granted!";
}
else
{
msg = "Login Denied";
}
JOptionPane.showMessageDialog(null,msg);
}
}
}
Kelas Cancel Listener
public class CancelListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
txtUsername.setText = " ";
txtPassword.setText = "";
txtUsername.requestFocus();
}
}
Komentar
Posting Komentar