Graphics2D类扩展于 Graphics 类,Graphics2D是在Java平台上渲染二维形状,文本和图像的基本类。无需jar包这个是jdk自带的;
public void Graphics2DDetails() throws IOException {
BufferedImage canvas = new BufferedImage(400, 661, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = (Graphics2D) canvas.getGraphics();
g.setBackground(Color.gray);
g.clearRect(0, 0, 400, 661);
BufferedImage backgroundImg = ImageIO.read(new URL(LOGOs));
g.drawImage(backgroundImg.getScaledInstance(400, 661, Image.SCALE_DEFAULT), 0, 0, null);
BufferedImage bufferedImageLogo = ImageIO.read(new URL(LOGO));
g.drawImage(bufferedImageLogo.getScaledInstance(80, 50, Image.SCALE_DEFAULT), 10, 10, null);
g.setColor(Color.WHITE);
g.setFont(new Font("宋体", Font.BOLD, 13));
g.drawString("张三", 100, 450);
Integer num = TEXT.length() % 7 == 0 ? TEXT.length() / 7 : TEXT.length() / 7 + 1;
Integer width = 350;
Integer height = 110;
for (int j = 1; j <= num; j++) {
height = 110;
String str = "";
if (j == num) {
str = TEXT.substring((j - 1) * 7, TEXT.length());
} else {
str = TEXT.substring((j - 1) * 7, j * 7);
}
for (int i = 0; i < str.length(); i++) {
g.setColor(Color.WHITE);
g.setFont(new Font("宋体", Font.BOLD, 20));
String val = String.valueOf(str.charAt(i));
g.drawString(val, width, height);
height += 20;
}
width -= 20;
}
Rectangle2D rect = new Rectangle2D.Double(20,30,80,40);
RoundRectangle2D rectRound = new RoundRectangle2D.Double(20,30,130,100,18,15);
Ellipse2D ellipse = new Ellipse2D.Double(20,30,100,50);
g.setClip(ellipse);
BufferedImage logo = ImageIO.read(new URL(LOGO));
g.drawImage(logo.getScaledInstance(100, 50, Image.SCALE_DEFAULT), 20, 30, null);
g.dispose();
File file = new File("d://9.png");
ImageIO.write(canvas, "png", file);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(canvas, "png", os);
InputStream inputStream = new ByteArrayInputStream(os.toByteArray());
}