I have this class, and I am trying to display a custom font in a text field, but but when I run it the font is super tiny like 2px tiny. If i just run font = new Font("sans-serif", Font.PLAIN, 24); it displays just fine at the right font size.
Here is what it looks like:
Here is what it looks like when I only use font = new Font("sans-serif", Font.PLAIN, 24);
What is causing the small text box with a custom font?
public class Search extends JTextField{
public Search(int width){
super(width);
Font font;
String filename = "/media/fonts/SourceCodePro-Light.ttf";
try{
InputStream is = this.getClass().getResourceAsStream(filename);
font = Font.createFont(Font.TRUETYPE_FONT, is);
font = font.deriveFont(24);
}catch(FontFormatException | IOException ex){
font = new Font("sans-serif", Font.PLAIN, 24);
}
this.setFont(font);
}
}
font.deriveFont has two overloaded forms that can be quite similar. The one taking int sets the font style, the one taking float sets the font size. YOu are invoking the int version instead of the float version. Change 24 to 24.0f, and it will work
Related
So I am working on a small project using Swing and I am trying to add a font to a JLabel, the font is a little bit weird it's called you murderer bb, I already am using a font that I added and it works fine, but when I do the exact same thing to this one well... it just displays a regular font.
private Font font;
File fontFile = new File("resources\\fonts\\Nunito-Regular.ttf");
try {
font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
font = font.deriveFont(14f);
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
private Font titleFont;
fontFile = new File("resources\\fonts\\youmurdererbb_reg.ttf");
try {
titleFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
titleFont = font.deriveFont(40f);
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
private JLabel title;
title = new JLabel("Welcom To Eureka");
title.setFont(titleFont);
title.setHorizontalAlignment(SwingConstants.CENTER);
title.setForeground(Color.decode("#FFFFFF"));
title.setBounds(228, 125, 354, 50);
private JLabel username;
username = new JLabel("Log In");
username.setFont(font);
username.setHorizontalAlignment(SwingConstants.CENTER);
username.setForeground(Color.decode("#BB86FC"));
username.setBounds(682, 80, 48, 20);
username.addMouseListener(new AppControler());
so the username is working fine and displaying the correct font but the title is just displaying a bigger font (i set the size of it to 40) but the font is not the one I am using
titleFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
titleFont = font.deriveFont(40f);
Should be:
titleFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
titleFont = titleFont.deriveFont(40f); // <- use the font just created!
Result (once 1st word spelling changed):
Is there a way to set fonts on a string, and then draw the string using graphics? I know you can do it on Jlabels, and Jtextfields, and J-other components, but is there a way to do it just on the string instead? Thank you.
You can do it with attributed strings an example of is
Font font = new Font("LucidaSans", Font.PLAIN, 14);
AttributedString atString= new AttributedString("Example text string");
atString.addAttribute(TextAttribute.FONT, font);
graphic.drawString(atString.getIterator(),x,y);
Cheers!
We can actually set the font in graphics...and then draw the string.
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.setFont(Fonts.f);
g.drawString(Fonts.text, 50, 50);
}
Yes, it's possible, start by checking out the Graphics, Text tutorials
Basically, you can set the font using Graphics#setFont and draw a String using Graphics#drawString.
Also see Performing Custom Painting for how you can perform custom painting in Swing
For example
List available font names....
String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for (String font : fonts) {
System.out.println(font);
}
or
Font fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for (Font font : fonts) {
System.out.println(font);
}
You can manipulate a font's properties using one of the Font#derive methods...
Font font = ...;
Font bigger = font.derive(32f);
Font bolder = font.derive(Font.BOLD);
Font biggerAndBolder = font.derive(Font.BOLD, 32f);
I found this component recently and its great exactly what I wanted but I am unsure how to use it.
How would I set the font of my JTextField to what is selected by the user in the JFontChooser?
This is all I can find on it:
The JFontChooser class is a swing component for font selection. This class has JFileChooser like APIs. The following code pops up a font chooser dialog.
JFontChooser fontChooser = new JFontChooser();
int result = fontChooser.showDialog(parent);
if (result == JFontChooser.OK_OPTION)
{
Font font = fontChooser.getSelectedFont();
System.out.println("Selected Font : " + font);
}
what I want it to do is:
update: tNumber.setFont(new font(""Tahoma", Font.BOLD, 300));
To what ever the user has chosen for the font style and size in font chooser.
Use font.deriveFont(Font.BOLD, 300f) to create a new font, based on the current font with the properties you supply.
tNumber.setFont(font.deriveFont(Font.BOLD, 300f));
See Font#deriveFont(float, int) for more details
Well you were very close,
JFontChooser fontChooser = new JFontChooser();
int result = fontChooser.showDialog(parent);
if (result == JFontChooser.OK_OPTION)
{
Font font = fontChooser.getSelectedFont();
tNumber.setFont(font.deriveFont(Font.BOLD, 300f));//This is the line I added.
}
See setFont and deriveFont documentation.
Usually, when I initialize the fonts I want to use in my SWING applications, I do it this way:
public static final Font TITLEFONT = new Font("Calibri", Font.BOLD, 40);
Now, I have to do it a bit differently since I'm using some custom fonts from a .ttf file. I initialize the font this way:
try
{
InputStream is = OptionsValues.class.getResourceAsStream("fonts//KOMIKAX_.ttf");
TITLEFONT = Font.createFont(Font.TRUETYPE_FONT, is);
}
catch (Exception ex)
{
ex.printStackTrace();
System.err.println("Font not loaded. Using Calibri font.");
TITLEFONT = new Font("Calibri", Font.BOLD, 40);
}
I'm pretty sure it initializes it correctly (I can't tell for sure since it is too small for me to see), but I'd like to know how I can manually set the font's size (and if it's bold / other attributes) when loading a font this way.
Thanks a lot in advance!
createFont returns a Font and you can call deriveFont(...) on this, passing in a float for the point size, or an int and float for Font style and point size. I cannot say whether it will work for your particular situation, but it's worth a try.
e.g.,
InputStream is = OptionsValues.class.getResourceAsStream("fonts//KOMIKAX_.ttf");
TITLEFONT = Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(Font.BOLD, 40f);
I'd simply use:
Font.ITALIC
Font.BOLD
Font.PLAIN
Is there any way to get the system default font name in Java? The default font can differ from os. So it can create trouble if we use font Arial and the jar is running in Linux without having Arial font installed.
Try this:
private final Font FONT = new JLabel().getFont();
JavaFX makes this a lot easier:
import javafx.scene.text.Font;
then use:
Font defaultFont = Font.getDefault();
or
// Where 14 is the font size
Font defaultFont = new Font(14);
Use the defined Font constants such as SERIF/SANS_SERIF etc.
I am currently using this to get the default font, although I would rather not need to use a graphics object to get it:
private final Font getFont()
{
Graphics g = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB).getGraphics();
Font font = new Font(g.getFont().toString(), 0, 12);
g.dispose();
return font;
}
I don't think there is a way of retrieving a system default font(in Swing/AWT the font is normally associated with the current LAF and component, for instance), but if your concern is font compatibility - you could check the font you are using against all the system fonts:
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] allFonts = e.getAllFonts();
and make a "fail-over" choice if it doesn't exist.
Take a look at public static Font decode(String str) here. When the decode method receives a null pointer as a parameter it returns the "Dialog" font which is usually the system font.
getFont() returns the current font, which is (usually?) the default.
I did this to increase font size.
public MyTextArea(){
Font currentFont = super.getFont();
String fontName = currentFont.getFontName();
int fontStyle = currentFont.getStyle();
int fontSize = currentFont.getSize() + 4;
super.setFont(new Font(fontName, fontStyle, fontSize));
}
In Windows, Segoe UI
Visit http://www.apaddedcell.com/sites/www.apaddedcell.com/files/fonts-article/final/index.html to see a list of preinstalled fonts.
I chose Verdana