How to make the font size bigger in g.drawString("Hello World",10,10); ?
- 
                    5Did you try `setFont()`? – trashgod Aug 15 '13 at 09:19
- 
                    7Start by taking at [java.awt.Font](http://docs.oracle.com/javase/7/docs/api/java/awt/Font.html). The method Font#deriveFont is very useful – MadProgrammer Aug 15 '13 at 09:23
- 
                    The answer below is mostly right. Start with the question slightly reworded. How do I change the font size of a g or g2d drawstring object? – Max West Jun 20 '14 at 15:15
- 
                    1The answer below is mostly right. Start with the question slightly reworded. How do I change the font size of a g or g2d drawstring object? First create your g (or g2d) drawstring object `String string = "Hello World";` then create a Font object `Font stringFont = new Font( "SansSerif", Font.PLAIN, 18 );` Next set the Font object to the g or g2d object `g2d.setFont( stringFont );` Now apply the g2d (or g) object to your drawstring object `g2d.drawString( string, Xposition, Yposition )` where X and Y are your integers for the positioning coordinates. – Max West Jun 20 '14 at 15:26
5 Answers
g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize)); 
Where fontSize is a int. The API for drawString states that the x and y parameters are coordinates, and have nothing to do with the size of the text.
- 
                    8This works, but if you only want to set the font size and not the font name, you can use `g.getFont().getFontName()` in place of the font name parameter – shieldgenerator7 Oct 07 '14 at 15:53
- 
                    11@shieldgenerator7 I think if you only want to change the size, then MadProgrammer's suggestion is better: use `deriveFont(float size)`. – Yohanes Khosiawan 许先汉 Oct 30 '14 at 10:39
- 
                    @YohanesKhosiawan许先汉 You're right that is much easier, however at the time I didn't see his comment because it was unreadable. Thanks for pointing that out. – shieldgenerator7 Nov 06 '14 at 13:05
- 
                    2For those seeking the the default font, `g.getFont().getFontName()` is your best bet, but on all the systems I have tested on, it's `Dialog`. – Scruffy Feb 21 '15 at 10:28
Because you can't count on a particular font being available, a good approach is to derive a new font from the current font. This gives you the same family, weight, etc. just larger...
Font currentFont = g.getFont();
Font newFont = currentFont.deriveFont(currentFont.getSize() * 1.4F);
g.setFont(newFont);
You can also use TextAttribute.
Map<TextAttribute, Object> attributes = new HashMap<>();
attributes.put(TextAttribute.FAMILY, currentFont.getFamily());
attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_SEMIBOLD);
attributes.put(TextAttribute.SIZE, (int) (currentFont.getSize() * 1.4));
myFont = Font.getFont(attributes);
g.setFont(myFont);
The TextAttribute method often gives one even greater flexibility. For example, you can set the weight to semi-bold, as in the example above.
One last suggestion... Because the resolution of monitors can be different and continues to increase with technology, avoid adding a specific amount (such as getSize()+2 or getSize()+4) and consider multiplying instead.  This way, your new font is consistently proportional to the "current" font (getSize() * 1.4), and you won't be editing your code when you get one of those nice 4K monitors.
Font myFont = new Font ("Courier New", 1, 17);
The 17 represents the font size. Once you have that, you can put:
g.setFont (myFont);
g.drawString ("Hello World", 10, 10);
 
    
    - 65
- 1
- 4
code example below:
g.setFont(new Font("TimesRoman", Font.PLAIN, 30));
g.drawString("Welcome to the Java Applet", 20 , 20);
 
    
    - 1,746
- 1
- 12
- 29
 
    
    - 51
- 1
- 1
I've an image located at here, Using below code. I am able to contgrol any things on the text that i wanted to write (Eg,signature,Transparent Water mark, Text with differnt Font and size).
 import java.awt.Font;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.font.TextAttribute;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.URL;
    import java.util.HashMap;
    import java.util.Map;
    import javax.imageio.ImageIO;
    public class ImagingTest {
        public static void main(String[] args) throws IOException {
            String url = "http://images.all-free-download.com/images/graphiclarge/bay_beach_coast_coastline_landscape_nature_nobody_601234.jpg";
            String text = "I am appending This text!";
            byte[] b = mergeImageAndText(url, text, new Point(100, 100));
            FileOutputStream fos = new FileOutputStream("so2.png");
            fos.write(b);
            fos.close();
        }
        public static byte[] mergeImageAndText(String imageFilePath,
                String text, Point textPosition) throws IOException {
            BufferedImage im = ImageIO.read(new URL(imageFilePath));
            Graphics2D g2 = im.createGraphics();
            Font currentFont = g2.getFont();
            Font newFont = currentFont.deriveFont(currentFont.getSize() * 1.4F);
            g2.setFont(newFont);
            Map<TextAttribute, Object> attributes = new HashMap<>();
            attributes.put(TextAttribute.FAMILY, currentFont.getFamily());
            attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_SEMIBOLD);
            attributes.put(TextAttribute.SIZE, (int) (currentFont.getSize() * 2.8));
            newFont = Font.getFont(attributes);
            g2.setFont(newFont);
            g2.drawString(text, textPosition.x, textPosition.y);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(im, "png", baos);
            return baos.toByteArray();
        }
    }
 
    
    - 3,707
- 5
- 36
- 57
 
     
     
     
     
    