trizehn

OFImage

Dec 9th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1.     /**
  2.  * Write a description of class OFImage here.
  3.  *
  4.  * @author Daffa Tristan Firdaus
  5.  * @version 7 Desember 2020
  6.  */
  7. import java.awt.*;
  8. import java.awt.image.*;
  9. import javax.swing.*;
  10.  
  11. public class OFImage extends BufferedImage
  12. {
  13.     public OFImage(BufferedImage image)
  14.     {
  15.         super(image.getColorModel(), image.copyData(null), image.isAlphaPremultiplied(), null);
  16.     }
  17.  
  18.     public OFImage(int width, int height)
  19.     {
  20.         super(width, height, TYPE_INT_RGB);
  21.     }
  22.  
  23.     public void setPixel(int x, int y, Color warna)
  24.     {
  25.         int pixel = warna.getRGB();
  26.         setRGB(x, y, pixel);
  27.     }
  28.  
  29.     public Color getPixel(int x, int y)
  30.     {
  31.         int pixel = getRGB(x, y);
  32.         return new Color(pixel);
  33.     }
  34.  
  35.     public void darker() {
  36.         int height = getHeight();
  37.         int width = getWidth();
  38.         for (int i = 0; i < height; i++)
  39.         {
  40.             for (int j = 0; j < width; j++)
  41.             {
  42.                 setPixel(j, i, getPixel(j, i).darker());
  43.             }
  44.         }
  45.     }
  46.  
  47.     public void lighter()
  48.     {
  49.         int height = getHeight();
  50.         int width = getWidth();
  51.         for (int i = 0; i < height; i++)
  52.         {
  53.             for (int j = 0; j < width; j++)
  54.             {
  55.                 setPixel(j, i, getPixel(j, i).brighter());
  56.             }
  57.         }
  58.     }
  59.  
  60.     public void threshold()
  61.     {
  62.         int height = getHeight();
  63.         int width = getWidth();
  64.         for (int i = 0; i < height; i++)
  65.         {
  66.             for (int j = 0; j < width; j++)
  67.             {
  68.                 Color pixel = getPixel(j, i);
  69.                 int brightness = (pixel.getRed() + pixel.getBlue() + pixel.getGreen()) / 3;
  70.                 if (brightness <= 85)
  71.                 {
  72.                     setPixel(j, i, Color.BLACK);
  73.                 }
  74.                 else if (brightness <= 85 * 2)
  75.                 {
  76.                     setPixel(j, i, Color.GRAY);
  77.                 }
  78.                 else
  79.                 {
  80.                     setPixel(j, i, Color.WHITE);
  81.                 }
  82.             }
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment