Membuat Image Viewer Dengan Bluej Versi 2.0
Nama : Isnaini Nurul KurniaSari
NRP : 05111740000010
Kelas : PBO B
Berikut ini adalah langkah-langkah untuk menjalankan aplikasi ImageViewer versi 2.0:
NRP : 05111740000010
Kelas : PBO B
Tugas Pemrograman Berbasis Objek (PBO)
Membuat Image Viewer Dengan Bluej Versi 2.0
Pada postingan kali ini, saya membuat Image Viewer versi 1.0 dengan GUI (Graphical User Interface). GUI merupakan desain aplikasi dengan tampilan visual sehingga pengguna dapat dengan mudah menggunakan aplikasi. Berikut class dan source code untuk membuat Image Viewer dengan GUI. Source code di bawah ini merupakan implementasi dari source yang ada di buku:
Berikut ini class yang dibutuhkan untuk membuat Pemrograman Image Viewer dengan Blue J
versi 2.0 adalah:
1. Class ImageViewer
2. Class ImageFileManager
3. Class ImagePanel
4. Class OFImage
5. Class Filter
6. Class ThresholdFilter
7. Class LighterFilter
8. Class DarkerFilter
Berikut ini adalah tampilan class-class pada blue J:
Tampilan class-class image viewer versi 2.0 pada blue J
- Berikut ini adalah source code untuk class Image Viewer:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.File;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
/**
* ImageViewer is the main class of the image viewer application. It builds and
* displays the application GUI and initialises all other components.
*
* To start the application, create an object of this class.
*
* @author Isnaini Nurul KurniaSari/05111740000010
* @class PBO B
* @version 1.0/20181126
*/
public class ImageViewer
{
private static final String VERSION = "Version 2.0";
private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
private JFrame frame;
private ImagePanel imagePanel;
private JLabel filenameLabel;
private JLabel statusLabel;
private OFImage currentImage;
private List<Filter> filters;
public ImageViewer()
{
currentImage = null;
filters = createFilters();
makeFrame();
}
private void openFile()
{
int returnVal = fileChooser.showOpenDialog(frame);
if(returnVal != JFileChooser.APPROVE_OPTION) {
return;
}
File selectedFile = fileChooser.getSelectedFile();
currentImage = ImageFileManager.loadImage(selectedFile);
if(currentImage == null) {
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();
}
private void close()
{
currentImage = null;
imagePanel.clearImage();
showFilename(null);
}
private void quit()
{
System.exit(0);
}
private void applyFilter(Filter filter)
{
if(currentImage != null) {
filter.apply(currentImage);
frame.repaint();
showStatus("Applied: " + filter.getName());
}
else {
showStatus("No image loaded.");
}
}
private void showAbout()
{
JOptionPane.showMessageDialog(frame,
"ImageViewer\n" + VERSION,
"About ImageViewer",
JOptionPane.INFORMATION_MESSAGE);
}
private void showFilename(String filename)
{
if(filename == null) {
filenameLabel.setText("No file displayed.");
}
else {
filenameLabel.setText("File: " + filename);
}
}
private void showStatus(String text)
{
statusLabel.setText(text);
}
private List<Filter> createFilters()
{
List<Filter> filterList = new ArrayList<Filter>();
filterList.add(new DarkerFilter("Darker"));
filterList.add(new LighterFilter("Lighter"));
filterList.add(new ThresholdFilter("Threshold"));
return filterList;
}
private void makeFrame()
{
frame = new JFrame("ImageViewer");
Container contentPane = frame.getContentPane();
makeMenuBar(frame);
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);
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 frame)
{
final int SHORTCUT_MASK =
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu menu;
JMenuItem item;
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);
menu = new JMenu("Filter");
menubar.add(menu);
for(final Filter filter : filters) {
item = new JMenuItem(filter.getName());
item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
applyFilter(filter);
}
});
menu.add(item);
}
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);
}
}
- Berikut ini adalah source code untuk class ImageFileManager
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
/**
* ImageFileManager is a small utility class with static methods to load
* and save images.
*
* The files on disk can be in JPG or PNG image format. For files written
* by this class, the format is determined by the constant IMAGE_FORMAT.
*
* @author Isnaini Nurul KurniaSari/05111740000010
* @class PBO B
* @version 1.0/20181126
*/
public class ImageFileManager
{
private static final String IMAGE_FORMAT = "jpg";
public static OFImage loadImage(File imageFile)
{
try {
BufferedImage image = ImageIO.read(imageFile);
if(image == null || (image.getWidth(null) < 0)) {
return null;
}
return new OFImage(image);
}
catch(IOException exc) {
return null;
}
}
public static void saveImage(OFImage image, File file)
{
try {
ImageIO.write(image, IMAGE_FORMAT, file);
}
catch(IOException exc) {
return;
}
}
}
- Berikut ini adalah source code untuk class ImagePanel
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
/**
* An ImagePanel is a Swing component that can display an OFImage.
* It is constructed as a subclass of JComponent with the added functionality
* of setting an OFImage that will be displayed on the surface of this
* component.
*
* @author Isnaini Nurul KurniaSari/05111740000010
* @class PBO B
* @version 1.0/20181126
*/
public class ImagePanel extends JComponent
{
private int width, height;
private OFImage panelImage;
public ImagePanel()
{
width = 360;
height = 240;
panelImage = null;
}
public void setImage(OFImage image)
{
if(image != null) {
width = image.getWidth();
height = image.getHeight();
panelImage = image;
repaint();
}
}
public void clearImage()
{
Graphics imageGraphics = panelImage.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(panelImage != null) {
g.drawImage(panelImage, 0, 0, null);
}
}
}
- Berikut ini adalah source code untuk class OFImage
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
/**
* OFImage is a class that defines an image in OF (Objects First) format.
*
* @author Isnaini Nurul KurniaSari/05111740000010
* @class PBO B
* @version 1.0/20181126
*/
public class OFImage extends BufferedImage
{
public OFImage(BufferedImage image)
{
super(image.getColorModel(), image.copyData(null),
image.isAlphaPremultiplied(), null);
}
public OFImage(int width, int height)
{
super(width, height, TYPE_INT_RGB);
}
public void setPixel(int x, int y, Color col)
{
int pixel = col.getRGB();
setRGB(x, y, pixel);
}
public Color getPixel(int x, int y)
{
int pixel = getRGB(x, y);
return new Color(pixel);
}
}
- Berikut ini adalah source code untuk class Filter
/**
* Filter is an abstract superclass for all image filters in this
* application. Filters can be applied to OFImages by invoking the apply
* method.
*
* @author Isnaini Nurul KurniaSari/05111740000010
* @class PBO B
* @version 1.0/20181126
*/
public abstract class Filter
{
private String name;
public Filter(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public abstract void apply(OFImage image);
}
- Berikut ini adalah source code untuk class ThresholdFilter
import java.awt.Color;
/**
* An three-level gray-based threshold filter.
*
* @author Isnaini Nurul KurniaSari/05111740000010
* @class PBO B
* @version 1.0/20181126
*/
public class ThresholdFilter extends Filter
{
public ThresholdFilter(String name)
{
super(name);
}
public void apply(OFImage image)
{
int height = image.getHeight();
int width = image.getWidth();
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
Color pixel = image.getPixel(x, y);
int brightness = (pixel.getRed() + pixel.getBlue() + pixel.getGreen()) / 3;
if(brightness <= 85) {
image.setPixel(x, y, Color.BLACK);
}
else if(brightness <= 170) {
image.setPixel(x, y, Color.GRAY);
}
else {
image.setPixel(x, y, Color.WHITE);
}
}
}
}
}
- Berikut ini adalah source code untuk class LighterFilter
/**
* An image filter to make the image a bit lighter.
*
* @author Isnaini Nurul KurniaSari/05111740000010
* @class PBO B
* @version 1.0/20181126
*/
public class LighterFilter extends Filter
{
public LighterFilter(String name)
{
super(name);
}
public void apply(OFImage image)
{
int height = image.getHeight();
int width = image.getWidth();
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
image.setPixel(x, y, image.getPixel(x, y).brighter());
}
}
}
}
- Berikut ini adalah source code untuk class DarkerFilter
/**
* An image filter to make the image a bit darker.
*
* @author Isnaini Nurul KurniaSari/05111740000010
* @class PBO B
* @version 1.0/20181126
*/
public class DarkerFilter extends Filter
{
public DarkerFilter(String name)
{
super(name);
}
public void apply(OFImage image)
{
int height = image.getHeight();
int width = image.getWidth();
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
image.setPixel(x, y, image.getPixel(x, y).darker());
}
}
}
}
Berikut ini adalah langkah-langkah untuk menjalankan aplikasi ImageViewer versi 2.0:
Klik kanan pada class image viewer
kemudian klik new ImageViewer()
Klik OK
Klik open pada menubar File
(untuk membuka file gambar yang akan kita pilih)
Pilih File yang akan ditampilkan
Tampilan gambar asli pada gambar
Tampilan ketika menggunakan Darker Filter
Tampilan ketika menggunakan Lighter Filter
Tampilan ketika menggunakan Threshold Filter
Terimakasih
Semoga Bermanfaat :)
Comments
Post a Comment