Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use AWT/Swing w/o window?

 
Siegfried Heintze
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Siegfried Heintze
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Siegfried Heintze
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic