Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.awt.*;  
  2. import java.awt.image.*;  
  3. import javax.swing.*;
  4.  
  5. //Class yang mendefinisikan sebuah gambar dalam OF (Objects First) format
  6. public class OF extends BufferedImage
  7. {
  8.     //Membuat gambar yang dicopy dari sebuah BufferedImage
  9.     public OF(BufferedImage img)
  10.     {
  11.         super(img.getColorModel(), img.copyData(null), img.isAlphaPremultiplied(), null);
  12.     }
  13.  
  14.     //Membuat gambar OF dengan ukuran tertentu
  15.     public OF(int width, int height)
  16.     {
  17.         super(width, height, TYPE_INT_RGB);
  18.     }
  19.    
  20.     //Mengatur pixel ke warna RGB
  21.     //dengan x dan y adalah posisi pixel, dan color adalah warna pixel
  22.     public void setPixel (int x, int y, Color color)
  23.     {
  24.         int pixel=color.getRGB();
  25.         setRGB(x, y, pixel);
  26.     }
  27.    
  28.     //Mendapatkan value color pada pixel tertentu
  29.     public Color getPixel(int x, int y)
  30.     {
  31.         int pixel = getRGB(x, y);
  32.         return new Color (pixel);
  33.     }
  34.    
  35.     //Untuk membuat gambar menjadi lebih gelap
  36.     public void dark ()
  37.     {
  38.         int height = getHeight();
  39.         int width = getWidth();
  40.         for (int y=0; y<height; y++)
  41.         {
  42.             for (int x=0; x<width; x++)
  43.             {
  44.                 setPixel(x,y,getPixel(x,y).darker());
  45.             }
  46.         }
  47.     }
  48.    
  49.     //Untuk membuat gambar menjadi lebih terang
  50.     public void bright ()
  51.     {
  52.         int height = getHeight();
  53.         int width = getWidth();
  54.         for (int y=0; y<height; y++)
  55.         {
  56.             for (int x=0; x<width; x++)
  57.             {
  58.                 setPixel(x,y,getPixel(x,y).brighter());
  59.             }
  60.         }
  61.     }
  62.    
  63.     //Threshold: membuat gambar hanya berwarna hitam, putih, abu-abu
  64.     public void threshold()
  65.     {
  66.         int height = getHeight();
  67.         int width = getWidth();
  68.         for (int y=0; y<height; y++)
  69.         {
  70.             for (int x=0; x<width; x++)
  71.             {
  72.                 Color pixel = getPixel(x,y);
  73.                 int brightness = (pixel.getRed() + pixel.getBlue() + pixel.getGreen())/3;
  74.                 if (brightness <= 85)
  75.                 {
  76.                     setPixel (x,y,Color.BLACK);
  77.                 }
  78.                 else if (brightness <= 170)
  79.                 {
  80.                     setPixel (x, y, Color.GRAY);
  81.                 }
  82.                 else
  83.                 setPixel (x, y, Color.WHITE);
  84.             }
  85.         }
  86.     }  
  87. }