gpt4 book ai didi

Java - 在图像中正确绘制多行字符串

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:55:06 25 4
gpt4 key购买 nike

我正在尝试用 java 编写水印程序,所以我找到了 this piece of code同样。

我为了自己的目的做了自己的改编(完整 src : https://gist.github.com/1373080/ )

 /**
*
* This method converts a multiline string into an ArrayList of Strings
* Each item on the array list is a line of the string.
* @param str A multiline string
* @return An ArrayList of strings , a string per line of text
* @see java.util.ArrayList
*/
private static ArrayList<String> convertStringToLineString(String str){
ArrayList<String> string = new ArrayList<String>();
String s = new String ();
char [] ca = str.toCharArray();

for(int i=0;i<ca.length;i++){
if(ca[i] == '\n'){
string.add(s);
s = new String();
}
s += ca[i];
}
return string;
}

字符串是这样画的

    int x =(img.getIconWidth() - (int) rect.getWidth()) / 2,
y =(img.getIconHeight() - (int) rect.getHeight()) / 2;

ArrayList<String> watermarks = convertStringToLineString(watermark);
for (String w : watermarks){
g2d.drawString(w,x,y);
y+=20;
}

我面临的问题是像日历这样的字符串

 November 2011      
Su Mo Tu We Th Fr Sa
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

其中包含多条线未正确绘制。

示例 enter image description here

PS:我对使用 java 的任何类型的解决方案持开放态度,即使是那些使用其他库的解决方案。

使用下面的代码行

g2D.setFont(new Font("Monospaced", Font.PLAIN, 12));

但必须有一个更优雅的解决方案,适用于任何类型的字体。

最佳答案

我不太喜欢你拆分多行字符串的方法。试试这个:

public static List<String>  convertStringToLineString(String str) {
if (str==null)
return null;

String[] parts = str.split("\n");
return Arrays.asList(parts);
}

关于Java - 在图像中正确绘制多行字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8167807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com