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

Get Swing Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.