Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Error when compile java code. package Java.awt does not exist

1,781 views
Skip to first unread message

Steve Richter

unread,
Oct 10, 2015, 12:01:15 PM10/10/15
to
I am hoping to use java.awt.font.TextMeasurer to measure the width of a text string rendered in a proportional font. But java code that uses the java.awt.font class does not compile for me. Package does not exist.

I have never compiled java code before. Is there a CLASSPATH or other environment variable that must be set to use Java.awt ?

If not awt is there another graphics package I can use?

here is my code. To compile I set the CLASSPATH = ".". Copy the code to an IFS stream file as ascii. Then run javac from QSH.


import java.lang.*;
import java.awt.*;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

class HelloWorld
{
public static void main (String args[]) throws IOException
{

// show that java.io classes will compile.
FileReader inputStream = null;
FileWriter outputStream = null;
try {
inputStream = new FileReader("xanadu.txt");
outputStream = new FileWriter("characteroutput.txt");
} finally {
}

// get package not found error on this code.
Java.awt.Font f = new Java.awt.Font("Helvetica",
Java.awt.Font.Bold, 24 ) ;

System.out.println("Hello World") ;
}
}

Dr.UgoGagliardelli

unread,
Oct 10, 2015, 2:09:22 PM10/10/15
to
Il 10.10.2015 18.01, Steve Richter ha scritto:
> I am hoping to use java.awt.font.TextMeasurer to measure the width of a text string
> rendered in a proportional font.
Non the right way, I suppose.

> But java code that uses the java.awt.font class does not compile for me. Package does not exist.
>
> I have never compiled java code before. Is there a CLASSPATH or other environment variable that must be set to use Java.awt ?
>
> If not awt is there another graphics package I can use?
>
> here is my code. To compile I set the CLASSPATH = ".". Copy the code to an IFS stream file as ascii. Then run javac from QSH.
Nothing todo with classpath. It were just typing mistakes.

>
>
> import java.lang.*;
> import java.awt.*;
> // get package not found error on this code.
> Java.awt.Font f = new Java.awt.Font("Helvetica",
> Java.awt.Font.Bold, 24 ) ;
Here it is, case matters! Java.awt needs to be lowercase, as you put in
imports. Then, if you import java.awt.* you may use just "Font f".
Further more, Font.Bold doesn't exists, you should use Font.BOLD instead.

To measure the width of a text string, you need also a graphics
environment, non just a font. The font itself doesn't carry any
information about graphics, it needs a renderer. For example the
graphics environment of a printer may be not the same as the graphics
environment of a display.

You can use:
FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
int width = fm.stringWidth(text);
or better
graphics.getFontMetrics()
int width = fm.stringWidth(text);
where you can get graphics from any awt container or component or a
printer aswell.
In both cases width is expressed in pixels.

Toolkit.getDefaultToolkit() will return the toolkit for the current
display, thus your width will be calculated for the current primary
screen, that can be different from the secondary one, or any other device.







Steve Richter

unread,
Oct 11, 2015, 3:11:47 PM10/11/15
to
On Saturday, October 10, 2015 at 2:09:22 PM UTC-4, Dr.UgoGagliardelli wrote:
> Further more, Font.Bold doesn't exists, you should use Font.BOLD instead.
>
> To measure the width of a text string, you need also a graphics
> environment, non just a font. The font itself doesn't carry any
> information about graphics, it needs a renderer. For example the
> graphics environment of a printer may be not the same as the graphics
> environment of a display.
>
> You can use:
> FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
> int width = fm.stringWidth(text);
> or better
> graphics.getFontMetrics()
> int width = fm.stringWidth(text);
> where you can get graphics from any awt container or component or a
> printer aswell.
> In both cases width is expressed in pixels.
>
> Toolkit.getDefaultToolkit() will return the toolkit for the current
> display, thus your width will be calculated for the current primary
> screen, that can be different from the secondary one, or any other device.

yeah, I loaded the code into Eclipse on a PC and saw my mistake.

And I am reading this thread which confirms what you are saying:
http://stackoverflow.com/questions/4914145/getting-string-size-in-java-without-having-a-graphics-object-available

What I am thinking is whatever value is returned for another context would be able to be transformed to the value used by AFPDS by multiplying some constant value.

thanks for the help,








Dr.UgoGagliardelli

unread,
Oct 12, 2015, 1:00:22 PM10/12/15
to
I'm pretty confused.
AFPDS has it's own graphics environment. If you want to use java Font I
think that one possible way would be mapping java fonts to afp fonts,
maybe using outline fonts. As AFP fonts are not part of an AFP stream,
only the name appears inside the document, they will be resolved at
runtime, during document presentation I mean. Exactly the same as for
java, and maybe any other presentation system.

Steve Richter

unread,
Oct 12, 2015, 5:01:35 PM10/12/15
to
I am looking for a way to calculate the width of text printed in an AFP proportional font. I do not expect the Java font to be the same size. Just see if there is a consistent proportional relationship between the two.

( I am also being told to look at the QGYOLAFP API. And the AFP toolkit. )

Dr.UgoGagliardelli

unread,
Oct 14, 2015, 6:07:04 AM10/14/15
to
Il 12.10.2015 23.01, Steve Richter ha scritto:
[...]
> I am looking for a way to calculate the width of text printed in an AFP proportional
> font. I do not expect the Java font to be the same size.
> Just see if there is a consistent proportional relationship between the two.
>
> ( I am also being told to look at the QGYOLAFP API. And the AFP toolkit. )
>
Well, calculating the width of a text printed in an AFP proportional
font, given both the font and the codepage, is surely feasable. I did it
for raster fonts only, but I guess that's feasable for outlines too. I
used C, but it can be done using Java as well.
As far as I remember about AFP toolkit, there's no methods to achieve
your goal.
Using AFP APIs you'll be able to materialize both the font and codepage
stream, then you have to parse each code-point (jumping between codepage
and font stream) to calculate the whole size. Obviously you must
thoroughly know MO-DCA and FOCA architectures either.

0 new messages