The following topics are covered:
- JLayer Class
- Nimbus Look & Feel
- Heavyweight and Lightweight Components
- Shaped and Translucent Windows
- Hue-Saturation-Luminance (HSL) Color Selection in JColorChooser Class
JLayer Class
The
JLayer
class is a flexible and powerful decorator for Swing components. It enables you to draw on components and respond to component events without modifying the underlying component directly.Example of JLayer class:--
Drawing on Components
To use the
JLayer
class, you need a good LayerUI
subclass. The simplest kinds of LayerUI
classes change how components are drawn. Here is one, for example, that paints a transparent color gradient on a component.class WallpaperLayerUI extends LayerUI<JComponent> { @Override public void paint(Graphics g, JComponent c) { super.paint(g, c); Graphics2D g2 = (Graphics2D) g.create(); int w = c.getWidth(); int h = c.getHeight(); g2.setComposite(AlphaComposite.getInstance( AlphaComposite.SRC_OVER, .5f)); g2.setPaint(new GradientPaint(0, 0, Color.yellow, 0, h, Color.red)); g2.fillRect(0, 0, w, h); g2.dispose(); } }
Thepaint()
method is where the custom drawing takes place. The call to thesuper.paint()
method draws the contents of theJPanel
object. After setting up a 50% transparent composite, the color gradient is drawn. After theLayerUI
subclass is defined, using it is simple. Here is some source code that uses theWallpaperLayerUI
class:Here is the result:import java.awt.*; import javax.swing.*; import javax.swing.plaf.LayerUI; public class Wallpaper { public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createUI(); } }); } public static void createUI() { JFrame f = new JFrame("Wallpaper"); JPanel panel = createPanel(); LayerUI<JComponent> layerUI = new WallpaperLayerUI(); JLayer<JComponent> jlayer = new JLayer<JComponent>(panel, layerUI); f.add (jlayer); f.setSize(300, 200); f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); f.setLocationRelativeTo (null); f.setVisible (true); } private static JPanel createPanel() { JPanel p = new JPanel(); ButtonGroup entreeGroup = new ButtonGroup(); JRadioButton radioButton; p.add(radioButton = new JRadioButton("Beef", true)); entreeGroup.add(radioButton); p.add(radioButton = new JRadioButton("Chicken")); entreeGroup.add(radioButton); p.add(radioButton = new JRadioButton("Vegetable")); entreeGroup.add(radioButton); p.add(new JCheckBox("Ketchup")); p.add(new JCheckBox("Mustard")); p.add(new JCheckBox("Pickles")); p.add(new JLabel("Special requests:")); p.add(new JTextField(20)); JButton orderButton = new JButton("Place Order"); p.add(orderButton); return p; } }
Nimbus Look & Feel
The Nimbus Look & Feel (L&F) has moved from com.sun.java.swing
to a standard API namespace, javax.swing
; see the javax.swing.plaf.nimbus
package for more information. Although it is not the default L&F, you can easily use it. Consult the Nimbus Look and Feel section in the Java Tutorial for more information and examples of three simple methods for using Nimbus in your applications.Heavyweight and Lightweight Components
Historically, mixing heavyweight (AWT) and lightweight (Swing) components in the same container has been problematic. However, mixing heavyweight and lightweight components is easy to accomplish in Java SE 7. The Mixing Heavyweight and Lightweight Components article shows you how.Shaped and Translucent Windows
The Java SE 7 release supports windows with transparency and non-rectangular shapes. See How to Create Translucent and Shaped Windows, part of the Java Tutorial.Hue-Saturation-Luminance (HSL) Color Selection in JColorChooser Class
An HSV tab has been added to the JColorChooser
class, which allows users to select colors using the Hue-Saturation-Luminance (HSL) color model.
No comments:
Post a Comment