A filter that translates to gray. Each of the red, green, and blue levels becomes the mean intensity. : RGBImageFilter « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.2D Graphics GUI1
3.3D
4.Advanced Graphics
5.Ant
6.Apache Common
7.Chart
8.Class
9.Collections Data Structure
10.Data Type
11.Database SQL JDBC
12.Design Pattern
13.Development Class
14.EJB3
15.Email
16.Event
17.File Input Output
18.Game
19.Generics
20.GWT
21.Hibernate
22.I18N
23.J2EE
24.J2ME
25.JDK 6
26.JNDI LDAP
27.JPA
28.JSP
29.JSTL
30.Language Basics
31.Network Protocol
32.PDF RTF
33.Reflection
34.Regular Expressions
35.Scripting
36.Security
37.Servlets
38.Spring
39.Swing Components
40.Swing JFC
41.SWT JFace Eclipse
42.Threads
43.Tiny Application
44.Velocity
45.Web Services SOA
46.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » RGBImageFilterScreenshots 
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, 1050this);
    g.drawImage(filteredImage, 27150this);

  }
}

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 + blue3;
    return 0xff000000 | mean << 16 | mean << | mean;
  }
}

 
Related examples in the same category
1.A filter class which translates moderately white pixels to green.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.