TUGAS 7 PBO C

Tugas ini merupakan pembuatan sebuah program Image Viewer yang merupakan implementasi GUI dengan komponen AWT dan Swing. Program ini berfungsi untuk menampilkan gambar / foto dan memiliki beberapa filter di dalamnya.

Bentuk modul sebagai berikut


Untuk source code sebagai berikut

ImageViewer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
 
import java.io.File;
 
public class ImageViewer
{
    // Field static
    private static final String VERSION = "Version 1.0";
    private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
   
    // Field
    private JFrame frame;
    private ImagePanel imagePanel;
    private JLabel filenameLabel;
    private JLabel statusLabel;
    private OFImage currentImage;
   
    //Buat ImageViewer ditampilkan di layar
    public ImageViewer()
    {
        currentImage = null;
        makeFrame();
    }
   
    //Implementasi fungsi menu
   
    //fungsi Open: Buka pemilih file untuk memilih file gambar baru
    private void openFile()
    {
        int returnVal = fileChooser.showOpenDialog(frame);
       
        if (returnVal != JFileChooser.APPROVE_OPTION)
        {
            return; // cancelled
        }
       
        File selectedFile = fileChooser.getSelectedFile();
        currentImage = ImageFileManager.loadImage(selectedFile);
       
        if (currentImage == null) ///image file tidak valid
        {
            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();
    }
   
    //fungsi Close: menutup foto yang ditampilkan
    private void close()
    {
        currentImage = null;
        imagePanel.clearImage();
        showFilename(null);
    }
   
    //fungsi Quit: keluar dari aplikasi
    private void quit()
    {
        System.exit(0);
    }
   
    //fungsi 'Darker': membuat gambar lebih hitam
    private void makeDarker()
    {
        if (currentImage != null)
        {
            currentImage.darker();
            frame.repaint();
            showStatus("Applied: darker");
        }
       
        else
        {
            showStatus("No image loaded.");
        }
    }
   
    //fungsi 'lighter': membuat gambar lebih terang
    private void makeLighter()
    {
        if (currentImage != null)
        {
            currentImage.lighter();
            frame.repaint();
            showStatus("Applied: lighter");
        }
       
        else
        {
            showStatus("No image loaded.");
        }
    }
   
    //fungsi 'threshold': menerapkan filter threshold
    private void threshold()
    {
        if (currentImage != null)
        {
            currentImage.threshold();
            frame.repaint();
            showStatus("Applied: threshold");
        }
       
        else
        {
            showStatus("No image loaded.");
        }
    }
   
    //fungsi 'lighter': membuat gambar lebih terang
    private void showAbout()
    {
        JOptionPane.showMessageDialog(frame, "ImageViewer\n" + VERSION,
        "About ImageViewer", JOptionPane.INFORMATION_MESSAGE);
    }
   
    //Support methods
   
    /**
     * tampilkan nama file pada label yang sesuai.
     *
     * @param  filename nama file yang akan ditampilkan
     */
    private void showFilename(String filename)
    {
        if (filename == null)
        {
            filenameLabel.setText("No file displayed.");
        }
       
        else
        {
            filenameLabel.setText("File: " + filename);
        }
    }
   
    /**
     * Membuat frame swing beserta isinya
     *
     * @param  text pesan status yang akan ditampilkan
     */
    private void showStatus(String text)
    {
        statusLabel.setText(text);
    }
   
    //Swing stuff untuk membangun frame dan semua komponen lainnya
   
    //Membuat frame swing dan semua komponennya
    private void makeFrame()
    {
        frame = new JFrame("ImageViewer");
        makeMenuBar(frame);
       
        Container contentPane = frame.getContentPane();
       
        // Pengaturan layout
        contentPane.setLayout(new BorderLayout(6, 6));
       
        filenameLabel = new JLabel();
        contentPane.add(filenameLabel, BorderLayout.NORTH);
       
        imagePanel = new ImagePanel();
        contentPane.add(imagePanel, BorderLayout.CENTER);
       
        statusLabel = new JLabel(VERSION);
        contentPane.add(statusLabel, BorderLayout.SOUTH);
       
        // Mengatur dan menampilkan konten
        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);
    }
   
