Java/2D Graphics GUI/RGBImageFilter

Материал из Java эксперт
Перейти к: навигация, поиск

A filter class which translates moderately white pixels to green.

 
import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
public class FilterLab extends Applet implements ActionListener {
  private Image originalImage, filteredImage;
  private Button btn = new Button("FILTER");
  private WhiteToGreenFilter cropFilter = new WhiteToGreenFilter();
  public void init() {
    originalImage = getImage(getDocumentBase(), "emily.gif");
    MediaTracker tracker = new MediaTracker(this);
    tracker.addImage(originalImage, 0);
    try {
      tracker.waitForAll();
    } catch (Exception e) {
    }
    filteredImage = originalImage;
    btn.addActionListener(this);
    add(btn);
  }
  public void actionPerformed(ActionEvent ev) {
    filter();
    repaint();
  }
  private void filter() {
    ImageProducer producer = originalImage.getSource();
    producer = new FilteredImageSource(producer, cropFilter);
    filteredImage = createImage(producer);
  }
  public void paint(Graphics g) {
    g.drawImage(originalImage, 10, 50, this);
    g.drawImage(filteredImage, 271, 50, this);
  }
}
class WhiteToGreenFilter extends RGBImageFilter {
  public WhiteToGreenFilter() {
    canFilterIndexColorModel = true;
  }
  public void setFast(boolean fast) {
    canFilterIndexColorModel = fast;
  }
  public int filterRGB(int x, int y, int rgb) {
    int red = (rgb & 0x00ff0000) >> 16;
    int green = (rgb & 0x0000ff00) >> 8;
    int blue = (rgb & 0x000000ff);
    if (blue >= 0xc0 && green >= 0xc0 && red >= 0xc0) {
      return 0xff00ff00;
    } else {
      return rgb;
    }
  }
}



A filter that translates to gray. Each of the red, green, and blue levels becomes the mean intensity.

 

import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
public class MainClass extends Applet implements ActionListener {
  private Image originalImage, filteredImage;
  private Button btn = new Button("FILTER");
  private GrayFilter cropFilter = new GrayFilter();
  public void init() {
    originalImage = getImage(getDocumentBase(), "emily.gif");
    MediaTracker tracker = new MediaTracker(this);
    tracker.addImage(originalImage, 0);
    try {
      tracker.waitForAll();
    } catch (Exception e) {
    }
    filteredImage = originalImage;
    btn.addActionListener(this);
    add(btn);
  }
  public void actionPerformed(ActionEvent ev) {
    filter();
    repaint();
  }
  private void filter() {
    ImageProducer producer = originalImage.getSource();
    producer = new FilteredImageSource(producer, cropFilter);
    filteredImage = createImage(producer);
  }
  public void paint(Graphics g) {
    g.drawImage(originalImage, 10, 50, this);
    g.drawImage(filteredImage, 271, 50, this);
  }
}
class GrayFilter extends RGBImageFilter {
  int nCalls;
  public GrayFilter() {
    canFilterIndexColorModel = true;
  }
  public void setFast(boolean fast) {
    canFilterIndexColorModel = fast;
  }
  public int filterRGB(int x, int y, int rgb) {
    int red = (rgb & 0x00ff0000) >> 16;
    int green = (rgb & 0x0000ff00) >> 8;
    int blue = (rgb & 0x000000ff);
    int mean = (red + green + blue) / 3;
    return 0xff000000 | mean << 16 | mean << 8 | mean;
  }
}