Created
March 8, 2013 16:45
-
-
Save resarahadian/5117858 to your computer and use it in GitHub Desktop.
Image Preview FileChooser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.EventQueue; | |
import javax.swing.ImageIcon; | |
import javax.swing.JFileChooser; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.UIManager; | |
import javax.swing.UnsupportedLookAndFeelException; | |
import javax.swing.border.EmptyBorder; | |
import javax.swing.JLabel; | |
import javax.swing.JButton; | |
import java.awt.event.ActionListener; | |
import java.awt.event.ActionEvent; | |
import javax.swing.border.EtchedBorder; | |
@SuppressWarnings("serial") | |
public class FrameView extends JFrame | |
{ | |
private JPanel contentPane; | |
private JLabel lblImage; | |
private JButton btnTampil; | |
private JFileChooser fc = new JFileChooser(); | |
/** | |
* Create the frame. | |
*/ | |
public FrameView() | |
{ | |
setResizable(false); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setBounds(100, 100, 677, 553); | |
contentPane = new JPanel(); | |
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); | |
setContentPane(contentPane); | |
contentPane.setLayout(null); | |
setLocationRelativeTo(null); | |
lblImage = new JLabel(""); | |
lblImage.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null)); | |
lblImage.setBounds(12, 12, 651, 431); | |
contentPane.add(lblImage); | |
btnTampil = new JButton("Buka Gambar"); | |
btnTampil.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/Gambar/Simpan.png")); | |
fc.setAccessory(new PreviewChooser(fc)); | |
btnTampil.addActionListener(new ActionListener() | |
{ | |
public void actionPerformed(ActionEvent act) | |
{ | |
int buka = fc.showOpenDialog(lblImage); | |
if(buka == JFileChooser.APPROVE_OPTION) | |
{ | |
String src = fc.getSelectedFile().getPath(); | |
lblImage.setIcon(new ImageIcon(src)); | |
} | |
} | |
}); | |
btnTampil.setBounds(277, 455, 169, 40); | |
contentPane.add(btnTampil); | |
} | |
/** | |
* Launch the application. | |
*/ | |
public static void main(String[] args) | |
{ | |
EventQueue.invokeLater(new Runnable() | |
{ | |
public void run() | |
{ | |
try | |
{ | |
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); | |
FrameView frame = new FrameView(); | |
frame.setVisible(true); | |
} | |
catch (UnsupportedLookAndFeelException e) { | |
} | |
catch (ClassNotFoundException e){ | |
} | |
catch (InstantiationException e) { | |
} | |
catch (IllegalAccessException e) { | |
} | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Dimension; | |
import java.awt.Graphics; | |
import java.awt.Image; | |
import java.beans.PropertyChangeEvent; | |
import java.beans.PropertyChangeListener; | |
import java.io.File; | |
import javax.swing.ImageIcon; | |
import javax.swing.JComponent; | |
import javax.swing.JFileChooser; | |
@SuppressWarnings("serial") | |
public class PreviewChooser extends JComponent implements PropertyChangeListener | |
{ | |
Double lebarImg = 200.0; | |
Double tinggiImg = 200.0; | |
File file = null; | |
ImageIcon imageIcon = null; | |
public PreviewChooser(JFileChooser fileChooser) | |
{ | |
setPreferredSize(new Dimension(lebarImg.intValue(),tinggiImg.intValue())); | |
fileChooser.addPropertyChangeListener(this); | |
} | |
public double getScale(ImageIcon img) | |
{ | |
double skala; | |
if((lebarImg>=img.getIconWidth()) && (tinggiImg >= img.getIconHeight())) | |
{ | |
skala = 1; | |
} | |
else | |
{ | |
double lebarSkala = lebarImg / img.getIconWidth(); | |
double tinggiSkala = tinggiImg / img.getIconHeight(); | |
if(lebarSkala > tinggiSkala) | |
{ | |
skala = tinggiSkala; | |
} | |
else | |
{ | |
skala = lebarSkala; | |
} | |
} | |
return skala; | |
} | |
public void propertyChange(PropertyChangeEvent pce) | |
{ | |
String properti = pce.getPropertyName(); | |
if(JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(properti)) | |
{ | |
file = null; | |
} | |
else if(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(properti)) | |
{ | |
file = (File) pce.getNewValue(); | |
} | |
if(file == null) | |
{ | |
imageIcon = null; | |
} | |
else | |
{ | |
ImageIcon imgIcon = new ImageIcon(file.getPath()); | |
double skala = getScale(imgIcon); | |
imageIcon = new ImageIcon(imgIcon.getImage().getScaledInstance((int)((double)imgIcon.getIconWidth() * skala),(int)((double)imgIcon.getIconHeight() * skala),Image.SCALE_DEFAULT)); | |
} | |
repaint(); | |
} | |
protected void paintComponent(Graphics graph) | |
{ | |
if(imageIcon != null) | |
{ | |
int x = getWidth() / 2 - imageIcon.getIconWidth() / 2; | |
int y = getHeight() / 2 - imageIcon.getIconHeight() / 2; | |
imageIcon.paintIcon(this, graph, x, y); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment