Mryqu's Notes


  • 首页

  • 搜索
close

[Swing]图片叠加效果

时间: 2014-12-25   |   分类: Java     |   阅读: 278 字 ~2分钟

玩一下使用Java做图片叠加效果。 Swing:图片叠加效果 代码如下:

package com.yqu.swing.img;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.GrayFilter;
import javax.swing.Icon;
import javax.swing.ImageIcon;

public class OverlayIcon implements Icon{
  private int maxWidth = -1, maxHeight = -1;
  private List icons;
  
  public OverlayIcon(String[] iconPaths) {
    if(iconPaths != null && iconPaths.length >0) {
      icons = new ArrayList(iconPaths.length);
      for (String iconPath : iconPaths) {
        Icon icon = makeIcon(iconPath, false);
        icons.add(icon);
        int width = icon.getIconWidth();
        int height = icon.getIconHeight();
        if (width > maxWidth)
          maxWidth = width;
        if (height > maxHeight)
          maxHeight = height;
      }
      if (maxWidth == -1)
        maxWidth = 16;
      if (maxHeight == -1)
        maxHeight = 16;
    }
  }
  
  private Icon makeIcon(String iconPath, boolean makeDisabled) {
    ImageIcon icon = new ImageIcon(iconPath);
    if (makeDisabled) {
      icon = new ImageIcon(
          GrayFilter.createDisabledImage(icon.getImage()));
    }
    return icon;
  }
  
  private BufferedImage getBufferedImage() {
    BufferedImage bi = new BufferedImage(maxWidth, maxHeight,
        BufferedImage.TYPE_INT_ARGB);
    for (Icon icon : icons) {
      icon.paintIcon(null, bi.getGraphics(), 0, 0);
    }
    return bi;
  }
    
  public Icon getIcon() {
    if(icons != null) {
      BufferedImage bi = getBufferedImage();
      return new ImageIcon(bi);
    }
    return null;
  }
  
  public void saveIcon(String flName) {
    if(icons != null) {
      BufferedImage bi = getBufferedImage();
      try {
        ImageIO.write(bi, "gif", new File(flName));
      } catch (IOException e) {
        e.printStackTrace();
      }
    } else {
      throw new IllegalStateException("Can't generate overlay icon file");
    }
  }
  
  @Override
  public void paintIcon(Component c, Graphics g, int x, int y) {
    if(icons != null) {
      for (Icon icon : icons) {
         icon.paintIcon(c, g, x, y);
      }
    }
  }

  @Override
  public int getIconWidth() {
    return maxWidth;
  }

  @Override
  public int getIconHeight() {
    return maxHeight;
  }
  
  public static void main(String[] args) {
    String[] iconPaths = {"Cycle.gif", "New_overlay.gif"};
    OverlayIcon icon = new OverlayIcon(iconPaths);
    icon.saveIcon("New_Cycle.gif");    
  }
}

标题:[Swing]图片叠加效果
作者:mryqu
声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 CN 许可协议。转载请注明出处!

#java# #swing# #overlay# #image# #图片叠加效果#
[CSS] 图片叠加效果
[OpenUI5] 打开web应用调试模式的方法
  • 文章目录
  • 站点概览

Programmer & Architect

662 日志
27 分类
1472 标签
GitHub Twitter FB Page
© 2009 - 2023 Mryqu's Notes
Powered by - Hugo v0.120.4
Theme by - NexT
0%