(newb question) Save a font object as a file?

A

adamorn

Hi!

Im trying to store a font that is selected in my application into a
more local directory. Im using:

GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
arFontNames = ge.getAvailableFontFamilyNames();

To get the fonts that are available on the system, and once the user
selects the font that they want to use, I want to save the file
(the .tff or whatever) to a new location...

So Im a bit stuck... what do I do to copy the file that has created
the font object??? I can clearly create the font object, but I need
it as a file.

any advice???
 
K

Knute Johnson

Hi!

Im trying to store a font that is selected in my application into a
more local directory. Im using:

GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
arFontNames = ge.getAvailableFontFamilyNames();

To get the fonts that are available on the system, and once the user
selects the font that they want to use, I want to save the file
(the .tff or whatever) to a new location...

So Im a bit stuck... what do I do to copy the file that has created
the font object??? I can clearly create the font object, but I need
it as a file.

any advice???

There are a couple of ways you could do this, one is to keep track of
the name and particulars of the Font and save that to a file. Then when
your program runs you can create the font again. The other option is to
save the Font object to a file. Font implements Serializable and so you
should be able to just write it to a file.
 
A

adamorn

There are a couple of ways you could do this, one is to keep track of
the name and particulars of theFontand save that to a file.  Then when
your program runs you can create thefontagain.  The other option is to
save theFontobject to a file.  Fontimplements Serializable and so you
should be able to just write it to a file.

--

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
      ------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access- Hide quoted text -

- Show quoted text -

how would I...
"keep track of the name and particulars of the Font and save that to a
file"???
 
K

Knute Johnson

how would I...
"keep track of the name and particulars of the Font and save that to a
file"???

See Font.getName(), Font.getFontName(), Font.getFamily(),
Font.getSize(), and Font.getStyle().

I got intrigued by the saving the Font object to a file and wrote the
little test program below. The first time you run it, it creates a new
file and after that it loads the font from a file. It appears to work
just fine although I would be curious to know if it will work if the
Font is not installed in the system. Maybe I'll test that.

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class test extends JPanel {
final Font font;
final boolean flag;

public test() throws Exception {
File f = new File("font.dat");
if (f.exists()) {
FileInputStream fis = new FileInputStream(f);
ObjectInputStream ois = new ObjectInputStream(fis);
font = (Font)ois.readObject();
ois.close();
flag = true;
} else {
font = new Font("Comic Sans MS",Font.BOLD,20);
FileOutputStream fos = new FileOutputStream("font.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(font);
oos.close();
flag = false;
}
setPreferredSize(new Dimension(400,300));
}

public void paintComponent(Graphics g) {
g.setFont(font);
if (flag)
g.drawString("This font loaded from file",10,30);
else
g.drawString("This font just created",10,30);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test t = new test();
f.add(t,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
 
K

Knute Johnson

Knute said:
how would I...
"keep track of the name and particulars of the Font and save that to a
file"???

See Font.getName(), Font.getFontName(), Font.getFamily(),
Font.getSize(), and Font.getStyle().

I got intrigued by the saving the Font object to a file and wrote the
little test program below. The first time you run it, it creates a new
file and after that it loads the font from a file. It appears to work
just fine although I would be curious to know if it will work if the
Font is not installed in the system. Maybe I'll test that.

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class test extends JPanel {
final Font font;
final boolean flag;

public test() throws Exception {
File f = new File("font.dat");
if (f.exists()) {
FileInputStream fis = new FileInputStream(f);
ObjectInputStream ois = new ObjectInputStream(fis);
font = (Font)ois.readObject();
ois.close();
flag = true;
} else {
font = new Font("Comic Sans MS",Font.BOLD,20);
FileOutputStream fos = new FileOutputStream("font.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(font);
oos.close();
flag = false;
}
setPreferredSize(new Dimension(400,300));
}

public void paintComponent(Graphics g) {
g.setFont(font);
if (flag)
g.drawString("This font loaded from file",10,30);
else
g.drawString("This font just created",10,30);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test t = new test();
f.add(t,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}

So I was right the code above only saves some of the information out of
the Font and not the details to recreate it if the Font has been removed
from the system.

Another thing you might consider is deploying your program with the font
file. Java uses True Type fonts and you can take the Windows font files
and load them directly into your program. This will work on both
Windows and Linux. Don't ask me about copyright and such, because I
don't know.
 
A

adamorn

Darn.

The application that I wrote allows the user to use any font that
exists, so sadly including the font file in the application wont solve
my issue.

But perhaps I can fool around a bit more with the code that you've
written - if I get it to work using your test code, I will post my
solution!



See Font.getName(), Font.getFontName(), Font.getFamily(),
Font.getSize(), and Font.getStyle().
I got intrigued by the saving the Font object to a file and wrote the
little test program below.  The first time you run it, it creates a new
file and after that it loads the font from a file.  It appears to work
just fine although I would be curious to know if it will work if the
Font is not installed in the system.  Maybe I'll test that.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class test extends JPanel {
    final Font font;
    final boolean flag;
    public test() throws Exception {
        File f = new File("font.dat");
        if (f.exists()) {
            FileInputStream fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(fis);
            font = (Font)ois.readObject();
            ois.close();
            flag = true;
        } else {
            font = new Font("Comic Sans MS",Font.BOLD,20);
            FileOutputStream fos = new FileOutputStream("font.dat");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(font);
            oos.close();
            flag = false;
        }
        setPreferredSize(new Dimension(400,300));
    }
    public void paintComponent(Graphics g) {
        g.setFont(font);
        if (flag)
            g.drawString("This font loaded from file",10,30);
        else
            g.drawString("This font just created",10,30);
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JFrame f = new JFrame();
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    test t = new test();
                    f.add(t,BorderLayout.CENTER);
                    f.pack();
                    f.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

So I was right the code above only saves some of the information out of
the Font and not the details to recreate it if the Font has been removed
from the system.

Another thing you might consider is deploying your program with the font
file.  Java uses True Type fonts and you can take the Windows font files
and load them directly into your program.  This will work on both
Windows and Linux.  Don't ask me about copyright and such, because I
don't know.

--

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
      ------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access- Hide quoted text -

- Show quoted text -
 
A

adamorn

See Font.getName(), Font.getFontName(), Font.getFamily(),
Font.getSize(), and Font.getStyle().
I got intrigued by the saving the Font object to a file and wrote the
little test program below.  The first time you run it, it creates a new
file and after that it loads the font from a file.  It appears to work
just fine although I would be curious to know if it will work if the
Font is not installed in the system.  Maybe I'll test that.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class test extends JPanel {
    final Font font;
    final boolean flag;
    public test() throws Exception {
        File f = new File("font.dat");
        if (f.exists()) {
            FileInputStream fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(fis);
            font = (Font)ois.readObject();
            ois.close();
            flag = true;
        } else {
            font = new Font("Comic Sans MS",Font.BOLD,20);
            FileOutputStream fos = new FileOutputStream("font.dat");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(font);
            oos.close();
            flag = false;
        }
        setPreferredSize(new Dimension(400,300));
    }
    public void paintComponent(Graphics g) {
        g.setFont(font);
        if (flag)
            g.drawString("This font loaded from file",10,30);
        else
            g.drawString("This font just created",10,30);
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JFrame f = new JFrame();
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    test t = new test();
                    f.add(t,BorderLayout.CENTER);
                    f.pack();
                    f.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

So I was right the code above only saves some of the information out of
the Font and not the details to recreate it if the Font has been removed
from the system.

Another thing you might consider is deploying your program with the font
file.  Java uses True Type fonts and you can take the Windows font files
and load them directly into your program.  This will work on both
Windows and Linux.  Don't ask me about copyright and such, because I
don't know.

--

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
      ------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access- Hide quoted text -

- Show quoted text -


Ah, solution found!

TO reload the Font Object do the following - Special thanks to Knute
Johnson:

try {

FileInputStream fileIn = new FileInputStream("font.dat");
ObjectInputStream in = new ObjectInputStream(fileIn);

Font x = (Font)in.readObject();
System.out.println(x.getFontName());

in.close();
fileIn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top