JImage.java


Untuk program ini Anda buat dulu folder misalkan JImage dan di dalam folder JImage buat lagi folder image untuk menampung file gambar.

// Java Image Viewer
// Oleh Iman Wibowo
// Copyright (C) 2009
// Sebenarnya seh ini rencana untuk Judul TA saya, tapi tidak apalah untuk memperbaiki nilai mata kuliah PBO, aplikasi ini saya keluarkan dulu sebelum waktunya. hehehehehehe
// Oya pak,untuk kesempurnaan aplikasi ini, saya harus mengirimkan folder yang berisi gambar2 yang diperlukan dalam aplikasi ini. nanti saya serahkan jika saya datang ke SMKN 3. okeh? :)

public class JImage extends javax.swing.JFrame{

private java.util.prefs.Preferences root, node;

private java.awt.Toolkit kit;
private java.awt.Dimension dim;

private int x,y,w,h;

private JPanel panel_image;

public JImage(){

kit = java.awt.Toolkit.getDefaultToolkit();
dim = kit.getScreenSize();

setTitle(“Sistem Aplikasi Pencari Data Gambar Berbasis Java”);

try{
java.awt.Image icon = javax.imageio.ImageIO.read(
new java.io.File(“image\\Preview.png”) );
setIconImage(icon);
}catch(java.io.IOException e){
}

root = java.util.prefs.Preferences.userRoot();
node = root.node(“Jav-Ass/JImage”);

w = node.getInt(“w”,800);
h = node.getInt(“h”,600);
x = node.getInt(“x”,(int)(dim.getWidth()-w)/2 );
y = node.getInt(“y”,(int)(dim.getHeight()-h)/2 );
setBounds(x,y,w,h);

setDefaultCloseOperation(javax.swing.JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter(){
public void windowClosing(java.awt.event.WindowEvent event){
exit();
}
} );

panel_image = new JPanel(this);
add(panel_image, java.awt.BorderLayout.CENTER);

}

public void exit(){
savePreference();
int i = Alert.Ask(this,”Apakah Anda yakin akan mengakhiri aplikasi?”,
new javax.swing.ImageIcon(“image\\question.png”) );
if(i == 0){
System.exit(0);
}
}

public void savePreference(){
node.putInt(“w”,getWidth());
node.putInt(“h”,getHeight());
node.putInt(“x”,getX());
node.putInt(“y”,getY());
}

public static void main (String[] args) {
JImage frame = new JImage();
frame.setVisible(true);
}

}

