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 2004

Tip: Looking for answers? Try searching our database.

Copy rectangular region from one graphics object to another?

Thread view: 
Reinhard Engels - 09 Jan 2004 17:20 GMT
I would like to enable users of my application to drag to select a
rectangular region of a component, and then copy an image of that
region to the system clipboard. The clipboard stuff seems to work fine
(on osx at least), despite the troubles I'd anticipated from browsing
usenet. I can select rectangle sized regions in my app and paste them
into ms-word. The images are the right size, but are blank, or contain
just a silly debugging note from myself.  I can't figure out what
seems to me (perhaps naively) like the easy part: how to actually
*copy* the graphics area to the image. I'm hoping I'd just being an
idiot and somewill that someone in this group will (gently) enlighten
me. It seems to me like a single line of code could do it, but I've
stared at the Graphics javadoc page in vain for an embarrassingly long
time already, and no luck...

// setup. works fine.
Toolkit toolkit =    Toolkit.getDefaultToolkit();
Clipboard clipboard = toolkit.getSystemClipboard();
Rectangle r = getDragRectangle();

// this creates an image that is the right size, but which is blank.
Image image = m_window.getPanel().createImage((int)r.getWidth(),(int)r.getHeight());

// here's the part that needs fixing.
Graphics g1 = m_window.getPanel().getGraphics();
Graphics g2 = image.getGraphics();
g2.setColor(Color.RED);
g2.drawString(
  "If only I could see the r region of g1 here instead of this
string...",
  10,10);

// This works. The clipboard successfully gets the image.
// TransferableImage is just a stupid simple
// implementation of Transferable which
// I won't bother posting.
clipboard.setContents(new TransferableImage(image),null);

Thanks in advance,

Reinhard

http://www.nosdiet.com/
ak - 09 Jan 2004 17:48 GMT
BufferedImage bi = new BufferedImage(r.width, r.height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.getGraphics();
g.setTransform(AffineTransform.getTranslateInstance(-r.x, -r.y);
m_window.getPanel().paint(g);

____________

http://reader.imagero.com the best java image reader.
Reinhard Engels - 12 Jan 2004 18:27 GMT
Thanks! That worked perfectly. Glad it was indeed that simple.

Reinhard

http://www.nosdiet.com

> BufferedImage bi = new BufferedImage(r.width, r.height,
> BufferedImage.TYPE_INT_ARGB);
[quoted text clipped - 5 lines]
>
> http://reader.imagero.com the best java image reader.


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.