Vous êtes sur la page 1sur 19

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Praktikum5;

import Praktikum1.citra1;
import Praktikum2.citra2;
import Praktikum3.citra3;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
*
* @author Riska
*/
public class citra5 extends javax.swing.JFrame {

BufferedImage image=null;

int width;
int height;
String scrIm = "dandelion.jpg";

/**
* Creates new form citra5
*/
public citra5() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

jLabel3 = new javax.swing.JLabel();


jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextPane1 = new javax.swing.JTextPane();
jButton3 = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();

jTextPane2 = new javax.swing.JTextPane();


jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jButton7 = new javax.swing.JButton();

jLabel3.setText("jLabel3");

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("Load");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("Grayscale");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jScrollPane1.setViewportView(jTextPane1);

jButton3.setText("Brightness");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

jScrollPane2.setViewportView(jTextPane2);

jButton4.setText("Contrast");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

jButton5.setText("Invers");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});

jButton6.setText("Auto-Level");

jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});

jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N


jLabel4.setText("Pengolahan Citra");

jLabel5.setText("Riska Ulfia ~ 361455401070");

jButton7.setText("at.clr");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(35, 35, 35)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 334,
javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 72,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addGap(28, 28, 28)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 50,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton3)))
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 380,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 52,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4)
.addGap(34, 34, 34)
.addComponent(jButton5)
.addGap(31, 31, 31)
.addComponent(jButton6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31,
Short.MAX_VALUE)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 65,
javax.swing.GroupLayout.PREFERRED_SIZE))))

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 227,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(248, 248, 248))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 143,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(318, 318, 318))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(5, 5, 5)
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING,
false)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(7, 7, 7))
.addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1)
.addComponent(jScrollPane2)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton7, javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGap(1, 1, 1)))
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 35,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 324,
Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 87,
Short.MAX_VALUE)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 14,
javax.swing.GroupLayout.PREFERRED_SIZE))
);

pack();
}// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton1ActionPerformed

// load
File input = new File(scrIm);
try {
image = ImageIO.read(input);
} catch (IOException ex) {
Logger.getLogger(citra2.class.getName()).log(Level.SEVERE, null, ex);
}
Icon imageIcon = new ImageIcon(image);
jLabel1.setIcon(imageIcon);
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton2ActionPerformed


// grayscale
//grayscale
File input = new File(scrIm);
try {
image = ImageIO.read(input);
width = image.getWidth();
height = image.getHeight();

BufferedImage grayScale = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

for (int i = 0; i <width; i++) {


for (int j = 0; j < height; j++) {
Color w = new Color (image.getRGB(i, j));
int red = (int)(w.getRed());
int green = (int) (w.getGreen());

int blue = (int) (w.getBlue());


int avg = (red+green+blue) /3 ;
Color newColor = new Color(avg,avg,avg);
grayScale.setRGB(i, j, newColor.getRGB());

}
}

ImageIcon imageIcon;
imageIcon = new ImageIcon(grayScale);
jLabel2.setIcon(imageIcon);
} catch (IOException ex) {
Logger.getLogger(citra5.class.getName()).log(Level.SEVERE,null, ex);
}
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton3ActionPerformed


// Brightness
File input = new File(scrIm);
width = image.getWidth();
height = image.getHeight();
int a = Integer.parseInt(jTextPane1.getText());
try{
image = ImageIO.read(input);

for (int i = 0; i <width ; i++) {


for (int j = 0; j < height; j++) {

Color w = new Color(image.getRGB(i, j));


// masing" warna menjadi grayscale
int red=(int)(w.getRed());
int blue=(int)(w.getBlue());
int green=(int)(w.getGreen());
//warna RGB grayscale
int xg = (int) (red+green+blue)/3;

int xb = xg+a;
if (xb < 0) {
xb = 0;
} else if (xb>255) {
xb = 255;
}
Color newcolor=new Color(xb,xb,xb);
image.setRGB(i,j,newcolor.getRGB());
}
}
ImageIcon imageIcon = new ImageIcon(image);
jLabel2.setIcon(imageIcon);
} catch (IOException ex) {
java.util.logging.Logger.getLogger(citra5.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_jButton3ActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton4ActionPerformed


// Contras

File input = new File(scrIm);


width = image.getWidth();
height = image.getHeight();
double c = Double.parseDouble(jTextPane2.getText());
try{
image = ImageIO.read(input);

for (int i = 0; i <width ; i++) {


for (int j = 0; j < height; j++) {
Color w = new Color(image.getRGB(i, j));
// masing" warna menjadi grayscale
int red=(int)(w.getRed());
int blue=(int)(w.getBlue());
int green=(int)(w.getGreen());
//warna RGB digabungkan
int xg = (int) (red+green+blue)/3;
int xb = (int) (xg*c);

Color newcolor=new Color(xb,xb,xb);


image.setRGB(i,j,newcolor.getRGB());
}
}
ImageIcon imageIcon = new ImageIcon(image);
jLabel2.setIcon(imageIcon);
} catch (IOException ex) {
java.util.logging.Logger.getLogger(citra5.class.getName()).log(Level.SEVERE, null, ex);
}

}//GEN-LAST:event_jButton4ActionPerformed

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton5ActionPerformed


// invers
File input = new File(scrIm);
width = image.getWidth();
height = image.getHeight();
//int c = Integer.parseInt(jTextPane2.getText());
try{
image = ImageIO.read(input);

for (int i = 0; i <width ; i++) {


for (int j = 0; j < height; j++) {
Color w = new Color(image.getRGB(i, j));
// masing" warna menjadi grayscale
int red=(int)(w.getRed());
int blue=(int)(w.getBlue());
int green=(int)(w.getGreen());
//warna RGB digabungkan
int xg = (int) (red+green+blue)/3;
int xb = (int) (255-xg);

Color newcolor=new Color(xb,xb,xb);


image.setRGB(i,j,newcolor.getRGB());
}
}
ImageIcon imageIcon = new ImageIcon(image);

jLabel2.setIcon(imageIcon);
} catch (IOException ex) {
java.util.logging.Logger.getLogger(citra5.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_jButton5ActionPerformed

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton6ActionPerformed


// autolevel
File input = new File(scrIm);
width = image.getWidth();
height = image.getHeight();

try{
image = ImageIO.read(input);
int xgmax=0;
int xgmin=255;
for (int i = 0; i <width ; i++) {
for (int j = 0; j < height; j++) {
Color w = new Color(image.getRGB(i, j));
// masing" warna menjadi grayscale
int red=(int)(w.getRed());
int blue=(int)(w.getBlue());
int green=(int)(w.getGreen());
//warna RGB digabungkan
int xg = (int) (red+green+blue)/3;
if (xg < xgmin) {
xgmin = xg;

}
if (xg > xgmax) {
xgmax = xg;
}
}
}
for (int i = 0; i <width ; i++) {
for (int j = 0; j < height; j++) {
Color w = new Color(image.getRGB(i, j));
// masing" warna menjadi grayscale
int red=(int)(w.getRed());
int blue=(int)(w.getBlue());
int green=(int)(w.getGreen());
//warna RGB digabungkan
int xg = (int) (red+green+blue)/3;

int xb=(int) (255*(xg-xgmin)/(xgmax-xgmin));


Color newcolor=new Color(xb,xb,xb);
image.setRGB(i,j,newcolor.getRGB());
}
}
ImageIcon imageIcon = new ImageIcon(image);
jLabel2.setIcon(imageIcon);
} catch (IOException ex) {
java.util.logging.Logger.getLogger(citra5.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_jButton6ActionPerformed

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GENFIRST:event_jButton7ActionPerformed


// autolevel color
File input = new File(scrIm);
width = image.getWidth();
height = image.getHeight();

try{
image = ImageIO.read(input);
int xgmax=0;
int xgmin=255;
for (int i = 0; i <width ; i++) {
for (int j = 0; j < height; j++) {
Color w = new Color(image.getRGB(i, j));
// masing" warna menjadi grayscale
int red=(int)(w.getRed());
int blue=(int)(w.getBlue());
int green=(int)(w.getGreen());
//warna RGB digabungkan
int xg = (int) (red+green+blue)/3;

if (xg < xgmin) {


xgmin = xg;
}
if (xg > xgmax) {
xgmax = xg;
}

}
}
for (int i = 0; i <width ; i++) {
for (int j = 0; j < height; j++) {
Color w = new Color(image.getRGB(i, j));
// masing" warna menjadi grayscale
int red=(int)(w.getRed());
int blue=(int)(w.getBlue());
int green=(int)(w.getGreen());
//warna RGB digabungkan
int xg = (int) (red+green+blue)/3;

int xb=(int) (255*(xg-xgmin)/(xgmax-xgmin));


Color newcolor=new Color(xb,xb,xb);
image.setRGB(i,j,newcolor.getRGB());
}
}
ImageIcon imageIcon = new ImageIcon(image);
jLabel2.setIcon(imageIcon);
} catch (IOException ex) {
java.util.logging.Logger.getLogger(citra5.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_jButton7ActionPerformed

/**
* @param args the command line arguments
*/

public static void main(String args[]) {


/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(citra5.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(citra5.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(citra5.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(citra5.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new citra5().setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables


private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextPane jTextPane1;
private javax.swing.JTextPane jTextPane2;
// End of variables declaration//GEN-END:variables
}

Vous aimerez peut-être aussi