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.