下面列出了java.awt.image.RescaleOp#filter() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void runTest(int sType, int dType, int expect) {
BufferedImage src = new BufferedImage(w, h, sType);
BufferedImage dst = new BufferedImage(w, h, dType);
String msg = getMsgText(sType, dType);
Graphics2D g2d = src.createGraphics();
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, w, h);
RescaleOp res = new RescaleOp(scaleFactor, offset, null);
res.filter(src, dst);
if (saveImage) {
try {
String fname = getFileName(sType, dType);
ImageIO.write(dst, "png", new File(fname));
} catch (IOException e) {
}
}
check(dst, expect, msg);
}
@Override
protected Icon getIcon ()
{
// TODO Use that to get the state (-> highlight or not)
TransitionAwareUI transitionAwareUI = (TransitionAwareUI) slider.getUI();
StateTransitionTracker stateTransitionTracker = transitionAwareUI.getTransitionTracker();
// stateTransitionTracker.getModelStateInfo().getCurrModelState();
final Icon icon = super.getIcon();
final BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
final Graphics iconGraphics = image.createGraphics();
icon.paintIcon(slider, iconGraphics, 0, 0);
// Make it brighter (very simple approach)
final RescaleOp rescaleOp = new RescaleOp(2.0f, 50, null);
rescaleOp.filter(image, image);
ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
op.filter(image, image);
return new ImageIcon(image);
}
/**
* Prepare a version of the place holder Sprite, that is suitable for the
* transparency mode of the client.
*
* @param original original placeholder Sprite
* @return an adjusted Sprite, or the original if no adjusting is needed
*/
private Sprite preparePlaceholder(Sprite original) {
if ((original == null) || (TransparencyMode.TRANSPARENCY == Transparency.BITMASK)) {
return original;
}
/*
* Using full alpha in the client.
*
* Create a black and white, but translucent version of the same image.
* The filtering has been chosen so that the slot images we use become
* suitably B&W, not for any general rule.
*
* What we'd really want is drawing an opaque B&W image in soft light
* mode, but swing back buffer does not actually support Composites
* despite being accessed via Graphics2D.
*/
BufferedImage img = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = img.createGraphics();
original.draw(g, 0, 0);
RescaleOp rescaleOp = new RescaleOp(new float[] {3.0f, 3.0f, 3.0f, 0.5f }, new float[] {-450f, -450f, -450f, 0f}, null);
rescaleOp.filter(img, img);
g.dispose();
return new ImageSprite(img);
}
public void applyDrawDecay(float factor, boolean bRedrawPointImg){
//System.out.println("applyDrawDecay: factor="+factor);
// 1)
int v = Color.GRAY.getRed();
//System.out.println("applyDrawDecay: v="+v);
RescaleOp brightenOp = new RescaleOp(1f, (255-v)*factor, null);
// 2)
//RescaleOp brightenOp = new RescaleOp(1f + factor, 0, null);
// 3)
//RescaleOp brightenOp = new RescaleOp(1f, (255)*factor, null);
pointImg = brightenOp.filter(pointImg, null);
if (bRedrawPointImg) {
ApplyToCanvas(pointImg);
RedrawPointLayer();
}
}
@Override
public void paint(Graphics g)
{
RescaleOp ro = new RescaleOp(0.6f, 0, null);
tempImage = ro.filter(image, null);
g.drawImage(tempImage, 0, 0, this);
if (!isShown)
{
setOpacity(1);
isShown = true;
}
}
@Override
public void paint(Graphics g)
{
RescaleOp ro = new RescaleOp(0.8f, 0, null);
tempImage = ro.filter(image, null);
g.drawImage(tempImage, 0, 0, this);
}
/**
* 使用RescaleOp对图片进行滤镜处理.
*
* @param iconBottom 原图
* @param redFilter 红色通道滤镜值,1.0f表示保持不变
* @param greenFilter 绿色通道滤镜值,1.0f表示保持不变
* @param blueFilter 蓝色通道滤镜值,1.0f表示保持不变
* @param alphaFilter alpha通道滤镜值,1.0f表示保持不变
* @return 处理后的图片新对象
* @author Jack Jiang, 2013-04-05
* @since 3.5
*/
public static ImageIcon filterWithRescaleOp(ImageIcon iconBottom
, float redFilter, float greenFilter, float blueFilter, float alphaFilter)
{
try
{
int w = iconBottom.getIconWidth(), h = iconBottom.getIconHeight();
//原图
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D gg = (Graphics2D)bi.getGraphics();
gg.drawImage(iconBottom.getImage(), 0, 0,w, h,null);
//设置滤镜效果
float[] scales = { redFilter, greenFilter, blueFilter,alphaFilter };
float[] offsets = new float[4];
RescaleOp rop = new RescaleOp(scales, offsets, null);
//执行
// gg.drawImage(bi, rop, 0, 0);//用这一行代码没效果,用下一行代码即可!
rop.filter(bi, bi);
return new ImageIcon(bi);
}
catch (Exception e)
{
LogHelper.error("filterWithRescaleOp出错了,"+e.getMessage()+",iconBottom="+iconBottom);
return new ImageIcon();
}
}
private void buildRescaleOpTab(JTabbedPane tabbedPane) {
BufferedImage dstImage = null;
float[] factors = new float[] {
1.4f, 1.4f, 1.4f
};
float[] offsets = new float[] {
0.0f, 0.0f, 30.0f
};
RescaleOp op = new RescaleOp(factors, offsets, null);
dstImage = op.filter(sourceImage, null);
tabbedPane.add("Rescale", new JLabel(new ImageIcon(dstImage)));
}
public static void increaseImageBrightness(JLabel c, BufferedImage image) {
float[] factors = new float[] {
1.4f, 1.4f, 1.4f, 1.4f
};
float[] offsets = new float[] {
0.0f, 0.0f, 0.0f, 0.0f
};
RescaleOp op = new RescaleOp(factors, offsets, null);
BufferedImage brighter = op.filter(image, null);
c.setIcon(new ImageIcon(brighter));
}
private static ImageIcon makeRolloverIcon(ImageIcon srcIcon) {
int w = srcIcon.getIconWidth();
int h = srcIcon.getIconHeight();
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
srcIcon.paintIcon(null, g2, 0, 0);
float[] scaleFactors = {.5f, .5f, .5f, 1f};
float[] offsets = {0f, 0f, 0f, 0f};
RescaleOp op = new RescaleOp(scaleFactors, offsets, g2.getRenderingHints());
g2.dispose();
return new ImageIcon(op.filter(img, null));
}
protected static ImageIcon makeRolloverIcon(ImageIcon srcIcon) {
int w = srcIcon.getIconWidth();
int h = srcIcon.getIconHeight();
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
srcIcon.paintIcon(null, g2, 0, 0);
float[] scaleFactors = {1.2f, 1.2f, 1.2f, 1f};
float[] offsets = {0f, 0f, 0f, 0f};
RescaleOp op = new RescaleOp(scaleFactors, offsets, g2.getRenderingHints());
g2.dispose();
return new ImageIcon(op.filter(img, null));
}
private static ImageIcon makeRolloverIcon(ImageIcon srcIcon) {
int w = srcIcon.getIconWidth();
int h = srcIcon.getIconHeight();
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
srcIcon.paintIcon(null, g2, 0, 0);
float[] scaleFactors = {1.2f, 1.2f, 1.2f, 1f};
float[] offsets = {0f, 0f, 0f, 0f};
RescaleOp op = new RescaleOp(scaleFactors, offsets, g2.getRenderingHints());
g2.dispose();
return new ImageIcon(op.filter(img, null));
}
public void applyDrawDecay(float factor){
RescaleOp brightenOp = new RescaleOp(1f, 150f/factor, null);
pointCanvas = brightenOp.filter(pointCanvas, null);
layerPointCanvas.setImage(pointCanvas);
layerPointCanvas.repaint();
}
private void adjustAndPaintOpenedImage(Graphics2D g2d)
{
if (image == null)
{
return;
}
imageWidth = image.getWidth(null);
imageHeight = image.getHeight(null);
imageScale = imageWidth * 1.0F / imageHeight;
targetWidth = imageWidth;
targetHeight = imageHeight;
if (imageWidth >= imageHeight)
{
if (imageWidth > imageMaxWidth)
{
targetWidth = imageMaxWidth;
targetHeight = (int) (imageMaxWidth / imageScale);
}
}
else
{
if (imageHeight > imageMaxHeight)
{
targetHeight = imageMaxHeight;
targetWidth = (int) (targetHeight * imageScale);
}
}
// 缩放比例
zoomScale = targetWidth * 1.0F / imageWidth;
// 缩放后的图像
scaledImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
scaledImage.getGraphics().drawImage(image.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH), 0, 0, null);
// 使图片居中显示
imageX = 0;
imageY = 0;
if (targetWidth < imageMaxWidth)
{
imageX = (imageMaxWidth - targetWidth) / 2;
}
if (targetHeight < imageMaxHeight)
{
imageY = (imageMaxHeight - targetHeight) / 2;
}
// 添加一层灰色
RescaleOp ro = new RescaleOp(0.3f, 0, null);
tempImage = ro.filter(scaledImage, null);
g2d.drawImage(tempImage, imageX, imageY, targetWidth, targetHeight, null);
selectedWidth = targetWidth < targetHeight ? targetWidth : targetHeight;
selectedHeight = selectedWidth;
drawX = (targetWidth - selectedWidth) / 2;
drawY = (targetHeight - selectedHeight) / 2;
g2d.setColor(Colors.LIGHT_GRAY);
// 绘制选定区域矩形
g2d.drawRect(drawX + imageX - 1, drawY + imageY - 1, selectedWidth + 1, selectedHeight + 1);
selectedImage = scaledImage.getSubimage(drawX, drawY, selectedWidth, selectedHeight);
g2d.drawImage(selectedImage, imageX + drawX, imageY + drawY, null);
g2d.dispose();
}
private static void paintTabBackground (Graphics g, int index, int state,
int x, int y, int w, int h) {
if (dummyTab == null) {
dummyTab = new JTabbedPane();
}
Region region = Region.TABBED_PANE_TAB;
if( !(UIManager.getLookAndFeel() instanceof SynthLookAndFeel) ) {
return; //#215311 - unsupported L&F installed
}
SynthLookAndFeel laf = (SynthLookAndFeel) UIManager.getLookAndFeel();
SynthStyleFactory sf = laf.getStyleFactory();
SynthStyle style = sf.getStyle(dummyTab, region);
SynthContext context =
new SynthContext(dummyTab, region, style,
state == SynthConstants.FOCUSED ? SynthConstants.SELECTED : state);
SynthPainter painter = style.getPainter(context);
long t1, t2;
if (state == SynthConstants.DEFAULT) {
t1 = System.currentTimeMillis();
painter.paintTabbedPaneTabBackground(context, g, x, y, w, h, index);
t2 = System.currentTimeMillis();
if ((t2 - t1) > 200) {
LOG.log(Level.WARNING, "painter.paintTabbedPaneTabBackground1 takes too long"
+ " x=" + x + " y=" + y + " w=" + w + " h=" + h + " index:" + index
+ " Time=" + (t2 - t1));
}
} else {
BufferedImage bufIm = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufIm.createGraphics();
g2d.setBackground(UIManager.getColor("Panel.background"));
g2d.clearRect(0, 0, w, h);
t1 = System.currentTimeMillis();
painter.paintTabbedPaneTabBackground(context, g2d, 0, 0, w, h, index);
t2 = System.currentTimeMillis();
if ((t2 - t1) > 200) {
LOG.log(Level.WARNING, "painter.paintTabbedPaneTabBackground1 takes too long"
+ " x=0" + " y=0" + " w=" + w + " h=" + h + " index:" + index
+ " Time=" + (t2 - t1));
}
// differentiate active and selected tabs, active tab made brighter,
// selected tab darker
RescaleOp op = state == SynthConstants.FOCUSED
? new RescaleOp(1.08f, 0, null)
: new RescaleOp(0.96f, 0, null);
BufferedImage img = op.filter(bufIm, null);
g.drawImage(img, x, y, null);
}
}
@Override
public void paint(Graphics g) {
RescaleOp ro = new RescaleOp(0.8f, 0, null);
tempImage = ro.filter(image, null);
g.drawImage(tempImage, 0, 0, this);
}
public BufferedImage filter(BufferedImage src, BufferedImage dest) {
RescaleOp op = new RescaleOp(-1.0f, 255f, null);
return op.filter(src, dest);
}
/**
* Mutates this image by scaling all pixel values by the given factor (brightness in other words).
*/
public void rescaleInPlace(double factor) {
RescaleOp rescale = new RescaleOp((float) factor, 0f,
new RenderingHints(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY));
rescale.filter(awt(), awt());
}