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

Slide Notes Out from the Taskbar #46
Chapter 6, Transparent and Animated Windows
|
245
HACK
Running the Hack
The SlideInNotification will take any JComponent as its contents. To make
things a little interesting, the
TestSlideInNotification class, shown in
Example 6-13, grabs an icon from the
JOptionPane class and makes a JLabel
of that and a little nonsense text.
public void setAnimatingHeight (int height) {
animatingSize.height = height;
setSize (animatingSize);
}
private void makeOffscreenImage(JComponent source) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment( );
GraphicsConfiguration gfxConfig =
ge.getDefaultScreenDevice().getDefaultConfiguration( );
offscreenImage =
gfxConfig.createCompatibleImage(source.getWidth( ),
source.getHeight( ));
Graphics2D offscreenGraphics =
(Graphics2D) offscreenImage.getGraphics( );
// windows workaround
offscreenGraphics.setColor (source.getBackground( ));
offscreenGraphics.fillRect (0, 0,
source.getWidth(), source.getHeight( ));
// paint from source to offscreen buffer
source.paint (offscreenGraphics);
}
public Dimension getPreferredSize( ) { return animatingSize; }
public Dimension getMinimumSize( ) { return animatingSize; }
public Dimension getMaximumSize( ) { return animatingSize; }
public void update (Graphics g) {
// override to eliminate flicker from
// unnecessary clear
paint (g);
}
public void paint (Graphics g) {
// get the top-most n pixels of source and
// paint them into g, where n is height
// (different from sheet example, which used bottom-most)
BufferedImage fragment =
offscreenImage.getSubimage (0,
0,
source.getWidth( ),
animatingSize.height);
g.drawImage (fragment, 0, 0, this);
}
}
Example 6-12. The AnimatingSheet inner class is used in creating notifications that slide in
and out (continued)
246
|
Chapter 6, Transparent and Animated Windows
#46 Slide Notes Out from the Taskbar
HACK
When you run this application, one thing to make note of is the standard
output because the
SlideInNotification class has one System.out.println( )
left in to show the discovered dimensions. Here’s what Windows reports
with a taskbar showing:
max window bounds = java.awt.Rectangle[x=0,y=0,width=800,height=570]
and what it reports with the taskbar set to auto-hide:
max window bounds = java.awt.Rectangle[x=0,y=0,width=800,height=600]
Meanwhile, on the Mac, the bounds with a dock on the bottom of the
screen look like this:
max window bounds = java.awt.Rectangle[x=0,y=22,width=1280,height=707]
and with the dock over on the right, they look like this:
max window bounds = java.awt.Rectangle[x=0,y=22,width=1244,height=746]
Notice that in each case, the first usable y-coordinate is 22, accounting for
the unusable space under the Mac’s monolithic menu bar. Notice also in the
second case that I’ve lost usable horizontal space because the dock is on the
right. By the way, if you’re doing the math and can’t figure out why nothing
adds up to 1024 × 768, it’s because I have a wide-screen monitor and my
screen size is 1280 × 768.
Of course, don’t stare too long at the console output, or you’ll miss the
appearance of the slide-in window. Figure 6-10 shows the window in mid-
animation on Windows, with and without a visible taskbar.
Example 6-13. Testing the slide-in notification
import javax.swing.*;
public class TestSlideInNotification {
public static void main (String[] args) {
Icon errorIcon = UIManager.getIcon ("OptionPane.errorIcon");
JLabel label = new JLabel ("Your application asplode",
errorIcon,
SwingConstants.LEFT);
SlideInNotification slider = new SlideInNotification (label);
slider.showAt (450);
}
}
Figure 6-10. Slide-in window on Windows with taskbar showing (left) and set to auto-
hide (right)

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