Home pageFind It!Contact PJAPJA documentation

PJA

 PJA Toolkit forum

This forum is dedicated to PJA Toolkit.
You may read freely the messages it contents. If you want to write a message or answer to a subject, subscribe to it first.

Subjects Recent messages Login Subscribe

Messages of subject Why my servlet can't print a picture with text on it ?

lcq210

Member since : May 23, 2003
Messages : 9
 Jul 23, 2003 at 5:46 AM
Let me explain my situation:I got PJADemo and a servlet in a same directory, there is a .pjaf file in the diretory.When I run PJADemo with java command line, It works well,and created a picture with my text on it. But When I run the servlet in web browser,It just can't find the .pjaf font file.
And my servlet draw rectangle and polygon with no problem. Why my servlet can't print a picture with text on it in browser?
Somebody help me...thanks!

lcq210

Member since : May 23, 2003
Messages : 9
 Jul 24, 2003 at 5:55 AM
Something wrong with my servlet?Here's the code:

import java.awt.Font;
import java.awt.AWTError;
import com.eteks.awt.servlet.PJAServlet;
import com.eteks.awt.PJAImage;
import com.eteks.awt.PJAGraphicsExtension;
import com.eteks.tools.fontcapture.PJAFontCapture;
import Acme.JPM.Encoders.GifEncoderNoCM;
import com.eteks.filter.Web216ColorsFilter;

public class PJAServletTest1 extends PJAServlet
{
public void doGetPJA (HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
response.setContentType ("text/plain");
PrintStream out = new PrintStream (response.getOutputStream ());
ComputeImageFiles (new PJAGraphicsTextDrawer (), new PJAImageBuilder (), "PJADemo1",out);

}//end of doGetPJA
protected static void ComputeImageFiles (PJAGraphicsTextDrawer drawer ,
ImageBuilder imageBuilder,
String filePrefix,
PrintStream out)
{

// Create an image and draw into it
Image image = imageBuilder.createImage (100, 50);

// Draw in the image
try
{
System.err.println ("Computing image (instance of " + image.getClass ().getName () + ")");
Graphics gc = image.getGraphics ();
try
{
if (gc instanceof Graphics2D)
{
// If Java2D available, compute the nicest image possible
((Graphics2D)gc).setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
((Graphics2D)gc).setRenderingHint (RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
((Graphics2D)gc).setRenderingHint (RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
}
}
catch (LinkageError e)
{ } // Graphics2D not found
drawer.paint (gc);

}
catch (AWTError error)
{
System.err.println (error + "\nIn case of an error related to font, you should run first PJAFontCapture to generate at least one .pjaf font file.");
}

try
{

new GifEncoderNoCM (new FilteredImageSource (image.getSource (),
new Web216ColorsFilter ()),out).encode ();
out.close ();
}
catch (IOException e)
{
System.err.println ("Image image couldn't be displayed");
}

}

private static class PJAImageBuilder extends ImageBuilder
{
public Image createImage (int width, int height)
{
return new PJAImage (width, height);
}
}


// Text exemples
private static class PJAGraphicsTextDrawer
{
// Paint rewriten to avoid use of Color, Font and FontMetrics classes
public void paint (Graphics gc)
{
((PJAGraphicsExtension)gc).setColor (102,204,255);
gc.fillRect (0, 0, 100, 50);
((PJAGraphicsExtension)gc).setFont ("Albertus Medium.pjaf", Font.PLAIN, 16); //this line throws an exception
((PJAGraphicsExtension)gc).setColor (0, 0, 0);
gc.drawString ("haha", 5, 20);


}

public String toString ()
{
return "TextDrawer";
}
}

protected static class ImageBuilder
{
public Image createImage (int width, int height)
{
// Java 1.0 simplest way to create a buffered image
Frame frame = new Frame ();
frame.addNotify ();
return frame.createImage (width, height);
}
}

protected static interface GraphicsDrawer
{
public int getWidth ();
public int getHeight ();
public void paint (Graphics gc);
}

}

bohnman

Location : essex jct, vermont, usa
Member since : Aug 4, 2003
Messages : 3
 Aug 4, 2003 at 6:43 PM
It looks as if you aren't telling your servlet where the .pjaf file resides. Try something like:

//directory where pjaf resides
String fontPath = getServletContext().getRealPath("/pjaf");
//set java.awt.fonts to that directory
System.setProperty("java.awt.fonts", fontPath);
---
Mike


Home pageFind It!ContactTop

© Copyrights 1997-2014 eTeks - All rights reserved

PJAPJA documentation