What is the correct syntax for changing fonts and drawing strings in Java?

Pit Digger picture Pit Digger · Mar 30, 2011 · Viewed 8.7k times · Source

Can someone check my syntax here? I am passing "Times New Roman","Arial","Verdana" to fontName and using 8,12,15 etc. for fontSize. It never changes the font here. I am doing this to write some text over an image.

Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
g2d.drawImage(photo, 0, 0, null);
g2d.setColor(Color.white);
Font font = new Font(fontName, Font.PLAIN, fontSize);
g2d.setFont(font);
g2d.drawString(text,x,y);

Answer

Pit Digger picture Pit Digger · Mar 30, 2011

I finally found out that none of fonts from my list were there on the system so I had to use getAllFonts() method and pass only those fonts from the list .