I keep getting a compile error on the return menuFont line it says there is no variable menuFont. Could someone please tell how to fix this.
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class loadFiles {
Font getFont(){
try {
Font menuFont = Font.createFont( Font.TRUETYPE_FONT, new FileInputStream("font.ttf"));
} catch (FileNotFoundException e) {
System.out.println("Cant find file.");
e.printStackTrace();
} catch (FontFormatException e) {
System.out.println("Wrong file type.");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Unknown error.");
e.printStackTrace();
}
return menuFont;
}
}
The basic problem with your code is that the Font object is only in scope for the duration of the try block, so it’s no longer available in your return statement at the end of the method. Two options:
Move the variable declaration outside the try block:
Or, do
return Font.creatFont(...)inside the try, thus avoiding the need for the variable in the first place (and, obviously,return nullat the end of the method).