O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

292
|
Chapter 8, Rendering
#56 Create a Magnifying Glass Component
HACK
Hacking the Hack
The only problem with the Robot approach is that it doesn’t limit itself to
capturing from the source component—it’ll take any onscreen pixels from
any application. That means that when you approach the edges of the
source component, you can pick up pixels from the desktop or other apps,
as seen in Figure 8-2.
Is this a problem? Some users will mind it, some won’t. To get rid of it,
you’ll need to smarten up
computeGrabRect( ) to bounds-check its right and
bottom edges (the top left is the cursor position, so it can never overshoot
the top or left of the component) by adding the untranslated point to the
proposed grab width and seeing if that is greater than the width of the
source component, and then doing the same for the height. This will take
some refactoring, since
computeGrabRect( ) doesn’t have access to the
untranslated point. Also, if you’re not necessarily going to be painting the
entire magnified component every time, you’ll have to erase it at the top of
paint( ), so you don’t have “crud” from the previous repaint left over after
this repaint.
Figure 8-2. Capturing pixels outside of the target component

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required