派生自 projectDept/qhighschool

EricsHu
2023-11-23 bef4f6d51bff03c0512a75f43e79699b7296f1fa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.qxueyou.scc.org.util;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
 
 
/**
 * 课程超市工具类
 * @author xiadehu
 *
 */
public class CourseMarketUtils {
 
    /**
     * 
     * @param text
     * @param bgColor
     * @return
     */
    public static BufferedImage drawPng(char letter,Color bgColor){
        
        //初始化画布
        int width=120;
        int height = 120;
        BufferedImage image = new BufferedImage(width, height,     BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = image.createGraphics();
        image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
        g2d.dispose();
        g2d = image.createGraphics();
         
        //背景
        g2d.setColor(bgColor);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        double centerX = width/2;
        double centerY = height/2;
        double radius = width/2;
        Ellipse2D circle = new Ellipse2D.Double();
        circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY+ radius);
        g2d.fill(circle);
        
        //文字
        g2d.setColor(new Color(255,255,255));
        int textSize = 64;
        
        Font font = new Font("微软雅黑", Font.PLAIN, textSize);
        g2d.setFont(font);
        FontMetrics metrics = g2d.getFontMetrics();
        
        float x = (width - metrics.charWidth(letter)) / 2.0f;
        float y = (height - metrics.getHeight()) / 2.0f + metrics.getAscent();
        
        g2d.drawString(String.valueOf(letter), x, y);
        
        //释放
        g2d.dispose();
        
        return image;
    }
    
    public static void main(String[] args){
        System.out.println((String)null);
    }
}