import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class DrawTransparentPic {
* 纯绘制图形,其背景是黑色的
* @param args
* @throws IOException
public void drawImage() throws IOException{
int width=256;
int height=256;
//创建BufferedImage对象
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
// 获取Graphics2D
Graphics2D g2d = bi.createGraphics();
// 画图BasicStroke是JDK中提供的一个基本的画笔类,我们对他设置画笔的粗细,就可以在drawPanel上任意画出自己想要的图形了。
g2d.setColor(new Color(255, 0, 0));
g2d.setStroke(new BasicStroke(1f));
g2d.fillRect(128, 128, width, height);
// 释放对象
g2d.dispose();
// 保存文件
ImageIO.write(bi, "png", new File("H:/test.png"));
* 绘制图形,把自己绘制的图形设置为透明或半透明,背景并不透明 前景透明,背景依然是黑色
* @param args
* @throws IOException
public static void drawImage1() throws IOException{
int width=256;
int height=256;
//创建BufferedImage对象
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 获取Graphics2D
Graphics2D g2d = bi.createGraphics();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));// 1.0f为透明度 ,值从0-1.0,依次变得不透明
// 画图BasicStroke是JDK中提供的一个基本的画笔类,我们对他设置画笔的粗细,就可以在drawPanel上任意画出自己想要的图形了。
g2d.setColor(new Color(255, 0, 0));
g2d.setStroke(new BasicStroke(1f));
g2d.fillRect(128, 128, width, height);
// 释放对象 透明度设置结束
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g2d.dispose();
// 保存文件
ImageIO.write(bi, "png", new File("H:/test.png"));
* 绘制透明图形
* @param args
* @throws IOException
public static void drawTransparent() throws IOException{
int width=256;
int height=256;
//创建BufferedImage对象
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 获取Graphics2D
Graphics2D g2d = image.createGraphics();
// 增加下面代码使得背景透明
image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
g2d.dispose();
g2d = image.createGraphics();
// 背景透明代码结束
// 画图BasicStroke是JDK中提供的一个基本的画笔类,我们对他设置画笔的粗细,就可以在drawPanel上任意画出自己想要的图形了。
g2d.setColor(new Color(255, 0, 0));
g2d.setStroke(new BasicStroke(1f));
g2d.fillRect(128, 128, width, height);
// 释放对象
g2d.dispose();
// 保存文件
ImageIO.write(image, "png", new File("H:/test.png"));
public static void main(String[] args) throws IOException {
// drawTransparent();
drawImage1();
File f = new File("D:\tag\20141204\chengxu\business-dossier\business-dossier-web\src\main\webapp\upload\2017-08-07\C7A23630C2700001FCD951071A601214.ico");
ICOFile ico = new ICOFile(f.toURL()
近用到
Java
动态生成
背景
透明
的图片功能,从gif和png中选择了png格式,个中缘由就不说了,于是动手到网上搜索有用的代码。现把搜索结果总结如下:1.生成png图片intwidth=400;intheight=300;//创建BufferedImage对象BufferedImageimage=newBufferedImage(width,height,Buff...
java
使用BufferedImage和
Graphics2D
制作
背景
透明
的水印
做小程序的同事让给做个水印图片的接口,以前没做过,百度了下,还挺多,不过做出来的要么图片不
透明
,要么水印
透明
度没法控制。改了半天终于做好了,下面上代码:
public String makewatermark(String mark){
Font font=new Font("微软雅黑", Font.PLAIN, 22);
Integer width=300;
Intege
小编典典您使用的规则错误-请勿使用AlphaComposite.CLEAR。AlphaComposite API声明有关CLEAR:目的地的颜色和Alpha均被清除(Porter-Duff清除规则)。源和目的地均不用作输入。因此,这将使图像消失。尝试其他规则。在创建SSCCE时,我创建了我的。查看当您注释掉另一条规则线时会发生什么。例如更改此// int rule = AlphaComposite...
package com.goldgrid.socket.client;
import
java
.awt.
Graphics2D
;import
java
.awt.Image;import
java
.awt.image.BufferedImage;import
java
.io.ByteArrayOutputStream;import
java
.io.File;
import
java
x.imagei...
graphics2d
.setBackground(Color.WHITE);
graphics2d
.clearRect(0, 0, width, height);
graphics2d
.setPaint(new Color(backgr...
package com.jhy.time;import
java
.awt.AlphaComposite;import
java
.awt.BasicStroke;import
java
.awt.Color;import
java
.awt.
Graphics2D
;import
java
.awt.Transparency;import
java
.awt.image.BufferedImage;import...