首页>代码>java swing绘制软键盘>/KeyboardTest/KeyboardTest/ImageTool.java

import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.RenderingHints.Key;

public class ImageTool {

	// 设置Graphics2D抗锯齿,具体请查看RenderingHints类的API  
	public static void setAntiAliasing(Graphics2D g2d) {  
		setRenderingHint(g2d, RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
	}  

	public static void setRenderingHint(Graphics2D g2d, Key key, Object value) {  
		if (g2d.getRenderingHints() == null) {  
			g2d.setRenderingHints(new RenderingHints(key, value));  
		} else {  
			g2d.setRenderingHint(key, value);  
		}  
	}  

	// 绘制圆角  
	public static void drawRoundRect(Graphics2D g2d, int width, int height, int r, Paint anglePaint, Paint[] borderPaints) {  
		clearAngle(g2d, anglePaint, width, height, r);// 清除角落  
		drawMultiBorder(g2d, width, height, r, anglePaint, borderPaints);// 画边框  
	}  

	// 清除角落  
	public static void clearAngle(Graphics2D g2d, Paint anglePaint, int width, int height, int r) {  
		setAntiAliasing(g2d);  
		Composite oldComposite = g2d.getComposite();  

		if (anglePaint == null) {  
			g2d.setComposite(AlphaComposite.Clear);// 设置Composite为清空  
		} else {  
			g2d.setPaint(anglePaint);  
		}  

		int npoints = 5;// 5点定位一个角落轨迹  
		// 左上角  
		int[] xpoints1 = { r, 0, 0, r / 4, r / 2 };  
		int[] ypoints1 = { 0, 0, r, r / 2, r / 4 };  
		Polygon polygon = new Polygon(xpoints1, ypoints1, npoints);  
		g2d.fillPolygon(polygon);  
		// 右上角  
		int[] xpoints2 = { width - r, width, width, width - r / 4, width - (r / 2) };  
		int[] ypoints2 = ypoints1;  
		polygon = new Polygon(xpoints2, ypoints2, npoints);  
		g2d.fillPolygon(polygon);  
		// 右下角  
		int[] xpoints3 = xpoints2;  
		int[] ypoints3 = { height, height, height - r, height - (r / 2), height - r / 4 };  
		polygon = new Polygon(xpoints3, ypoints3, npoints);  
		g2d.fillPolygon(polygon);  
		// 左下角  
		int[] xpoints4 = xpoints1;  
		int[] ypoints4 = ypoints3;  
		polygon = new Polygon(xpoints4, ypoints4, npoints);  
		g2d.fillPolygon(polygon);  
		// 还原Composite  
		g2d.setComposite(oldComposite);  
	}  

	// 绘制有层次感的边框  
	public static void drawMultiBorder(Graphics2D g2d, int width, int height, int r, Paint anglePaint, Paint[] borderPaints) {  
		setAntiAliasing(g2d);  

		int roundx = r * 2;  
		int roundy = roundx;  
		int grow = 2;  
		int x = 0;  
		int y = 0;  
		int w = width;  
		int h = height;  

		// 从外层往内层开始画  
		for (int i = 0; i < borderPaints.length; i++, x++, y++, w -= grow, h -= grow) {  
			g2d.setPaint(borderPaints[i]);  
			if (r > 0) {  
				g2d.drawRoundRect(x, y, w - 1, h - 1, roundx, roundy);  
			} else {  
				g2d.drawRect(x, y, w - 1, h - 1);  
			}  
		}  
	}  
}
最近下载更多
杜崴  LV1 2021年9月18日
叨叨喜欢刨坑  LV4 2021年7月25日
186啊哈  LV2 2021年6月4日
liuliuqiu  LV1 2020年12月18日
Standingflower  LV1 2020年3月23日
deanw7  LV1 2019年12月5日
ssh0  LV11 2019年12月5日
756398953  LV12 2019年7月3日
yyPander  LV2 2019年4月3日
why2560259286  LV1 2019年1月3日
最近浏览更多
lshlsh 2023年12月25日
暂无贡献等级
fesfefe  LV13 2023年11月11日
微信网友_5992582549164032  LV6 2022年6月21日
陈小灏  LV14 2022年5月30日
暂无贡献等级
孙纪龙啊  LV10 2021年12月7日
terryang  LV19 2021年7月26日
叨叨喜欢刨坑  LV4 2021年7月25日
风过无痕  LV1 2021年7月4日
186啊哈  LV2 2021年6月4日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友