embedding fonts into applets

hi im making a applet game and it has a custom font

how can i embed the font so the user does not have to install it manually

i read on page u can do this by adding it to the jar and pointing to it some how but they dint explain how

is this possible or is there another way ? point to the font if i upload it or soming

thx :)

[356 byte] By [russdxa] at [2007-11-26 17:09:56]
# 1
Font has a static createFont method that takes an InputStream.For the input stream, take the URL of the font and openStream(),or use the class method getResourceAsStream().
DrLaszloJamfa at 2007-7-8 23:37:45 > top of Java-index,Java Essentials,New To Java...
# 2

not 100% clear on what you mean

to display my font i do

bronze.setBounds(((187*maxSlots)/2)-99, 14 ,100, 50);

bronze.setFont(new Font("LCD", Font.PLAIN, 19));

bronze.setForeground(Color.red);

parrentapp.add(bronze);

how would i go about getting that front from Root of applet class on my server

thanks

russdxa at 2007-7-8 23:37:45 > top of Java-index,Java Essentials,New To Java...
# 3

bronze.setFont(new Font((this.getClass().getClassLoader().getResourceAsStream("AthleticTown.ttf")), Font.PLAIN, 19));

how come this wont compile then :(

D:\UNI\Project\Socket>javac SimClient.java

.\drawTopbox.java:23: cannot find symbol

symbol : constructor Font(java.io.InputStream,int,int)

location: class java.awt.Font

bronze.setFont(new Font((this.getClass().getClassLoader().getResourceAsStream("Athle

ticTown.ttf")), Font.PLAIN, 19));

^

1 error

this is the code import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.applet.*;

import java.io.InputStream.*;

public class drawTopbox {

private Applet parrentapp;

JLabel imageLabel;

ImageIcon ImageIcon;

JLabel bronze = new JLabel("000.00");

JLabel silver = new JLabel("000.00");

JLabel gold = new JLabel("000.00");

//InputStream is = this.getClass().getClassLoader().getResourceAsStream("license.txt");

public drawTopbox(Applet parrent, int maxSlots) {

parrentapp = parrent;

// bronze

bronze.setBounds(((187*maxSlots)/2)-99, 14 ,100, 50);

bronze.setFont(new Font("LCD", Font.PLAIN, 19));

bronze.setForeground(Color.red);

parrentapp.add(bronze);

// silver

silver.setBounds(((187*maxSlots)/2)-54, 45 ,100, 50);

silver.setFont(new Font("LCD", Font.PLAIN, 25));

silver.setForeground(Color.red);

parrentapp.add(silver);

// gold

gold.setBounds(((187*maxSlots)/2)-20, 9 ,200, 50);

gold.setFont(new Font("LCD", Font.PLAIN, 35));

gold.setForeground(Color.red);

parrentapp.add(gold);

// background

ImageIcon = new ImageIcon(parrentapp.getImage(parrentapp.getCodeBase(), "Images/topbox.gif"));

imageLabel = new JLabel(ImageIcon);

imageLabel.setBounds(((187*maxSlots)/2)-135, 0, 271, 100);

parrentapp.add(imageLabel);

parrentapp.setLayout(null);

}

/***************************

*displayBronze*

***************************/

public void displayBronze(double value) {

bronze.setText(String.valueOf(value) + "0");

}

/***************************

*displaySilver*

***************************/

public void displaySilver(double value) {

silver.setText(String.valueOf(value) + "0");

}

/***************************

*displayGold*

***************************/

public void displayGold(double value) {

gold.setText(String.valueOf(value) + "0");

}

/***************************

*clearValue*

***************************/

public void clearValue(int value) {

switch (value) {

case 0:

gold.setText("");

break;

case 1:

silver.setText("");

break;

case 2:

bronze.setText("");

break;

}

}

}

just can not get tht input stream to compile :(

Message was edited by:

russdx

Message was edited by:

russdx

Message was edited by:

russdx

russdxa at 2007-7-8 23:37:45 > top of Java-index,Java Essentials,New To Java...
# 4

You tried to pass an input stream to a Font constructor, but there is no such constructor. Take a closer look at my hint: Font has a static factory method called [url=http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Font.html#createFont(int,%20java.io.InputStream)]createFont[/url] that you should use.

DrLaszloJamfa at 2007-7-8 23:37:45 > top of Java-index,Java Essentials,New To Java...
# 5

been playin with this for past few hours not getting any were just dont no java well enough to really understand whats going on

all going over my head

font is at http://www.auba18.dsl.pipex.com/fruitsim/AthleticTown.ttf

my applet class files r at

http://www.auba18.dsl.pipex.com/fruitsim/

so same place as my font

i just need the applet to some how load this font and display it were the other ones are grabbing "LCD"

iv got a few pieces of code no idea which one to use or how to get them to compile properlty

would some one be able to add the code in for me would really appreciate it thanks :)

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.applet.*;

import java.io.*;

import java.io.InputStream;

import java.net.URL;

public class drawTopbox {

private Applet parrentapp;

JLabel imageLabel;

ImageIcon ImageIcon;

JLabel bronze = new JLabel("000.00");

JLabel silver = new JLabel("000.00");

JLabel gold = new JLabel("000.00");

//InputStream in = getClass().getResourceAsStream("AthleticTown.ttf");

//public static Font createFont(int in);

//Fontfont = Font.createFont(Font.TRUETYPE_FONT, in);

//InputStream fis = new FileInputStream("font.ttf");

//Font f = Font.createFont(Font.TRUETYPE_FONT, fis);

public drawTopbox(Applet parrent, int maxSlots) {

parrentapp = parrent;

try {

URL url = new URL("AthleticTown.ttf");

InputStream is = url.openStream();

Font font = Font.createFont(Font.TRUETYPE_FONT, is);

} catch (Exception e) {

e.printStackTrace();

}

// bronze

bronze.setBounds(((187*maxSlots)/2)-99, 14 ,100, 50);

bronze.setFont(new Font(font, Font.PLAIN, 25));

bronze.setForeground(Color.red);

parrentapp.add(bronze);

// silver

silver.setBounds(((187*maxSlots)/2)-54, 45 ,100, 50);

silver.setFont(new Font("LCD", Font.PLAIN, 25));

silver.setForeground(Color.red);

parrentapp.add(silver);

// gold

gold.setBounds(((187*maxSlots)/2)-20, 9 ,200, 50);

gold.setFont(new Font("LCD", Font.PLAIN, 35));

gold.setForeground(Color.red);

parrentapp.add(gold);

// background

ImageIcon = new ImageIcon(parrentapp.getImage(parrentapp.getCodeBase(), "Images/topbox.gif"));

imageLabel = new JLabel(ImageIcon);

imageLabel.setBounds(((187*maxSlots)/2)-135, 0, 271, 100);

parrentapp.add(imageLabel);

parrentapp.setLayout(null);

}

/***************************

*displayBronze*

***************************/

public void displayBronze(double value) {

bronze.setText(String.valueOf(value) + "0");

}

/***************************

*displaySilver*

***************************/

public void displaySilver(double value) {

silver.setText(String.valueOf(value) + "0");

}

/***************************

*displayGold*

***************************/

public void displayGold(double value) {

gold.setText(String.valueOf(value) + "0");

}

/***************************

*clearValue*

***************************/

public void clearValue(int value) {

switch (value) {

case 0:

gold.setText("");

break;

case 1:

silver.setText("");

break;

case 2:

bronze.setText("");

break;

}

}

}

silver and gold work fine (old way grabbing from pc)

trying to convert bronze to work by grabbing font from my server

russdxa at 2007-7-8 23:37:45 > top of Java-index,Java Essentials,New To Java...
# 6

ok think iv grabbed the font on using

InputStream is = getClass().getResourceAsStream("AthleticTown.ttf");

public drawTopbox(Applet parrent, int maxSlots) {

parrentapp = parrent;

Font font = null;

try {

font = Font.createFont(Font.TRUETYPE_FONT, is);

} catch (Exception e) {

e.printStackTrace();

}

displays

ava.awt.Font[family=AthleticTown v0.1,name=AthleticTown v0.1,style=plain,size=1]

on system out so guessing its got it

how do i go about resizing it and applyin it to my lable tho

need to apply it here

bronze.setBounds(((187*maxSlots)/2)-99, 14 ,100, 50);

//bronze.setFont(new Font(font, Font.PLAIN, 25));

bronze.setForeground(Color.red);

parrentapp.add(bronze);

above does not work

russdxa at 2007-7-8 23:37:45 > top of Java-index,Java Essentials,New To Java...
# 7
> above does not workOkay. So your assignment is to find out in what way it does not work and report back. Full details, please.
DrClapa at 2007-7-8 23:37:45 > top of Java-index,Java Essentials,New To Java...
# 8
i no its commented out lolwhen its NOT commented out does not workout of ideas :(got a feel i dont need to do this load font code i just need to set the size on my font object then apply that to my bronze label
russdxa at 2007-7-8 23:37:45 > top of Java-index,Java Essentials,New To Java...
# 9
well this seamed to do the trickFont fontx = font.deriveFont(24.0f);got der in the end lol
russdxa at 2007-7-8 23:37:45 > top of Java-index,Java Essentials,New To Java...