Graphics2D.drawString换行示例

java 生成图片使用 g.drawString 时,如果文本太长,超出的部分会丢失

drawString 没法自动换行

解决方案示例Demo如下 : 

  1 import java.awt.Color;
  2 import java.awt.Font;
  3 import java.awt.Graphics2D;
  4 import java.awt.font.FontRenderContext;
  5 import java.awt.geom.Rectangle2D;
  6 import java.awt.image.BufferedImage;
  7 import java.io.FileOutputStream;
  8 import java.io.IOException;
  9 import java.util.ArrayList;
 10 import java.util.List;
 11 
 12 import com.sun.image.codec.jpeg.JPEGCodec;
 13 import com.sun.image.codec.jpeg.JPEGImageEncoder;
 14 
 15 
 16 public class DrawStringTest {
 17 
 18     /**
 19      * @param args
 20      */
 21     public static void main(String[] args) {
 22         BufferedImage bufferImage = null;
 23         FileOutputStream output = null ;
 24         
 25         String info = "客户姓名:唐伯虎;身份证号:12345678906666666;某某合同编号:张店哈哈哈哈高抵字2015年第02-30在参数StringFormat标志位 !";
 26         int w = 500 ;//画布宽度
 27         int h = 300 ;//画布高度
 28         try {
 29             bufferImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB) ;
 30             Graphics2D g = bufferImage.createGraphics() ;
 31             Font font = new Font("微软雅黑",Font.PLAIN,30) ;
 32             List<String> line_list = new ArrayList<String>();
 33 
 34             FontRenderContext frc = g.getFontRenderContext();
 35             g.getFontRenderContext();
 36             Rectangle2D stringBounds = font.getStringBounds(info, frc);
 37             double fontWidth = stringBounds.getWidth();
 38             System.out.println("不换行时文本宽度 : "+fontWidth);
 39             if(fontWidth <= w){
 40                 line_list.add(info);
 41             }else{
 42                 int text_width = w ;//输出文本宽度,这里就以画布宽度作为文本宽度测试
 43                 double bs =  fontWidth/text_width ;//文本长度是文本框长度的倍数
 44                 int line_char_count = (int)Math.ceil(info.length() / bs) ;//每行大概字数
 45                 int begin_index = 0 ;
 46                 while(begin_index < info.length()){
 47                     int end_index = begin_index + line_char_count ;
 48                     if(end_index >= info.length()){
 49                         end_index = info.length();
 50                     }
 51                     String line_str = info.substring(begin_index, end_index);
 52                     Rectangle2D tempStringBounds = font.getStringBounds(line_str, frc);
 53                     int tzcs = 0 ;//调整次数
 54                     int tzzs = 1 ;//调整字数,临时文本的字符串长度大于要求的文本宽度时,每次减少临时文本的字数,然后重新测量文本宽度
 55                     while(tempStringBounds.getWidth() > text_width){
 56                         line_str = line_str.substring(0, line_str.length() - tzzs );//每次向前 tzzs 个字符重新计算(待优化)
 57                         tempStringBounds = font.getStringBounds(line_str, frc);
 58                         tzcs++;
 59                     }
 60                     System.out.println("调整"+(end_index - begin_index -line_str.length())+"个数字,调整了"+tzcs+"次,当前行宽度:"+tempStringBounds.getWidth());
 61                     line_list.add(line_str);
 62                     begin_index = begin_index + line_str.length()  ;
 63                 }
 64             }
 65    
 66             g.setColor(Color.WHITE);
 67             g.fillRect(0,0,w,h);//填充整个屏幕  
 68             g.setFont(font);
 69             g.setColor(Color.RED) ;
 70             
 71             for(int i = 0;i<line_list.size();i++){
 72                 String line_str = line_list.get(i);
 73                 g.drawString(line_str, 0, (i+2)*35) ;//35为每行的高度
 74             }
 75             
 76             g.drawString("Graphics2D.drawString换行示例", 0, 35) ;
 77             g.dispose() ;
 78             
 79             output = new FileOutputStream ("E:/1.jpg");//输入文件到E盘根目录
 80             System.out.println("文件 E:/1.jpg 已经生成");
 81             JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(output) ;
 82             en.encode(bufferImage) ;
 83             
 84             output.flush() ;
 85         } catch (Exception e) {
 86             e.printStackTrace();
 87         } finally{
 88             if(output!=null) {
 89                 try {
 90                     output.close();
 91                 } catch (IOException e) {
 92                     e.printStackTrace();
 93                 }
 94             }
 95             System.out.println("end/:-)");
 96         }
 97 
 98     }
 99 
100 }
View Code

通过计算文本宽度实现自动换行

原文地址:https://www.cnblogs.com/hi-gdl/p/10445566.html