vendredi 8 janvier 2016

Java graphics random text

I'm trying to draw random text with fontSize based on word frequency on the BufferedImage using the drawString(word, x , y ) with random x,y values. Unfortunately it draws the random text that is overlaping. The image frame size is 1200 x 650 and the random numbers for x, y are between these values. Here's my code: `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 );`




Aucun commentaire:

Enregistrer un commentaire