
Signature
Regards,
Jarrick
------------------------------------
Jarrick.Chagma@your.mind.gmail.com
(Lose your mind before emailing me)
> > I have written code which creates an image on a JPanel (inside a
> > JFrame) and saves it as a jpeg.
[quoted text clipped - 8 lines]
> I am a bit confused. All images implement getWidth() and getHeight()
> methods. Is there a reason why you cannot use those?
The image includes a lot of text. I override the paintComponent
method, and use the drawX methods on the Graphics2D object.
If I create an image by say creating a BufferedImage, the size is
taken in the constructor.
Basically in a nutshell I suppose I want to crop the image, or resize
the JPanel to fit the image, with no excess...
Is that any clearer?
Knute Johnson - 29 Jun 2009 22:38 GMT
>>> I have written code which creates an image on a JPanel (inside a
>>> JFrame) and saves it as a jpeg.
[quoted text clipped - 15 lines]
> the JPanel to fit the image, with no excess...
> Is that any clearer?
BufferedImage has a method to get a sub-image so assuming you know how
big it is, that shouldn't be a problem. If you then want do draw that
image onto a JPanel and you want the full JPanel displayed in your
JFrame, you need to set the preferred size of the JPanel to the size of
your image and call pack() on the JFrame. You also might just be able
to call validate() but I'd have to try that.
But it still isn't 100% clear that that is what you want. You might try
again by explaining it from the beginning.

Signature
Knute Johnson
email s/nospam/knute2009/
markspace - 30 Jun 2009 02:20 GMT
> But it still isn't 100% clear that that is what you want. You might try
> again by explaining it from the beginning.
I think Andrew got it when he mentioned FontMetrics. The OP has a bunch
of stuff to render, and wants to discover how much space in pixels it
will take. The OP is hoping that some Java (Swing) class will do this
for him.
This is kinda similar to the JTextArea question we got earlier, where
the poster was hoping the JTextArea could expand and report to it's
parent the size of all the text it has. (JTextArea does the opposite:
it takes a size from it's parent and fits it's contents into that, with
scroll bars if needed.)
So yeah, Java could use a "render all this crud for me and tell me how
big it was" class. I don't know of one.
John B. Matthews - 30 Jun 2009 03:43 GMT
> > But it still isn't 100% clear that that is what you want. You might try
> > again by explaining it from the beginning.
[quoted text clipped - 12 lines]
> So yeah, Java could use a "render all this crud for me and tell me how
> big it was" class. I don't know of one.
As suggested, I typically use a Font's getStringBounds() or a
Graphics context's getFontMetrics().stringWidth(s) to position a
single line of text.
I see that LineBreakMeasurer can calculate a series of TextLayouts for
lines of styled text. The union of each TextLayout's getBounds() might
reasonably approximate the area required, without actually rendering
the text.
<http://java.sun.com/javase/6/docs/api/java/awt/font/LineBreakMeasurer.html>
<http://java.sun.com/javase/6/docs/api/java/awt/font/TextLayout.html>

Signature
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
Andrew Thompson - 29 Jun 2009 22:43 GMT
Please trim replies of text no longer immediately
relevant.
...
> > > I would like to be able to determine the bounds of the image in code, ...
> The image includes a lot of text.
See
<http://java.sun.com/javase/6/docs/api/java/awt/FontMetrics.html>
--
Andrew T.
pscode.org