Java/2D Graphics GUI/FilteredImageSource

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

Use FilteredImageSource

 
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.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageProducer;
import javax.swing.JApplet;
public class FilterLab extends JApplet implements ActionListener {
  private Image originalImage, filteredImage;
  private Button btn = new Button("FILTER");
  private CropImageFilter cropFilter = new CropImageFilter(50, 20, 120, 140);
  public void init() {
    originalImage = getImage(getDocumentBase(), "e.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);
  }
}