class JPanel extends javax.swing.JPanel{

private javax.swing.JMenuBar menubar;
private javax.swing.JMenuItem item_file, item_edit, item_about;

private javax.swing.JPanel toolbar, panel_edit, panel_file, panel_about;
private javax.swing.JButton button_open, button_exit,
button_enlarge, button_reduce, button_about, button_actual;
private java.awt.CardLayout layout_toolbar;

private javax.swing.JLabel image;
private javax.swing.JScrollPane pane_image;

private javax.swing.JFileChooser file_choose;
private FileFilter filter;
private FileView view;
private Preview view_image;

private java.awt.Image original_image;
private int size_image;

private JImage parent;
private JDialog dialog_about;
private javax.swing.JLabel status;

public JPanel(JImage frame){

parent = frame;

setLayout(new java.awt.BorderLayout() );

image = new javax.swing.JLabel();
image.setHorizontalAlignment(javax.swing.JLabel.CENTER);
image.setVerticalAlignment(javax.swing.JLabel.CENTER);
pane_image = new javax.swing.JScrollPane(image);
javax.swing.JScrollBar barv = new javax.swing.JScrollBar();
barv.setUnitIncrement(100);
pane_image.setVerticalScrollBar(barv);
pane_image.setHorizontalScrollBarPolicy(
javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
pane_image.setVerticalScrollBarPolicy(
javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(pane_image, java.awt.BorderLayout.CENTER);

item_file = new javax.swing.JMenuItem(“Data”);
item_file.addActionListener(new aksi_menu() );
item_edit = new javax.swing.JMenuItem(“Lihat”);
item_edit.addActionListener(new aksi_menu() );
item_about = new javax.swing.JMenuItem(“Tentang”);
item_about.addActionListener(new aksi_menu() );
menubar = new javax.swing.JMenuBar();
menubar.add(item_file);
menubar.add(item_edit);
menubar.add(item_about);
menubar.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT) );
parent.setJMenuBar(menubar);

panel_file = new javax.swing.JPanel(
new java.awt.FlowLayout(java.awt.FlowLayout.LEFT) );
button_exit = new javax.swing.JButton();
addButtonToolbar(
button_exit,panel_file,”image\\Shutdown.png”,”exit”,”Keluar Aplikasi”);
button_open = new javax.swing.JButton();
addButtonToolbar(
button_open,panel_file,”image\\Folder Blue Pics 1.png”,”open”,”Buka Gambar”);
panel_edit = new javax.swing.JPanel(
new java.awt.FlowLayout(java.awt.FlowLayout.LEFT) );
button_reduce = new javax.swing.JButton();
addButtonToolbar(
button_reduce,panel_edit,”image\\Back – Previous.png”,”reduce”,”Perkecil Gambar”);
button_actual = new javax.swing.JButton();
addButtonToolbar(
button_actual,panel_edit,”image\\Refresh.png”,”actual”,”Kembalikan ke ukuran asal”);
button_enlarge = new javax.swing.JButton();
addButtonToolbar(
button_enlarge,panel_edit,”image\\Forward – Next.png”,”enlarge”,”Perbesar Gambar”);
panel_about = new javax.swing.JPanel(
new java.awt.FlowLayout(java.awt.FlowLayout.LEFT) );
button_about = new javax.swing.JButton();
addButtonToolbar(
button_about,panel_about,”image\\Home.png”,”about”,”Tentang Aplikasi”);
layout_toolbar = new java.awt.CardLayout();
toolbar = new javax.swing.JPanel();
toolbar.setLayout(layout_toolbar);
toolbar.add(“file”,panel_file);
toolbar.add(“view”,panel_edit);
toolbar.add(“about”,panel_about);
add(toolbar, java.awt.BorderLayout.NORTH);

filter = new FileFilter();
view = new FileView(filter);
file_choose = new javax.swing.JFileChooser(“.”);
file_choose.setFileFilter(filter);
file_choose.setFileView(view);
file_choose.setAcceptAllFileFilterUsed(false);
view_image = new Preview(file_choose);
file_choose.setAccessory(view_image);
file_choose.addPropertyChangeListener(new aksi_selected_file() );

dialog_about = new JDialog(parent);

status = new javax.swing.JLabel(“Inilah daerah kolom status”, javax.swing.JLabel.LEFT);
add(status, java.awt.BorderLayout.SOUTH);

}

public void addButtonToolbar(javax.swing.JButton tombol,
javax.swing.JPanel panel_toolbar,
String icon_path,
String perintah,
String status_label){
tombol.setIcon(new javax.swing.ImageIcon(icon_path) );
tombol.setActionCommand(perintah);
tombol.addActionListener(new aksi_button() );
tombol.addMouseListener(new aksi_mouse_status(status_label) );
tombol.setToolTipText(status_label);
panel_toolbar.add(tombol);
}

public void loadImage(String file){
size_image = 100;
try{
original_image = javax.imageio.ImageIO.read(new java.io.File(file) );
javax.swing.Icon icon = new javax.swing.ImageIcon(
original_image.getScaledInstance(
original_image.getWidth(null),
original_image.getHeight(null),
java.awt.Image.SCALE_DEFAULT));
image.setIcon(icon);
}catch(java.io.IOException e){
Alert.Message(parent,e.getMessage(),new javax.swing.ImageIcon(
“image\\error.png”) );
}
}

public void about(){
System.out.println(“ABOUT”);
dialog_about.setVisible(true);
}

public void exit(){
System.out.println(“EXIT”);
parent.exit();
}

public void open(){
System.out.println(“OPEN”);
int valid = file_choose.showOpenDialog(parent);
if(valid == javax.swing.JFileChooser.APPROVE_OPTION){
if(filter.accept(file_choose.getSelectedFile())){
String path = file_choose.getSelectedFile().getPath();
loadImage(path);
}else{
Alert.Message(parent,
“Unsuported Format”,
new javax.swing.ImageIcon(“image\\error.png”) );
}
}else{
return ;
}
}

public void enlarge(){
System.out.println(“REDUCE”);
if(original_image == null){
Alert.Message(parent,”Anda harus membuka data gambar terlebih dahulu”,
new javax.swing.ImageIcon(“image\\warning.png”) );
return ;
}
size_image += 10;
if(size_image > 1000){
Alert.Message(parent,
“Anda hanya dapat memperbesar sampai 100%”,
new javax.swing.ImageIcon(“image\\smile.png”) );
size_image = 1000;
}else{
javax.swing.Icon icon = new javax.swing.ImageIcon(
original_image.getScaledInstance(
(int)((double)original_image.getWidth(null)*((double)size_image/100.0)),
(int)((double)original_image.getHeight(null)*((double)size_image/100.0)),
java.awt.Image.SCALE_DEFAULT));
image.setIcon(icon);
}

}

public void reduce(){
System.out.println(“REDUCE”);
if(original_image == null){
Alert.Message(parent,”Anda harus membuka data gambar terlebih dahulu”,
new javax.swing.ImageIcon(“image\\warning.png”) );
return ;
}
size_image -= 10;
if(size_image < 10){
Alert.Message(parent,
“Anda hanya dapat memperkecil sampai 10%”,
new javax.swing.ImageIcon(“image\\smile.png”) );
size_image = 10;
}else{
javax.swing.Icon icon = new javax.swing.ImageIcon(
original_image.getScaledInstance(
(int)((double)original_image.getWidth(null)*((double)size_image/100.0)),
(int)((double)original_image.getHeight(null)*((double)size_image/100.0)),
java.awt.Image.SCALE_DEFAULT));
image.setIcon(icon);
}
}

public void actual(){
System.out.println(“ACTUAL”);
if(original_image == null){
Alert.Message(parent,”Anda harus membuka data gambar terlebih dahulu”,
new javax.swing.ImageIcon(“image\\warning.png”) );
return ;
}
size_image = 100;
javax.swing.Icon icon = new javax.swing.ImageIcon(
original_image.getScaledInstance(
original_image.getWidth(null),
original_image.getHeight(null),
java.awt.Image.SCALE_DEFAULT));
image.setIcon(icon);
}

private class aksi_mouse_status extends java.awt.event.MouseAdapter{

private String message;

public aksi_mouse_status(String message){
this.message = message;
}

public void mouseEntered(java.awt.event.MouseEvent event){
status.setText(message);
}

public void mouseExited(java.awt.event.MouseEvent event){
status.setText(“Status Bar”);
}

}

private class aksi_selected_file implements java.beans.PropertyChangeListener{

public void propertyChange(java.beans.PropertyChangeEvent event){
try{
if(event.getPropertyName().equals(
javax.swing.JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)){
String value = event.getNewValue().toString();
view_image.setImage(value);
}
}catch(Throwable trouble){
view_image.setIcon(null);
}
}

}

private class aksi_menu implements java.awt.event.ActionListener{

public void actionPerformed(java.awt.event.ActionEvent event){
String perintah = event.getActionCommand();
if(perintah.equals(“Data”)){
layout_toolbar.show(toolbar,”file”);
}
if(perintah.equals(“Lihat”)){
layout_toolbar.show(toolbar,”view”);
}
if(perintah.equals(“Tentang”)){
layout_toolbar.show(toolbar,”about”);
}
}

}

private class aksi_button implements java.awt.event.ActionListener{

public void actionPerformed(java.awt.event.ActionEvent event){
String perintah = event.getActionCommand();
if(perintah.equals(button_about.getActionCommand())){
about();
}else if(perintah.equals(button_actual.getActionCommand())){
actual();
}else if(perintah.equals(button_enlarge.getActionCommand())){
enlarge();
}else if(perintah.equals(button_exit.getActionCommand())){
exit();
}else if(perintah.equals(button_open.getActionCommand())){
open();
}else if(perintah.equals(button_reduce.getActionCommand())){
reduce();
}
}

}

}

class JDialog extends javax.swing.JDialog{

private JImage parent;
private java.awt.Image gambar;
private javax.swing.JLabel icon;

public JDialog(JImage frame){

try{
gambar = javax.imageio.ImageIO.read(
new java.io.File(“image\\Tentang.jpg”) );
}catch(java.io.IOException e){
}

parent = frame;
setUndecorated(true);

setDefaultCloseOperation(javax.swing.JDialog.DISPOSE_ON_CLOSE);

icon = new javax.swing.JLabel(
new javax.swing.ImageIcon(gambar) );
add(icon);
setSize(gambar.getWidth(null), gambar.getHeight(null));

java.awt.Dimension dim = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

int x = (int) (dim.getWidth()-gambar.getWidth(null))/2;
int y = (int) (dim.getHeight()-gambar.getHeight(null))/2;

setLocation(x,y);

addMouseListener(new java.awt.event.MouseAdapter(){
public void mouseReleased(java.awt.event.MouseEvent event){
setVisible(false);
}
} );

}

public void setParent(JImage frame){
parent = frame;
}

public void setVisible(boolean isVisible){

parent.setEnabled(!isVisible);
super.setVisible(isVisible);

}

}

class FileFilter extends javax.swing.filechooser.FileFilter{

public FileFilter(){

}

public boolean accept(java.io.File f){

if(f.isDirectory()){
return true;
}
String file = f.getName().toUpperCase();
if(file.endsWith(“.JPG”) || file.endsWith(“.GIF”) ||
file.endsWith(“PNG”) || file.endsWith(“BMP”)){
return true;
}
return false;

}

public String getDescription(){
return “Image File (GIF, JPG, PNG, BMP)”;
}

}

