如何使用RGBImageFilter进行图象颜色过滤

net_zero 2000-12-27 10:59:00
我想通过一个JAVA APPLET使网页上的图片达到变化颜色的效果
现在只能实现将彩色图象转化为黑白图象,不知道如何使彩色图象变成另一种颜色的图象
下面是我现在的JAVA APPLET,不知道如何修改以达到转化图象颜色的效果。
或者有没有更好的方法实现?

//==============================================================
// Filter.java - Converts color GIF file to gray-scale image
//==============================================================

import java.applet.*;
import java.awt.*;
import java.awt.image.*;

//==============================================================
// BWFilter (black and white filter) class
//==============================================================

class BWFilter extends RGBImageFilter {

// Constructor
public BWFilter() {
canFilterIndexColorModel = true;
}

// Return rgb color converted to shade of gray
public int filterRGB(int x, int y, int rgb) {
// Reduce rgb to hue, saturation, brightness elements
Color c = new Color(rgb);
float[] hsbvals = Color.RGBtoHSB(c.getRed(), c.getGreen(),
c.getBlue(), null);
// Return new color value of same brightness but
// with hue and saturation set to zero
return Color.HSBtoRGB(hsbvals[2], hsbvals[2], 255.0f);
}
}

//==============================================================
// Applet class
//==============================================================

public class Filter
extends Applet
implements Runnable {
// Instance variables
Image pic; // GIF image producer
int picID; // Arbitrary image ID
MediaTracker tracker; // Tracks loading of image
Thread loadingThread; // Thread for loading image
String filename = "hn094.gif"; // File name
boolean imageReady = false; // Offscreen image flag
Image bwPic; // Offscreen image object

// Initialize applet
public void init() {
// Size applet window
resize(640, 480);
// Create MediaTracker object
tracker = new MediaTracker(this);
// Start image loading
pic = getImage(getDocumentBase(), filename);
picID = 0;
tracker.addImage(pic, picID);
// Create thread to monitor image loading
loadingThread = new Thread(this);
loadingThread.start();
}

// Run loading thread
// Allows other processes to run while loading
// the image data
public void run() {
try {
tracker.waitForID(picID);
if (tracker.checkID(picID, true)) {
// Create offscreen image using loaded GIF
// file filtered by our BWFilter class
ImageProducer picSource = pic.getSource();
BWFilter bwFilter = new BWFilter();
bwPic = createImage(new
FilteredImageSource(picSource, bwFilter));
imageReady = true;
}
} catch (InterruptedException ie) {
return;
}
repaint(); // Cause paint() to draw loaded image
}

// Paint window contents
// Displays loading or error message until
// image is ready, then shows image
public void paint(Graphics g) {
if (tracker.isErrorID(picID))
g.drawString("Error loading " + filename, 10, 20);
else if (tracker.checkID(picID) && imageReady)
g.drawImage(bwPic,0, 0, this); // Show offscreen image
else
g.drawString("Loading " + filename, 10, 20);
}
}

...全文
410 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
The_east_key 2000-12-29
  • 打赏
  • 举报
回复
建议您访问www.etechbase.net/tech,里面有很多资料,也许可以解决您的问题。
访问http://168.168.18.11:81/etechbase/advsearch.php将您的问题输入查询内容框,选择不同的精确程度,即可以找到你所需要的答案。效果还是可以的。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