1 文本操作
1.1 渲染文本
public void loadTxt(String color, String fontName, String text, int fontSize,int x, int y, Graphics2D g)throws IOException{
Font font = new Font(fontName, Font.PLAIN, fontSize);
g.setFont(font);
g.setPaint(Color.decode(color));
g.drawString(text, x, y);
}
1.2 获取文本渲染之后的长度
private int loadTxt(String text,Graphics2D g){
return g.getFontMetrics().stringWidth(text);
}
2 图片操作
2.1 渲染图片
public void loadImg(String url, int width, int height, int x, int y, Graphics2D g) throws IOException {
BufferedImage erweima = ImageIO.read(new URL(url));
g.drawImage(erweima, x, y, width, height, null);
}
2.2 图片处理为圆角
public BufferedImage setClip(BufferedImage img,int radius){
int width = img.getWidth();
int height = img.getHeight();
BufferedImage res = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
Graphics2D gs = res.createGraphics();
gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gs.fillRoundRect(0, 0,width, height, radius, radius);
gs.setComposite(AlphaComposite.SrcIn);
gs.drawImage(img,0,0,null);
gs.dispose();
return res;
}