Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot apply physical font to an AWT Button in Java applet run under Windows
    primarykey
    data
    text
    <p>I am writing an applet using a "physical font", that I embedded in the jar file as follows:</p> <pre><code>javac Test.java jar -cf Test.jar Test.class *.ttf </code></pre> <p>The compilation gives no error and the font is in the same directory as the source and is successfully embedded in the resulting jar.</p> <p>The font is LiberationSerif-Regular.ttf</p> <p>Here is the code of a near-minimal example Test.java</p> <pre><code>import java.applet.*; import java.awt.*; import java.io.*; public class Test extends Applet { Font font; public Test() { try { InputStream f; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); ClassLoader classLoader = getClass().getClassLoader(); f=classLoader.getResourceAsStream("LiberationSerif-Regular.ttf"); font=Font.createFont(Font.TRUETYPE_FONT,f).deriveFont(15.0f); System.out.println(ge.registerFont(font)); System.out.println("ok"); } catch(Exception e) { System.out.println("oops"); System.out.println(e.getMessage()); } } public void init() { setLayout(new FlowLayout()); Button b=new Button("Am I serifed ?"); add(b); b.setFont(font); } } </code></pre> <p>Here is the file Go.html</p> <pre><code>&lt;HTML&gt; &lt;HEAD&gt;&lt;/HEAD&gt; &lt;BODY&gt; &lt;APPLET CODE = "Test.class" ARCHIVE="Test.jar" WIDTH = 400 HEIGHT=200&gt; &lt;/APPLET&gt; &lt;/BODY&gt; &lt;/HTML&gt; </code></pre> <p>I copy Go.html and Test.jar to a new directory, I cd to that directory, then I run</p> <pre><code>appletViewer Go.html </code></pre> <p>It outputs in the console:</p> <pre><code>true ok </code></pre> <p><em>but</em> the button in the applet has the default font instead of the one I shipped (it has the right size 13pt, though).</p> <p>Tested on Windows 7 using java 1.7.0_25</p> <p>Permuting add(b); and b.setFont(font); did not help.</p> <p>I also tested the resulting code by putting it on my website and browsing it from a Mac and a PC with Firefox : it worked on the mac, but not the pc! Firefox has the same Java plugin version on both machines: 1.7.0_25. Neither the mac nor the pc has this font installed in its OS.</p> <p>If I ask the applet to draw a string in the paint(Graphics g) function, with the physical font, it works! (in the three tests!) So the applet has the font. It even works <em>without</em> registering the font with ge.registerFont(font).</p> <p>I tried to add other buttons with logical fonts in the applet: it works. But logical fonts, though they have system independent names, turn out to have system dependent shape and size, so this is not what I want.</p> <p>My guess is that I forgot some operation, but which?</p> <p>Any help appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload