Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / June 2009

Tip: Looking for answers? Try searching our database.

Determining image bounds of image created in code

Thread view: 
Aaron - 29 Jun 2009 12:28 GMT
I have written code which creates an image on a JPanel (inside a
JFrame) and saves it as a jpeg.
Each image is a different size, so I need to manually resize the
JFrame to the bounds of the image before saving (else the whole image
isn't saved).
I would like to be able to determine the bounds of the image in code,
so this manual resizing is not needed.
How would I go about acheiving this?
Thanks in Advance.
Andrew Thompson - 29 Jun 2009 13:04 GMT
> I have written code which creates an image..
> I would like to be able to determine the bounds of the image ...

Given you created the Image, I cannot understand
how you cannot know its bounds.

Can you prepare an SSCCE* that sums up your current
code?  * <http://pscode.org/sscce.html>

--
Andrew T.
pscode.org
Jarrick Chagma - 29 Jun 2009 13:08 GMT
> I have written code which creates an image on a JPanel (inside a
> JFrame) and saves it as a jpeg.
[quoted text clipped - 5 lines]
> How would I go about acheiving this?
> Thanks in Advance.

I am a bit confused.  All images implement getWidth() and getHeight()
methods.  Is there a reason why you cannot use those?

Signature

Regards,

Jarrick

------------------------------------
Jarrick.Chagma@your.mind.gmail.com
(Lose your mind before emailing me)

Aaron - 29 Jun 2009 22:01 GMT
> > 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.