    /**
     * Membuat main frame's menu bar.
     *
     * @param frame Frame menu harus ditambahkan
     */
    private void makeMenuBar(JFrame frame)
    {
        final int SHORTCUT_MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
       
        JMenuBar menubar = new JMenuBar();
        frame.setJMenuBar(menubar);
       
        JMenu menu;
        JMenuItem item;
       
        // Membuat menu File
        menu = new JMenu("File");
        menubar.add(menu);
       
        item = new JMenuItem("Open");
            item.setAccelerator(KeyStroke.getKeyStroke(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);
       
        // Membuat menu Filter
        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 JMenuItem("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);
       
        // Membuat menu Help
        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);
    }
}


ImageFileManager

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class ImageFileManager
{
//Sebuah konstanta untuk format gambar yang digunakan untuk menulis
//Format yang tersedia adalah "jpg" dan "png"
private static final String IMAGE_FORMAT ="jpg";
 
/**
* Membaca file gambar dari disk, dan menampilkan nya menjadi sebuah
* gambar.
* Metode ini bisa membaca format file JPG dan PNG.
* Jika ada masalah (misalnya file tersebut tidak ada,tidak sesuai
* dengan format yang dapat dikodekan, atau kesalahan baca lainnya) metode ini
* tidak menampilkan apa-apa.
*
* @param imageFile File gambar yang akan loading.
* @return Objek gambar atau null yang tidak dapat dibaca.
*/
public static OFImage loadImage (File imageFile){
try {
BufferedImage image = ImageIO.read (imageFile);
if (image==null||(image.getWidth(null)<0)){
//kita tidak bisa memasukkan gambar- kemungkinan karna format salah
return null;
}return new OFImage (image);
}
catch (IOException exc){
return null;
}
}
 
/**
* Tulis file gambar ke disk.Format file adalah JPG.
* @param image Gambar akan disimpan
* @param file File yang akan disimpan
*/
public static void saveImage (OFImage image, File file){
try {
ImageIO.write(image,IMAGE_FORMAT,file);
}
catch (IOException exc){
return;
}
}
}


ImagePanel

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
public class ImagePanel extends JComponent {
 
//Tinggi dan lebar panel sebelumnya
private int width,height;
 
// Buffer gambar internal yang digunakan untuk melukis.
//Untuk tampilan yang sebenarnya,buffer gambar akan disalin ke layar
private OFImage panelImage;
 
//Buat imagepanel baru yang kosong
public ImagePanel ()
{
width =360;
height=240;
panelImage=null;
}
/**
* Pilih gambar yang akan ditampilkan panel
* @param image gambar yang ditampilkan
*/
public void setImage (OFImage image)
{
if (image != null)
{
width = image.getWidth();
height= image.getHeight();
panelImage=image;
repaint ();
}
}
 
//Menghilangkan gambar pada panel
public void clearImage(){
Graphics imageGraphics = panelImage.getGraphics();
imageGraphics.setColor(Color.LIGHT_GRAY);
imageGraphics.fillRect(0, 0, width, height);
repaint();
}
// Metode berikut adalah redefinisi metode yg didapat dari superclasses.
/**
* Buat ke layout manager berapa besar yg kita inginkan.
* (Metode ini dipanggil layout managers utk menempatkan komponen).
* @return Dimensi yg disukai utk komponen ini.
*/
public Dimension getPreferredSize(){
return new Dimension(width, height);
}
/**
* Komponen perlu ditampilkan lagi. Salin gambar internal ke layar.
* (Metode ini dipanggil Swing screen painter setiap kali mau menampilkan komponen)
* @param g Konteks grafik yg dpt digunakan utk menggambar pd komponen.
*/
public void paintComponent(Graphics g){
Dimension size = getSize();
g.clearRect(0, 0, size.width, size.height);
if(panelImage != null) {
g.drawImage(panelImage, 0, 0, null);
}
}
}
 


OFImage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
public class OFImage extends BufferedImage{
/**
* Buat OFImage yg merupakan salinan BufferedImage.
* @param image Gambar yg akan disalin.
*/
public OFImage(BufferedImage image){
super(image.getColorModel(), image.copyData(null),
image.isAlphaPremultiplied(), null);
}
/**
* Create an OFImage with specified size and unspecified content.
* @param width The width of the image.
* @param height The height of the image.
*/
public OFImage(int width, int height){
super(width, height, TYPE_INT_RGB);
}
/**
* Atur pixel gambar dg warna yg spesifik (r,g,b).
* @param x Posisi x dlm pixel.
* @param y Posisi y dlm pixel.
* @param col Warna pixel.
*/
public void setPixel(int x, int y, Color col){
int pixel = col.getRGB();
setRGB(x, y, pixel);
}
/**
* Dapatkan value dari warna pada posisi pixel.
* @param x Posisi x dlm pixel.
* @param y Posisi y dlm pixel.
* @return Warna pixel pada posisi yg sudah ditentukan.
*/
public Color getPixel(int x, int y){
int pixel = getRGB(x, y);
return new Color(pixel);
}
// Buat gambar lebih gelap.
public void darker(){
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());
}
}
}
// Buat gambar lebih cerah
public void lighter(){
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());
}
}
}
/**
* Tampilkan three level threshold operation.
* Untuk mewarnai ulang gambar dg tiga warna: putih, abu2, hitam
*/
public void threshold(){
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()) / 3;
if(brightness <= 85){
setPixel(x, y, Color.BLACK);
}else if(brightness <= 170){
setPixel(x, y, Color.GRAY);
}else{
setPixel(x, y, Color.WHITE);
}
}
}
}
}

Hasil dari program ini yaitu:

Normal


Lighter

Darker


Threshold









Comments

Popular posts from this blog

ETS PBO C (NO 3 dan 4)

Membuat Web Warung Tegal

TUGAS 4 PBO C