class FileView extends javax.swing.filechooser.FileView{

javax.swing.filechooser.FileFilter filter;

public FileView(javax.swing.filechooser.FileFilter filter){
this.filter = filter;
}

public javax.swing.Icon getIcon(java.io.File f){

if(f.isDirectory()){
return null;
}
if(filter.accept(f)){
String file = f.getName().toUpperCase();
if(file.endsWith(“.JPG”)){
return new javax.swing.ImageIcon(“image\\JPG.png”);
}
if(file.endsWith(“.GIF”)){
return new javax.swing.ImageIcon(“image\\GIF.png”);
}
if(file.endsWith(“.PNG”)){
return new javax.swing.ImageIcon(“image\\PNG.png”);
}
if(file.endsWith(“.BMP”)){
return new javax.swing.ImageIcon(“image\\BMP.png”);
}
return null;
}else{
return null;
}

}

}

class Preview extends javax.swing.JLabel{

private java.awt.Image gambar;

public Preview(javax.swing.JFileChooser parent){
setHorizontalAlignment(javax.swing.JLabel.CENTER);
setVerticalAlignment(javax.swing.JLabel.CENTER);
setPreferredSize(new java.awt.Dimension(300,300) );
setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.BLACK));
}

public void setImage(String path){

try{
gambar = javax.imageio.ImageIO.read(
new java.io.File(path) );
if(gambar.getWidth(null) > gambar.getHeight(null)){
if(gambar.getWidth(null) > 300 | gambar.getHeight(null) > 300){
setIcon(new javax.swing.ImageIcon(
gambar.getScaledInstance(300,-1,
java.awt.Image.SCALE_FAST)) );
}else{
setIcon(new javax.swing.ImageIcon(gambar) );
}
}else{
if(gambar.getWidth(null) > 300 | gambar.getHeight(null) > 300){
setIcon(new javax.swing.ImageIcon(
gambar.getScaledInstance(-1,300,
java.awt.Image.SCALE_FAST)) );
}else{
setIcon(new javax.swing.ImageIcon(gambar) );
}
}
repaint();
}catch(java.io.IOException e){
}

}

}

class Alert{

public static void Message(javax.swing.JFrame parent, String message, javax.swing.Icon icon){
javax.swing.JOptionPane.showMessageDialog(
parent,
message,
“Peringatan..!!!”,
javax.swing.JOptionPane.WARNING_MESSAGE,
icon);
}

public static int Ask(javax.swing.JFrame parent, String message, javax.swing.Icon icon){
int i = javax.swing.JOptionPane.showConfirmDialog(
parent,
message,
“Peringatan..!!!”,
javax.swing.JOptionPane.YES_NO_OPTION,
javax.swing.JOptionPane.WARNING_MESSAGE,
icon);
return i;
}

}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s