Hola a todos, muy buen tema pero el metodo que posteo rigoxls no funciona al menos no a mi.
public class fuentes{
public fuentes(){
}
public Font fuentes1(String font_name){
try {
font = Font.createFont(Font.TRUETYPE_FONT, new File(font_name));
}catch (Exception ex) {
}
return font.deriveFont(60f);
}
}
Ese código tiene algunos errores bueno cualquiera que no sea novato lo notaria, pero lo digo que si se va a posterar algún código que sea bien, no tengo nada contra ti, asi que no te la tomes a mal.
p. ej.
fuentes1??
font no está declarada.
Pero corrigiendo esto aun asi al compilar no te marca error, sino hasta ejecutar... por lo menos a mi me tira el siguiente error.
Exception in thread "main" java.lang.NullPointerException
at fuentes.fuentes(fuente.java:87)
at fuente.inciarComponentes(fuente.java:35)
at fuente.<init>(fuente.java:16)
at fuente.main(fuente.java:12)
fuente es mi clase principal.
Y mejor aun, volviendo al punto de obtener la fuente del jar ese metodo no funciona ya que utilizas la clase File, mas bien se debe usar la clase InputStream, como lo dice la referencia que posteaste.
However, an InputStream might be needed if your code is unable to access file system resources, or if the font is packaged in a Java Archive (JAR) file along with the rest of the application or applet.
fuente:
http://java.sun.com/docs/books/tutorial/2d/text/fonts.htmlmas bien deberia ser algo asi.
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void loadFont() throws FontFormatException, IOException{
String fontFileName = "yourfont.ttf";
InputStream is = this.getClass().getResourceAsStream(fontFileName);
Font ttfBase = Font.createFont(Font.TRUETYPE_FONT, is);
Font ttfReal = ttfBase.deriveFont(Font.PLAIN, 24);
}
public static void main(String[] args) {
}
}
fuente:
http://www.java2s.com/Code/JavaAPI/java.awt/FontcreateFontintfontFormatInputStreamfontStream.htmY para los que no saben como llamar la fuente obtenida desde su clase principal es algo asi (Utilizando la clase (corregida) de rigoxls).
En la clase donde se manda llamar
fuentes f = new fuentes();
Font font = f.fuentes("Kaela.ttf");
etqHMC.setFont(font);
etqHMC.setText("Hola Mundo Cruel");
Esta es mi clase fuentes
class fuentes{
public Font font;
public fuentes(){
}
public Font fuentes(String nombreFuente, int tamanoFuente){
InputStream nombre = getClass().getResourceAsStream("/fonts/"+nombreFuente); //Jar
try{
font = Font.createFont(Font.TRUETYPE_FONT, nombre);
font = font.deriveFont(Font.PLAIN, tamanoFuente);
}catch (Exception ex){
}
return font;
}
}
/fonts/ es una carpeta donde tengo mis fuentes
Bueno esto lo posteo para aquellos que se interesen, y que no les haya funcionado el primer metodo, y al parecer es la unica forma de hacerlo.
Alguien sabe si se pueden abrir fonts OpenType? o solo truetype.
Se aceptan criticas o sugerencias
Saludos