📄 processorapp.java
字号:
/********************************************************************************
* This code includes sample image filters built in Java and a demo applet using
* some of their features.
* Please note: I also have advanced filters such as blur, sharpen, diffuse, etc.
* but due to time constraints, I could not include them here. If you are
* interested in those or other filters, please contact me at kels@wwa.com
*
* ALL CONTENTS COPYRIGHT 1996 KELLY DEMPSKI (kels@wwa.com). ALL RIGHTS RESERVED
********************************************************************************/
import java.awt.*;
import java.lang.*;
import java.awt.image.*;
/* this filter just copies images from one to another - not really a filter */
class CopyFilter extends RGBImageFilter
{
public CopyFilter(){}
public int filterRGB(int x, int y, int rgb)
{return rgb;}
}
/* This filter adjusts the brightnesses of one or all of the color channels, depending on the
values specified in the constructor */
class AdjustBrightnessFilter extends RGBImageFilter
{
private int ChangeRed, ChangeGreen, ChangeBlue;
public AdjustBrightnessFilter(int RedDelta, int GreenDelta, int BlueDelta)
{
ChangeRed = RedDelta;
ChangeGreen = GreenDelta;
ChangeBlue = BlueDelta;
}
public int filterRGB(int x, int y, int rgb)
{
Color CurrentColor = new Color(rgb);
Color FinalColor = new Color(Math.min(255, Math.max(0, CurrentColor.getRed() + ChangeRed)),
Math.min(255, Math.max(0, CurrentColor.getGreen() + ChangeGreen)),
Math.min(255, Math.max(0, CurrentColor.getBlue() + ChangeBlue)));
return FinalColor.getRGB();
}
}
/* This filter adjusts the contrast of one or all of the color channels, depending on the
values specified in the constructor */
class AdjustContrastFilter extends RGBImageFilter
{
private int ChangeRed, ChangeGreen, ChangeBlue;
public AdjustContrastFilter(int RedDelta, int GreenDelta, int BlueDelta)
{
ChangeRed = RedDelta;
ChangeGreen = GreenDelta;
ChangeBlue = BlueDelta;
}
public int filterRGB(int x, int y, int rgb)
{
Color CurrentColor = new Color(rgb);
int Red = CurrentColor.getRed();
int Green = CurrentColor.getGreen();
int Blue = CurrentColor.getBlue();
if ((Red > 128) && (ChangeRed > 0)) { Red = Math.min(Red + ChangeRed, 255);}
if ((Red > 128) && (ChangeRed < 0)) { Red = Math.max(Red + ChangeRed, 128);}
if ((Red < 128) && (ChangeRed > 0)) { Red = Math.max(Red - ChangeRed, 0);}
if ((Red < 128) && (ChangeRed < 0)) { Red = Math.min(Red - ChangeRed, 128);}
if ((Green > 128) && (ChangeGreen > 0)) { Green = Math.min(Green + ChangeGreen, 255);}
if ((Green > 128) && (ChangeGreen < 0)) { Green = Math.max(Green + ChangeGreen, 128);}
if ((Green < 128) && (ChangeGreen > 0)) { Green = Math.max(Green - ChangeGreen, 0);}
if ((Green < 128) && (ChangeGreen < 0)) { Green = Math.min(Green - ChangeGreen, 128);}
if ((Blue > 128) && (ChangeBlue > 0)) { Blue = Math.min(Blue + ChangeBlue, 255);}
if ((Blue > 128) && (ChangeBlue < 0)) { Blue = Math.max(Blue + ChangeBlue, 128);}
if ((Blue < 128) && (ChangeBlue > 0)) { Blue = Math.max(Blue - ChangeBlue, 0);}
if ((Blue < 128) && (ChangeBlue < 0)) { Blue = Math.min(Blue - ChangeBlue, 128);}
Color FinalColor = new Color(Red, Green, Blue);
return FinalColor.getRGB();
}
}
/* This filter changes the image to grayscale */
class GrayscaleFilter extends RGBImageFilter
{
public GrayscaleFilter(){}
public int filterRGB(int x, int y, int rgb)
{
Color CurrentColor = new Color(rgb);
int ColorAverage = (CurrentColor.getRed() + CurrentColor.getGreen() + CurrentColor.getBlue())/3;
Color FinalColor = new Color(ColorAverage, ColorAverage, ColorAverage);
return FinalColor.getRGB();
}
}
/* This filter creates the interesting effect caused by reexposing film midway in the developing process*/
class SolarizeFilter extends RGBImageFilter
{
public SolarizeFilter(){}
public int filterRGB(int x, int y, int rgb)
{
Color CurrentColor = new Color(rgb);
int Red = CurrentColor.getRed();
int Green = CurrentColor.getGreen();
int Blue = CurrentColor.getBlue();
if (Red > 128) {Red = 255 - Red;}
if (Green > 128) {Green = 255 - Green;}
if (Blue > 128) {Blue = 255 - Blue;}
Color FinalColor = new Color(Red, Green, Blue);
return FinalColor.getRGB();
}
}
/* This filter adjusts the colors of the image in HSB color space*/
class AdjustHSBFilter extends RGBImageFilter
{
private float HChange, SChange, BChange;
public AdjustHSBFilter(float HDelta, float SDelta, float BDelta)
{
HChange = HDelta;
SChange = SDelta;
BChange = BDelta;
}
public int filterRGB(int x, int y, int rgb)
{
Color CurrentColor = new Color(rgb);
float HSBVals[] = new float[3];
Color.RGBtoHSB(CurrentColor.getRed(), CurrentColor.getGreen(), CurrentColor.getBlue(), HSBVals);
HSBVals[0] = HSBVals[0] + HChange;
HSBVals[1] = Math.min(HSBVals[1] + SChange, 1);
HSBVals[2] = Math.min(1, Math.max(0, HSBVals[2] + BChange));
if (HSBVals[1] <= 0)
{
HSBVals[0] += .5;
HSBVals[1] = Math.abs(HSBVals[1]);
}
if (HSBVals[0] >= 1) {HSBVals[0] -= (int)HSBVals[0];}
if (HSBVals[0] <= 0) {HSBVals[0] += 1;}
return Color.HSBtoRGB(HSBVals[0], HSBVals[1], HSBVals[2]);
}
}
/* This filter creates a negative of the image*/
class NegativeFilter extends RGBImageFilter
{
public NegativeFilter(){}
public int filterRGB(int x, int y, int rgb)
{
Color CurrentColor = new Color(rgb);
int Red, Green, Blue;
Red = Math.abs(CurrentColor.getRed() - 255);
Green = Math.abs(CurrentColor.getGreen() - 255);
Blue = Math.abs(CurrentColor.getBlue() - 255);
Color FinalColor = new Color(Red, Green, Blue);
return FinalColor.getRGB();
}
}
/* the actual applet */
final public class ProcessorApp extends java.applet.Applet
{
Graphics CurrentG;
Image BaseImage;
Panel ButtonPanel;
Button BrightUpBtn, BrightDownBtn, ConUpBtn, ConDownBtn, GrayBtn, NegBtn, BlurBtn, OtherBtn;
ImageFilter CurrentImageFilter;
Image FilteredImage;
/* set everything up. notice the image name is hard coded for now */
public void init()
{
CurrentG = getGraphics();
SetUpApp();
BaseImage = getImage(getDocumentBase(), "test.gif");
CurrentImageFilter = new CopyFilter();
FilteredImage = createImage(new FilteredImageSource(BaseImage.getSource(),
CurrentImageFilter));
}
/* lay out buttons, etc. */
public void SetUpApp()
{
setLayout(new BorderLayout());
ButtonPanel = new Panel();
ButtonPanel.setLayout(new GridLayout(8,1));
BrightUpBtn = new Button("Bright +");
BrightDownBtn = new Button("Bright -");
ConUpBtn = new Button("Contrast +");
ConDownBtn = new Button("Contrast -");
GrayBtn = new Button("Gray");
NegBtn = new Button("Negative");
BlurBtn = new Button("Solarize");
OtherBtn = new Button("Hue +");
ButtonPanel.add(BrightUpBtn);
ButtonPanel.add(BrightDownBtn);
ButtonPanel.add(ConUpBtn);
ButtonPanel.add(ConDownBtn);
ButtonPanel.add(GrayBtn);
ButtonPanel.add(NegBtn);
ButtonPanel.add(BlurBtn);
ButtonPanel.add(OtherBtn);
add("West",ButtonPanel);
}
/* handle the click of a button */
public boolean handleEvent(Event evt)
{
if(evt.target instanceof Button)
{
String BtnLabel = ((Button)evt.target).getLabel();
if(BtnLabel.equals("Bright +"))
CurrentImageFilter = new AdjustBrightnessFilter(50, 50, 50);
if(BtnLabel.equals("Bright -"))
CurrentImageFilter = new AdjustBrightnessFilter(-50, -50, -50);
if(BtnLabel.equals("Contrast +"))
CurrentImageFilter = new AdjustContrastFilter(50, 50, 50);
if(BtnLabel.equals("Contrast -"))
CurrentImageFilter = new AdjustContrastFilter(50, 50, 50);
if(BtnLabel.equals("Gray"))
CurrentImageFilter = new GrayscaleFilter();
if(BtnLabel.equals("Negative"))
CurrentImageFilter = new NegativeFilter();
if(BtnLabel.equals("Solarize"))
CurrentImageFilter = new SolarizeFilter();
if(BtnLabel.equals("Hue +"))
CurrentImageFilter = new AdjustHSBFilter((float).5, (float)0, (float)0);
FilteredImage = createImage(new FilteredImageSource(BaseImage.getSource(),
CurrentImageFilter));
paint(CurrentG);
}
return true;
}
public void paint(Graphics g)
{
CurrentG.drawImage(BaseImage, 410, 0, this);
CurrentG.drawImage(FilteredImage, 100, 0, this);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -