DESAIN
1. SOURCE untuk CLASS Fgambar
package pmodul6;
import java.awt.Image;
import javax.swing.*;
import java.io.*;
import javax.swing.filechooser.FileFilter;
public class FGambar extends javax.swing.JFrame {
DefaultListModel gambar;
final String[]
EXT = {".jpg", ".jpeg", ".gif",
".tiff", ".png"};
public
FGambar() {
initComponents();
gambar =
new DefaultListModel();
lstDaftar.setModel(gambar);
private void
btnPilihActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fc = new JFileChooser();
fc.addChoosableFileFilter(new JenisFile(EXT));
int hasil =
fc.showOpenDialog(this);
if (hasil
== JFileChooser.APPROVE_OPTION) {
File
file = fc.getSelectedFile();
String
nf = file.getAbsolutePath();
gambar.addElement(nf); } }
private void
btnHapusActionPerformed(java.awt.event.ActionEvent evt) {
int index = lstDaftar.getSelectedIndex();
gambar.remove(index);
lblGambar.setIcon(null); }
private void
lstDaftarMouseClicked(java.awt.event.MouseEvent evt) {
String nf =
lstDaftar.getSelectedValue().toString();
ImageIcon
gbr = new ImageIcon(nf);
if
(chkFit.isSelected())
gbr =
new ImageIcon(gbr.getImage().getScaledInstance(200,
-1,
Image.SCALE_DEFAULT));
lblGambar.setIcon(gbr);
}
Tambahan
private void
chkFitMouseClicked(java.awt.event.MouseEvent evt) {
String nf =
lstDaftar.getSelectedValue().toString();
ImageIcon gbr = new ImageIcon(nf);
if (chkFit.isSelected())
gbr = new
ImageIcon(gbr.getImage().getScaledInstance(200,
-1,
Image.SCALE_DEFAULT));
lblGambar.setIcon(gbr); }
|
Hasil Running Program
Sebelum dipilih fit lebar
Sesudah dipilih fit lebar
2. SOURCE untuk CLASS JenisFile
package pmodul6;
import
javax.swing.filechooser.FileFilter;
import java.io.*;
public class JenisFile extends
FileFilter {
private String[] s;
public JenisFile(String[] sArgs) {
s = sArgs; }
public boolean accept(File fArg) {
if (fArg.isDirectory())
return true;
for (int i = 0; i < s.length; ++i)
if
(fArg.getName().toLowerCase().indexOf(s[i].toLowerCase()) > 0)
return true;
return false; }
public String getDescription() {
String tmp = "";
for (int i = 0; i<s.length; ++i)
tmp += "*" + s[i] +
" ";
return tmp; } }
0 komentar:
Posting Komentar