Search code examples
javafont-sizegraphics2ddrawstring

How to decrease font size to 0.0001f when using drawString()?


I have an assignment right now about OpenStreetMaps, where one of the exercises is to display the road names at their respective roads on the map.

My problem right now, is that the coordinates we're using are so small, that even the smallest int font size is hundred times larger than what it's supposed to be.

I have tried the method deriveFont(), but it doesn't seem to have any effect.

g.setPaint(Color.black);
    for (DrawnString d : model.getRoads()){
        Point2D.Double p = d.getPosition();

        Font font = new Font("TimesRoman", Font.PLAIN, 1);
        font.deriveFont(0.0001f); //doesn't work!
        g.setFont(font);

        g.drawString(d.getText(), (float) p.x, (float) p.y);
    }

My question is, if there's a way to decrease the font size to a small size like 0.0001f?


Solution

  • The deriveFont() method returns an object of type font that is a replica of the calling font with changed parameters. So change the line to: font = font.deriveFont(0.001f); and everything works just as expected (with very tiny font)