最近做一个验证码生成程序rotate旋转后面的字符总是无法定位坐标导致生成后有的字符会写的图片外面,求大神解答。
代码:
for (int i = 0; i < codeCount; i++) {
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
int wordType = random.nextInt(4);
char retWord = 0;
double theta = 0.0;
switch (wordType) {
case 0:
retWord = this.getSingleNumberChar();
g.setFont(new Font("宋体", Font.PLAIN + Font.ITALIC, fontHeight-random.nextInt(fontHeight/10)));
theta = getRandomDegree();
g.rotate(theta,(i) * x,codeY);
System.out.println(theta);
break;
case 1:
retWord = this.getLowerOrUpperChar(0);
g.setFont(new Font("宋体", Font.PLAIN + Font.ITALIC, fontHeight));
theta = getRandomDegree();
g.rotate(theta,(i) * x,codeY);
System.out.println(theta);
break;
case 2:
retWord = this.getLowerOrUpperChar(1);
g.setFont(new Font("宋体", Font.PLAIN + Font.ITALIC, fontHeight-random.nextInt(fontHeight/6)));
theta = getRandomDegree();
g.rotate(theta,(i) * x,codeY);
System.out.println(theta);
break;
case 3:
retWord = this.getRandomSinoChar();
g.setFont(new Font("宋体", Font.PLAIN + Font.ITALIC, fontHeight-random.nextInt(fontHeight/4)));
theta = getRandomDegree();
g.rotate(theta,(i) * x,codeY);
System.out.println(theta);
break;
}
sRand += String.valueOf(retWord);
g.setColor(new Color(red, green, blue));
//g.drawString(String.valueOf(retWord), (i) * x, codeY);
g.drawString(retWord+"", (i) * x, codeY);
//g.translate(0, 0);
}
在drawString和rotate方法中写坐标都无法解决。
可以先正向旋转,写完以后,再反向转回来