RickyS20_

Untitled

Nov 23rd, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1.  
  2. /**
  3.  * OFImage is a class that defines an image in OF (Objects First) format.
  4.  *
  5.  * @author (Ricky S)
  6.  * @version (1.0)
  7.  */
  8. import java.awt.*;
  9. import java.awt.image.*;
  10. import javax.swing.*;
  11.  
  12. public class OFImage extends BufferedImage
  13. {
  14.     /**
  15.       * Create an OFImage copied from a BufferedImage.
  16.       * @param image The image to copy.
  17.       */
  18.     public OFImage(BufferedImage image)
  19.      {
  20.          super(image.getColorModel(), image.copyData(null),
  21.          image.isAlphaPremultiplied(), null);
  22.         }
  23.     /**
  24.       * Create an OFImage with specified size and unspecified content.
  25.       * @param width The width of the image.
  26.       * @param height The height of the image.
  27.       */
  28.     public OFImage(int width, int height)
  29.      {
  30.          super(width, height, TYPE_INT_RGB);
  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.        * Get the color value at a specified pixel position.
  47.        * @param x The x position of the pixel.
  48.        * @param y The y position of the pixel.
  49.        * @return The color of the pixel at the given position.
  50.        */
  51.       public Color getPixel(int x, int y)
  52.       {
  53.           int pixel = getRGB(x, y);
  54.           return new Color(pixel);
  55.     }
  56.     /**
  57.       * Make this image a bit darker.
  58.       */
  59.      public void darker()
  60.      {
  61.          int height = getHeight();
  62.          int width = getWidth();
  63.          for(int y = 0; y < height; y++) {
  64.              for(int x = 0; x < width; x++) {
  65.                  setPixel(x, y, getPixel(x, y).darker());
  66.                 }
  67.             }
  68.         }
  69.     /**
  70.       * Make this image a bit lighter.
  71.       */
  72.      public void lighter()
  73.      {
  74.          int height = getHeight();
  75.          int width = getWidth();
  76.          for(int y = 0; y < height; y++) {
  77.              for(int x = 0; x < width; x++) {
  78.                  setPixel(x, y, getPixel(x, y).brighter());
  79.                 }
  80.             }
  81.         }
  82.     /**
  83.       * Perform a three level threshold operation.
  84.       * That is: repaint the image with only three color values:
  85.       * white, gray, and black.
  86.       */
  87.      public void threshold()
  88.      {
  89.          int height = getHeight();
  90.          int width = getWidth();
  91.          for(int y = 0; y < height; y++) {
  92.              for(int x = 0; x < width; x++) {
  93.                  Color pixel = getPixel(x, y);
  94.                  int brightness = (pixel.getRed() + pixel.getBlue() + pixel.getGreen())/ 3;
  95.                  if(brightness <= 85) {
  96.                      setPixel(x, y, Color.BLACK);
  97.                     }
  98.                     else if(brightness <= 170) {
  99.                         setPixel(x, y, Color.GRAY);
  100.                     }
  101.                     else {
  102.                         setPixel(x, y, Color.WHITE);
  103.                     }
  104.                 }
  105.             }
  106.         }
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment