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 / January 2005

Tip: Looking for answers? Try searching our database.

create an image out of a portion of a JPanel

Thread view: 
fahruz@hotmail.com - 12 Jan 2005 09:32 GMT
Hi,

This has been addressed to some extent in the past, save for the
following point:

how does one create an image from a specified area of a JPanel without
retrieving the whole graphics data?

The easy, straightforward way would be:

BufferedImage image = new BufferedImage(jpanel.getWidth(),
jpanel.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
paint(g2);
image = image.getSubimage(x, y, width, height);

which is highly inefficient if the panel is very large and all the
graphic data has to be copied to the BufferedImage before cropping.
Isn't there a direct way to paint only a specific area inside a
Graphics object?

Thx.
Andrey Kuznetsov - 12 Jan 2005 10:06 GMT
> This has been addressed to some extent in the past, save for the
> following point:
[quoted text clipped - 13 lines]
> Isn't there a direct way to paint only a specific area inside a
> Graphics object?

you can make following:

BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
g2.translate(x, y);
paint(g2);
g2.dispose();

if you need to make this often, then don't create image every time.

Signature

Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

fahruz@hotmail.com - 13 Jan 2005 03:06 GMT
Thanks for the answer Andrey but this doesn't work. It seems you're
trying to translate the coordinates of the target image's graphics
object whereas it should probably be the jpanel's. Maybe
Graphics2D g2p = (Graphics2D)jpanel.getGraphics();
g2p.translate(x,y);

But then, how to copy the rectangle portion of the jpanel's graphics
into the image's graphics?

I might also point out there was a small mistake in my code example. I
meant to call the jpanel's paint method

BufferedImage image = new BufferedImage(jpanel.getWidth(),
jpanel.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
jpanel.paint(g2); // instead of just paint(g2);
image = image.getSubimage(x, y, width, height);

Thx.
Andrew Thompson - 13 Jan 2005 08:26 GMT
> I might also point out there was a small mistake in my code example.

You might instead post an SSCCE, it will save time.
<http://www.physci.org/codes/sscce.jsp>

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

fahruz@hotmail.com - 14 Jan 2005 08:11 GMT
You might instead RTTBR (Read The Thread Before Replying), it will save
time. My code is not the problem here. We're looking for efficient ways
to address the issue described in the first question.
Andrew Thompson - 14 Jan 2005 10:12 GMT
> You might instead RTTBR (Read The Thread Before Replying), ..

Many things might happen.

You for example, might quote a little of what you are replying to,
as I have above - to provide context to the reader.  Threads from
earlier conversations drop off a lot of servers.

You might even (by use of effective communication) resolve the
technical issue.

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Andrey Kuznetsov - 14 Jan 2005 09:47 GMT
> Thanks for the answer Andrey but this doesn't work.
which result you got?

> It seems you're
> trying to translate the coordinates of the target image's graphics
> object whereas it should probably be the jpanel's. Maybe
> Graphics2D g2p = (Graphics2D)jpanel.getGraphics();
> g2p.translate(x,y);

surely this is the wrong way

> But then, how to copy the rectangle portion of the jpanel's graphics
> into the image's graphics?

you can't

> I might also point out there was a small mistake in my code example. I
> meant to call the jpanel's paint method
[quoted text clipped - 4 lines]
> jpanel.paint(g2); // instead of just paint(g2);
> image = image.getSubimage(x, y, width, height);

this is not the problem.

Signature

Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Andrey Kuznetsov - 14 Jan 2005 10:47 GMT
> BufferedImage image = new BufferedImage(width, height,
> BufferedImage.TYPE_INT_RGB);
> Graphics2D g2 = image.createGraphics();
> g2.translate(x, y);
> paint(g2);
> g2.dispose();

if it didn't worked, may be you should change this a little:
g2.translate(-x, -y);

Signature

Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities



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



©2008 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.