Advertisement
mnaufaldillah

OFImage Tugas 7

Dec 6th, 2020
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.image.*;
  3. import javax.swing.*;
  4. /**
  5.  * OFImage is a class that defines an image in OF (Objects First) format.
  6.  *
  7.  * @author Muhammad Naufaldillah
  8.  * @version 5 December 2020
  9.  */
  10. public class OFImage extends BufferedImage
  11. {
  12.     /**
  13.      * Create an OFImage copied from a BufferedImage.
  14.      * @param image The image to copy.
  15.      */
  16.     public OFImage(BufferedImage image)
  17.     {
  18.         super(image.getColorModel(), image.copyData(null),
  19.               image.isAlphaPremultiplied(), null);
  20.     }
  21.    
  22.     /**
  23.      * Create an OFImage with specified size and unspecified content.
  24.      * @param width The width of the image.
  25.      * @param height The height of the image.
  26.      */
  27.     public OFImage(int width, int height)
  28.     {
  29.         super(width, height, TYPE_INT_RGB);
  30.     }
  31.    
  32.     /**
  33.      * Set a given pixel of this image to a specified color. The
  34.      * color is represented as an (r,g,b) value.
  35.      * @param x The x position of the pixel.
  36.      * @param y The y position of the pixel.
  37.      * @param col The color of the pixel.
  38.      */
  39.     public void setPixel(int x, int y, Color col)
  40.     {
  41.         int pixel = col.getRGB();
  42.         setRGB(x, y, pixel);
  43.     }
  44.    
  45.     /**
  46.      * Set a given pixel of this image to a specified color. The
  47.      * color is represented as an (r,g,b) value.
  48.      * @param x The x position of the pixel.
  49.      * @param y The y position of the pixel.
  50.      * @param col The color of the pixel.
  51.      */
  52.     public Color getPixel(int x, int y)
  53.     {
  54.         int pixel = getRGB(x, y);
  55.         return new Color(pixel);
  56.     }
  57.    
  58.     /**
  59.      * Make this image a bit darker
  60.      */
  61.     public void darker()
  62.     {
  63.         int height = getHeight();
  64.         int width = getWidth();
  65.         for(int y = 0; y < height; y++)
  66.         {
  67.             for(int x = 0; x < width; x++)
  68.             {
  69.                 setPixel(x, y, getPixel(x, y).darker());
  70.             }
  71.         }
  72.     }
  73.    
  74.     /**
  75.      * Make this image a bit lighter
  76.      */
  77.     public void lighter()
  78.     {
  79.         int height = getHeight();
  80.         int width = getWidth();
  81.         for(int y = 0; y < height; y++)
  82.         {
  83.             for(int x = 0; x < width; x++)
  84.             {
  85.                 setPixel(x, y, getPixel(x, y).brighter());
  86.             }
  87.         }
  88.     }
  89.    
  90.     /**
  91.      * Perform a three level threshold operation.
  92.      * That is: repaint the image with only three color values:
  93.      * white, gray, and black.
  94.      */
  95.     public void threshold()
  96.     {
  97.         int height = getHeight();
  98.         int width = getWidth();
  99.         for(int y = 0; y < height; y++)
  100.         {
  101.             for(int x = 0; x < width; x++)
  102.             {
  103.                 Color pixel = getPixel(x, y);
  104.                 int brightness = (pixel.getRed() + pixel.getBlue() + pixel.getGreen());
  105.                
  106.                 if(brightness <= 85)
  107.                 {
  108.                     setPixel(x, y, Color.BLACK);
  109.                 }
  110.                 else if(brightness <= 85)
  111.                 {
  112.                     setPixel(x, y, Color.GRAY);
  113.                 }
  114.                 else
  115.                 {
  116.                     setPixel(x, y, Color.WHITE);
  117.                 }
  118.             }
  119.         }
  120.     }
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement