File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How to use AWT/Swing w/o window? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to use AWT/Swing w/o window?" Watch "How to use AWT/Swing w/o window?" New topic
Author

How to use AWT/Swing w/o window?

Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 376
I've got some example extensions working so I can embed script in my xsl files as per the examples at http://xml.apache.org/xalan-j/samples.html#extensions.

I want to write some extensions that will produce SVG code and perform calcuations based on the bounding box for a text string given a font and its position. I have no interest in creating a window: this is strictly a command line operation.

Can someone point me to an example that uses a function (usually called GetBoundingBox or GetBBox or GetTextExtent) to calculate the width and height (in millimeeters) of a string and correctly position it above another string printed in a different font and center the two strings in a rectangle without creating a Frame or JFrame? I want to use it in the apache XML extensions what will create SVG code for me.

Thanks!
Siegfried
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

See here. Basically, you can provide a system property to the JVM to tell it to run without trying to open the windowing system; then you can use some graphics functions without windows present.


[Jess in Action][AskingGoodQuestions]
Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 376
That is interesting.

I'm still not clear on how I do it! Do I just code up a GUI app that displays a little window (JApplet?) that passes a graphics object to my paint function I perform the font calcuations I need and then recompile it with that system property? Do I call show for my JApplet so it will call my paint function?

Is there a simpler way to get a graphics object that letting JApplet or JFrame pass one to me? All I want is a graphics object, not a window!

What is the name of the function that will give the bounding box for a text string?

Thanks!
Siegfried
Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 376
I tried modifying the code at http://www.java2s.com/Code/JavaAPI/java.awt/FontgetStringBoundsStringstrFontRenderContextfrc.htm to demonstrate the bounding box but no luck: it does not surround the text. Here is my attempt:

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
Does it matter where the box is drawn? The important thing is how big the box is, no? You can check whether it's the right size by inspecting its width and height.

If you want to make this code draw correctly, you should change the rectangle coordinates by the same offsets as the string, i.e. centerX - width / 2 and baselineY, respectively.


Ping & DNS - updated with new look and Ping home screen widget
Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 376
I think I found what I am looking for in "Java Servlet Programming" by Jason Hunter. He has several example servlets that create a AWT frame and from the frame create an image. Hmmm... I'm wondering if I can get millimeter dimensions out of that?

Thanks!
Siegfried
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use AWT/Swing w/o window?
 
Similar Threads
Groovy for XSL Extensions?
Passing an XPath Param value
xml to htrml
XSL Global Parameter
displaying XML formatted by XSL in JSP