Advertisement
reyhanzo

Untitled

Dec 15th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 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 Michael Kolling and David J. Barnes  
  8.   * @version 1.1  
  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.     * Create an OFImage with specified size and unspecified content.  
  23.     * @param width The width of the image.  
  24.     * @param height The height of the image.  
  25.     */  
  26.    public OFImage(int width, int height)  
  27.    {  
  28.      super(width, height, TYPE_INT_RGB);  
  29.    }  
  30.    /**  
  31.     * Set a given pixel of this image to a specified color. The  
  32.     * color is represented as an (r,g,b) value.  
  33.     * @param x The x position of the pixel.  
  34.     * @param y The y position of the pixel.  
  35.     * @param col The color of the pixel.  
  36.     */  
  37.    public void setPixel(int x, int y, Color col)  
  38.    {  
  39.      int pixel = col.getRGB();  
  40.      setRGB(x, y, pixel);  
  41.    }  
  42.    /**  
  43.     * Get the color value at a specified pixel position.  
  44.     * @param x The x position of the pixel.  
  45.     * @param y The y position of the pixel.  
  46.     * @return The color of the pixel at the given position.  
  47.     */  
  48.    public Color getPixel(int x, int y)  
  49.    {  
  50.      int pixel = getRGB(x, y);  
  51.      return new Color(pixel);  
  52.    }  
  53.    /**  
  54.     * Make this image a bit darker.  
  55.     */  
  56.    public void darker()  
  57.    {  
  58.      int height = getHeight();  
  59.      int width = getWidth();  
  60.      for(int y = 0; y < height; y++) {  
  61.        for(int x = 0; x < width; x++) {  
  62.          setPixel(x, y, getPixel(x, y).darker());  
  63.        }  
  64.      }  
  65.    }  
  66.    /**  
  67.     * Make this image a bit lighter.  
  68.     */  
  69.    public void lighter()  
  70.    {  
  71.      int height = getHeight();  
  72.      int width = getWidth();  
  73.      for(int y = 0; y < height; y++) {  
  74.        for(int x = 0; x < width; x++) {  
  75.          setPixel(x, y, getPixel(x, y).brighter());  
  76.        }  
  77.      }  
  78.    }  
  79.    /**  
  80.     * Perform a three level threshold operation.  
  81.     * That is: repaint the image with only three color values:  
  82.     *     white, gray, and black.  
  83.     */  
  84.    public void threshold()  
  85.    {  
  86.      int height = getHeight();  
  87.      int width = getWidth();  
  88.      for(int y = 0; y < height; y++) {  
  89.        for(int x = 0; x < width; x++) {  
  90.          Color pixel = getPixel(x, y);  
  91.          int brightness = (pixel.getRed() + pixel.getBlue() + pixel.getGreen()) / 3;  
  92.          if(brightness <= 85) {  
  93.            setPixel(x, y, Color.BLACK);  
  94.          }  
  95.          else if(brightness <= 170) {  
  96.            setPixel(x, y, Color.GRAY);  
  97.          }  
  98.          else {  
  99.            setPixel(x, y, Color.WHITE);  
  100.          }  
  101.        }  
  102.      }  
  103.    }  
  104.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement