前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java graphics2d 透明_Java Graphics2D绘制背景透明的图形过程

java graphics2d 透明_Java Graphics2D绘制背景透明的图形过程

作者头像
Java架构师必看
发布2022-04-15 20:13:11
2.7K0
发布2022-04-15 20:13:11
举报
文章被收录于专栏:Java架构师必看

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 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();

}

}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-04-132,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档