-1

I want to put a custom font using drawString method in my applet the problem is that it doesn't change the font even I do it properly. I am still student so please bare at me for my code. Please help me to change the font. I found out that there are certain fonts that java allowed to use in method font please help me.

import javax.swing.*;
import java.awt.*;
public class WoodyWoodPeckerShow extends JApplet {
    private Font font;



    public void init(){
        setFont(new Font("WoodPecker",Font.TRUETYPE_FONT,30));
    }

    public void paint(Graphics g){
        Graphics2D g2 = (Graphics2D)g;

        g2.setFont(font);
        g2.drawString("WoodyWoodPecker",10,200);


    }
}

It gives me the default font instead of like this enter image description here

Please help me. Thank you guys. Hope you can help me :)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rig
  • 7
  • 4
  • 1) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/moving-to-a-plugin-free-web). 2) If the code lists the fonts in the `init` method, does "Woodpecker" appear? See [this answer](https://stackoverflow.com/a/8365030/418556) on importing a font. – Andrew Thompson Nov 11 '19 at 00:30

2 Answers2

0

The instance variable font in your WoodyWoodPeckerShow class is null, because you only set the value of the superclass variable that has the same name. You can remove the instance variable altogether and use the one in the superclass, like this:

import javax.swing.*;
import java.awt.*;

public class WoodyWoodPeckerShow extends JApplet {
    @Override
    public void init() {
        setFont(new Font("WoodPecker", Font.TRUETYPE_FONT, 30));
    }

    @Override
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        g2.setFont(getFont());
        g2.drawString("WoodyWoodPecker", 10, 200);
    }
}
radulfr
  • 616
  • 3
  • 6
0

You haven’t set any properties for your font you declared, that’s why you got font with default size and style.

 g2.setFont(font);

You have to set all this stuff or declare new font as anonymous class , like:

g2.setFont(new Font("Some", Font.BOLD, 16));
Gipsy King
  • 186
  • 2
  • 2
  • 14
  • I forgot to write it here, but it doesn't work, If I use serif fonts it change but when other fonts that are glamorous it doesn't change. – Rig Nov 10 '19 at 13:01
  • @Rig what kind of fonts did you try to use? – Gipsy King Nov 10 '19 at 13:03
  • I am trying to use WoodPecker font as you can see in the image that I provide. – Rig Nov 10 '19 at 13:04
  • https://www.wfonts.com/font/woodpecker it is the link for the font, please help me it will be a pleasure for me. I am just new in java – Rig Nov 10 '19 at 13:06