LOGIN SYSTEM DAN IMAGE VIEWER
LOGIN
/**
* tampilan login sistem.
*
* @author IchsanulAulia
* @version (12142020)
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Login
{
String Username = "Ozoraa";
String Password = "1223";
String msg = " ";
private JTextField txtUsername;
private JPasswordField txtPassword;
public static void main (String args[])
{
Login gui = new Login();
gui.go();
}
public void go(){
JFrame frame = new JFrame("Welcome To The Aplication !!!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel lblUsername = new JLabel("Username:");
JLabel lblPassword = new JLabel("Password:");
txtUsername = new JTextField(20);
txtPassword = new JPasswordField(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);
panel.add(btnLogin);
panel.add(btnCancel);
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(txtUsername.getText())){
if(Password.equals(txtPassword.getText()))
{
msg = "Login Successfull!!";
}
else
{
msg = "Can't Login, Username/Password Incorrect!!";
}
}
else
{
msg = "Can't Login, Username/Password Incorrect!";
}
JOptionPane.showMessageDialog(null,msg);
}
}
public class CancelListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
txtUsername.setText("");
txtPassword.setText("");
txtUsername.requestFocus();
}
}
}
1. UI
2. LOGIN SUCCES
Image Brightened
Image Treshold
1. UI
2. LOGIN SUCCES
Image Viewer
1. class ImgPanel
/**
*
* @author IchsanulAulia
* @version (12142020)
*/
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
public class ImgPanel extends JComponent {
private int width, height;
private JImg panelImg;
public ImgPanel() {
width = 360;
height = 240;
panelImg = null;
}
public void setImage(JImg image) {
if (image!=null) {
width = image.getWidth();
height = image.getHeight();
panelImg = image;
repaint();
}
}
public void clearImage() {
Graphics imageGraphics = panelImg.getGraphics();
imageGraphics.setColor(Color.LIGHT_GRAY);
imageGraphics.fillRect(0, 0, width, height);
repaint();
}
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
public void paintComponent(Graphics g) {
Dimension size = getSize();
g.clearRect(0, 0, size.width, size.height);
if(panelImg != null) {
g.drawImage(panelImg, 0, 0, null);
}
}
}
2. class ImgFM
/**
*
* @author IchsanulAulia
* @version (12142020)
*/
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class ImgFM {
private static final String IMG_FORMAT = "jpg";
public static JImg loadImg(File imgFile) {
try {
BufferedImage img = ImageIO.read(imgFile);
if(img == null || (img.getWidth(null) < 0) ) {
return null;
}
return new JImg(img);
}
catch(IOException exc) {
return null;
}
}
public static void saveImg(JImg img, File file) {
try {
ImageIO.write(img, IMG_FORMAT, file);
}
catch(IOException exc) {
return;
}
}
}
3. class JImg
/**
* mengubah image.
*
* @author IchsanulAulia
* @version (12142020)
*/
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
public class JImg extends BufferedImage {
public JImg(BufferedImage img) {
super(img.getColorModel(), img.copyData(null),
img.isAlphaPremultiplied(), null);
}
public JImg(int width, int height) {
super(width, height, TYPE_INT_RGB);
}
public void setPixel(int x, int y, Color c) {
int pixel = c.getRGB();
setRGB(x,y,pixel);
}
public Color getPixel(int x,int y) {
int pixel = getRGB(x,y);
return new Color(pixel);
}
public void dark() {
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 bright() {
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 monokrom() {
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);
}
}
}
}
}
4. class ImgViewer
/**
*
* @author IchsanuLAulia
* @version (12142020)
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.File;
public class ImgViewer {
private JFrame frame;
private ImgPanel ImgPanel;
private JLabel filenameLabel;
private JLabel statusLabel;
private JImg currentImg;
private static final String VERSION = "Version 1.0";
private static JFileChooser fileChooser =
new JFileChooser(System.getProperty("user.dir"));
public ImgViewer() {
currentImg = null;
makeFrame();
}
private void makeFrame() {
frame = new JFrame("ImageViewer");
makeMenuBar(frame);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout(6,6));
filenameLabel = new JLabel();
contentPane.add(filenameLabel, BorderLayout.NORTH);
ImgPanel = new ImgPanel();
contentPane.add(ImgPanel, 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 makeMenuBar(JFrame f) {
final int SHORTCUT_MASK =
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
JMenuBar menubar = new JMenuBar();
f.setJMenuBar(menubar);
JMenu menu;
JMenuItem menuitem;
menu = new JMenu("File");
menubar.add(menu);
menuitem = new JMenuItem("Open");
menuitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0, SHORTCUT_MASK));
menuitem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
openFile();
}
}
);
menu.add(menuitem);
menuitem = new JMenuItem("Close");
menuitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0, SHORTCUT_MASK));
menuitem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
close();
}
}
);
menu.add(menuitem);
menu.addSeparator();
menuitem = new JMenuItem("Quit");
menuitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0, SHORTCUT_MASK));
menuitem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
quit();
}
}
);
menu.add(menuitem);
menu = new JMenu ("Filter");
menubar.add(menu);
menuitem = new JMenuItem("Darker");
menuitem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
makeDarker();
}
}
);
menu.add(menuitem);
menuitem = new JMenuItem("Lighter");
menuitem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
makeBrighter();
}
}
);
menu.add(menuitem);
menuitem = new JMenuItem("Threshold");
menuitem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
threshold();
}
}
);
menu.add(menuitem);
menu = new JMenu("Help");
menubar.add(menu);
menuitem = new JMenuItem("About ImageViewer...");
menuitem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
showAbout();
}
}
);
menu.add(menuitem);
}
private void openFile() {
int returnVal = fileChooser.showOpenDialog(frame);
if (returnVal != JFileChooser.APPROVE_OPTION) {
return;
}
File selectedFile = fileChooser.getSelectedFile();
currentImg = ImgFM.loadImg(selectedFile);
if(currentImg == null) {
JOptionPane.showMessageDialog(frame, "The file was not in a recognized image file format.","Image Load Error",JOptionPane.ERROR_MESSAGE);
return;
}
ImgPanel.setImage(currentImg);
showFilename(selectedFile.getPath());
showStatus("File loaded.");
frame.pack();
}
private void close() {
currentImg = null;
ImgPanel.clearImage();
showFilename(null);
}
private void quit() {
System.exit(0);
}
private void makeDarker() {
if(currentImg != null) {
currentImg.dark();
frame.repaint();
showStatus("Image Darkened.");
}
else {
showStatus("No Image Loaded.");
}
}
private void makeBrighter() {
if(currentImg != null) {
currentImg.bright();
frame.repaint();
showStatus("Image Brightened.");
}
else {
showStatus("No Image Loaded.");
}
}
private void threshold() {
if(currentImg != null) {
currentImg.monokrom();
frame.repaint();
showStatus("Applied Threshold");
}
else {
showStatus("No Image Loaded.");
}
}
private void showAbout() {
JOptionPane.showMessageDialog(frame, "ImageViewer\n" + VERSION, "About Image Viewer", JOptionPane.INFORMATION_MESSAGE);
}
private void showFilename(String filename) {
if(filename == null) {
filenameLabel.setText("No File loaded.");
}
else {
filenameLabel.setText("File: " + filename);
}
}
private void showStatus(String text) {
statusLabel.setText(text);
}
public static void main(String[] args) {
ImgViewer img = new ImgViewer();
}
}
Image Treshold
Komentar
Posting Komentar