我正在尝试使用随机 x,y 值fontSize根据 BufferedImage 上的词频绘制随机文本。drawString(word, x, y)不幸的是,它绘制了重叠的随机文本。图像帧大小为 1200 x 650,x、y 的随机数位于这些值之间。这是我的代码:
Random rand = new Random();
Font f = getRandomFont(fontSize);
FontMetrics metrics = graphics.getFontMetrics(f);
AffineTransform affinetransform = new AffineTransform();
FontRenderContext frc = new FontRenderContext(affinetransform,true,true);
int textwidth = (int)(f.getStringBounds(word, frc).getWidth());
int textheight = (int)(f.getStringBounds(word, frc).getHeight());
graphics.setColor(Color.red);
graphics.setFont(f);
int x = textwidth + rand.nextInt(800);
int y = -textheight + rand.nextInt(800);
graphics.drawString(word, x , y );
Run Code Online (Sandbox Code Playgroud)