28.12.2012 Views

Core JFC Java Foundation Classes 2nd edition - Read

Core JFC Java Foundation Classes 2nd edition - Read

Core JFC Java Foundation Classes 2nd edition - Read

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Logical Scrolling and Forcing Visibility of an Area 577<br />

Listing 5-19 A custom scroll pane that can handle logical<br />

scrolling (continued)<br />

}<br />

private int [] positions;<br />

private JLabel[] labels; // Pictures, one per JLabel<br />

private int picturesPerRow; // Pictures in each row<br />

private int rows; // Number of rows<br />

correct orientation. The makeVisible method helps to provide the functionality<br />

offered by the button panel on the main frame, as you'll see later.<br />

The rest of this class is the implementation of the PictureScrollBar<br />

class, which is derived from JScrollBar and overrides the getUnitIncrement<br />

and getBlocklncrement methods to provide the logical scrolling.<br />

The getUnit Increment method is trivial—it just returns 1, causing the<br />

viewport to move by f pixel in the appropriate direction. All of the real work<br />

of this class is in getBlocklncrement. You've already seen a description of<br />

how this method works in principle. Since this class could manage either a<br />

vertical or a horizontal scrollbar, there are two slightly different pieces of<br />

code, in the getlmagePositions method, that achieve the same thing in a<br />

slightly different way. In the case of a horizontal scrollbar, the x coordinates<br />

of the picture columns are found by invoking getLocation on the three pictures<br />

on the top row; since the pictures are arranged in columns, this returns<br />

the x coordinates of all the pictures. Similarly, the vertical scrollbar finds the y<br />

locations of the pictures in the left-hand column.<br />

Since the pictures won't move once they have been placed on the panel,<br />

the getBlocklncrement method only invokes getlmagePositions the<br />

first time it is called, saving the resulting array of offsets in the positions<br />

array for future use. Using this array and the current value of the scrollbar,<br />

obtained using getValue, the index of the picture that occupies the top-left<br />

corner of the viewport can be located. As described earlier, it is then a simple<br />

matter to locate the picture that should next be made visible and, with that<br />

information and the values in the positions array, the distance by which<br />

the scrollbar should be moved can be calculated with a simple subtraction.<br />

Forcing Visibility of Part of a Scrolled Area<br />

The last piece of the PictureScrollPane class is the makeVisible<br />

method. When the user presses one of the nine buttons at the top right of the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!