wood burning stoves 2.0*
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
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: 381
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: 24183
    
  34

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: 381
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: 381
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: 41106
    
  45
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 - my free Android networking tools app
Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 381
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to use AWT/Swing w/o window?
 
Similar Threads
displaying XML formatted by XSL in JSP
Groovy for XSL Extensions?
XSL Global Parameter
xml to htrml
Passing an XPath Param value