0

I have been planning to make a font changer app to ultimately put on my phone. But I am having some difficulties at the moment because some fonts just aren't working. I think it could be my code actually. I may be missing something because I saw another Font changer code and it worked perfectly. So all I want to know is why aren't some of these fonts working. Is there a certain method that I am suppose to call? I did some research on this and found that I had to use the import java.awt.Font; Which makes certain fonts work. I could add custom fonts ( which I will later ) but I want to have all the fonts available on my pc to work in this app.

here is my code:

import java.awt.*;
import java.awt.Font.*;
import java.awt.event.*;
import java.applet.Applet;

public class FontChangerForAndroid extends Applet implements ActionListener
    {

    Button Change;
    Button TestOutput;
    Choice Fonts;
    TextField TestTxt;
    Label text;
    /***
     * Okay, this works so far. Not sure if the comic one isnt working. Or if I just spelled it wrong. I will have to
     * look it up in a java Font libaray.
     *
     * I basically want to make this a function on my entire phone. So i'll have to change it to an android app.
     * ( I hope it has the Rosemarry font)
     * While I'm practicing with this app, I'll add a text box that will let me change the label. Or enter the words
     * in the app.
     *
     */
    public void init(){

        setLayout(new FlowLayout());
        Fonts = new Choice();
        //Some fonts don't work because we I don't know what fonts are in the JGE. Working on how to find them
        //Will just add fonts to this system if possible.
        Fonts.addItem("Dialog");
        Fonts.addItem("Serif");
        Fonts.addItem("SansSerif");
        Fonts.addItem("Monospaced");
        Fonts.addItem("DialogInput");
        Fonts.addItem("Calist");
        Fonts.addItem("Centbi");

        add(Fonts);
        TestTxt = new TextField("Enter Text",15);
        add(TestTxt);
        TestOutput = new Button("Enter");
        add(TestOutput);
        Change = new Button("Change Font");
        add(Change);
        text = new Label("Real eye realize real lies");
        add(text);

        Change.addActionListener(this);
        TestOutput.addActionListener(this);
    }

   public void actionPerformed(ActionEvent evt) {
      String item = (String) Fonts.getSelectedItem(); 
      //Gets the selected option from the list and sets the text as the Font.
      if (evt.getSource() == Change){
        text.setFont(new Font(item, Font.PLAIN, 12));
      }
      //In this section you can enter something and print it out on the screen.
      if ( evt.getSource() == TestOutput){
         repaint();
       }
    }


    /**
     * I'm trying to figure out what code goes here in order for the font to change here also....5/22/13
     *
     */
    public void paint (Graphics g){
     //String item = Fonts.getSelectedItem();
    //g.setFont(TestTxt.setFont(new Font(item, Font.PLAIN, 12)));  
     g.drawString(TestTxt.getText(),20,100);

    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

1

There is a method in GraphicsEnvironment called registerFont. E.G. as seen in this answer.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433