Dwinanda

Image Viewer (OFImage)

Dec 7th, 2020
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1.  
  2. /**
  3.  * Source code ini berisikan tentang editan-editan yang akan ditampilkan nantinya
  4.  *
  5.  * @author Dwinanda Bagoes Ansori
  6.  * @version Final Version, 8 Desember 2020
  7.  */
  8. import java.awt.*;
  9. import java.awt.image.*;
  10. import javax.swing.*;
  11.  
  12. public class OFImage extends BufferedImage
  13. {
  14.     public OFImage(BufferedImage image)
  15.     {
  16.         super(image.getColorModel(), image.copyData(null),
  17.                 image.isAlphaPremultiplied(), null);
  18.     }
  19.  
  20.     public OFImage(int width, int height)
  21.     {
  22.         super(width, height, TYPE_INT_RGB);
  23.     }
  24.    
  25.     private void setPixel(int x, int y, Color col)
  26.     {
  27.         int pixel = col.getRGB();
  28.         setRGB(x, y, pixel);
  29.     }
  30.    
  31.     public Color getPixel(int x, int y)
  32.     {
  33.         int pixel = getRGB(x, y);
  34.         return new Color(pixel);
  35.     }
  36.    
  37.     public void darker()
  38.     {
  39.         int height = getHeight();
  40.         int width = getWidth();
  41.         for(int y = 0; y < height; y++)
  42.         {
  43.             for(int x = 0; x < width; x++)
  44.             {
  45.                 setPixel(x, y, getPixel(x, y).darker());
  46.             }
  47.         }
  48.     }
  49.    
  50.     public void lighter()
  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.     public void threshold()
  64.     {
  65.         int height = getHeight();
  66.         int width = getWidth();
  67.         for(int y = 0; y < height; y++)
  68.         {
  69.             for(int x = 0; x < width; x++)
  70.             {
  71.                 Color pixel = getPixel(x, y);
  72.                 int brightness = (pixel.getRed() + pixel.getBlue() + pixel.getGreen()) / 3;
  73.                
  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.                 {
  84.                     setPixel(x, y, Color.WHITE);
  85.                 }
  86.             }
  87.         }
  88.     }
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment