Sample code |
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import org.jvnet.substance.SubstanceLookAndFeel;
import org.jvnet.substance.painter.AlphaControlBackgroundComposite;
/**
* Test application that shows the use of the
* {@link SubstanceLookAndFeel#BACKGROUND_COMPOSITE} client property.
*
* @author Kirill Grouchnikov
* @see SubstanceLookAndFeel#BACKGROUND_COMPOSITE
*/
public class BackgroundComposite extends JFrame {
/**
* Creates the main frame for <code>this</code> sample.
*/
public BackgroundComposite() {
super("Background composite");
this.setLayout(new BorderLayout());
// Create panel with custom painting logic - simple
// diagonal fill.
JPanel samplePanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
Graphics2D graphics = (Graphics2D) g.create();
graphics
.setPaint(new GradientPaint(0, 0, new Color(100, 100,
255), getWidth(), getHeight(), new Color(255,
100, 100)));
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.dispose();
}
};
samplePanel.setPreferredSize(new Dimension(800, 400));
samplePanel.setSize(this.getPreferredSize());
samplePanel.setMinimumSize(this.getPreferredSize());
final JScrollPane scrollPane = new JScrollPane(samplePanel);
this.add(scrollPane, BorderLayout.CENTER);
JPanel controls = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton set30_50_Composite = new JButton("Set 30-50");
set30_50_Composite.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
scrollPane
.putClientProperty(
SubstanceLookAndFeel.BACKGROUND_COMPOSITE,
new AlphaControlBackgroundComposite(
0.3f, 0.5f));
repaint();
}
});
}
});
controls.add(set30_50_Composite);
JButton set50_70_Composite = new JButton("Set 50-70");
set50_70_Composite.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
scrollPane
.putClientProperty(
SubstanceLookAndFeel.BACKGROUND_COMPOSITE,
new AlphaControlBackgroundComposite(
0.5f, 0.5f));
repaint();
}
});
}
});
controls.add(set50_70_Composite);
JButton set50_100_Composite = new JButton("Set 50-100");
set50_100_Composite.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
scrollPane.putClientProperty(
SubstanceLookAndFeel.BACKGROUND_COMPOSITE,
new AlphaControlBackgroundComposite(0.5f));
repaint();
}
});
}
});
controls.add(set50_100_Composite);
JButton set70_70_Composite = new JButton("Set 70-70");
set70_70_Composite.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
scrollPane
.putClientProperty(
SubstanceLookAndFeel.BACKGROUND_COMPOSITE,
new AlphaControlBackgroundComposite(
0.7f, 0.7f));
repaint();
}
});
}
});
controls.add(set70_70_Composite);
final JCheckBox hasOverlay = new JCheckBox("has overlay");
hasOverlay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
scrollPane.putClientProperty(
SubstanceLookAndFeel.OVERLAY_PROPERTY,
hasOverlay.isSelected() ? Boolean.TRUE : null);
repaint();
}
});
}
});
controls.add(hasOverlay);
this.add(controls, BorderLayout.SOUTH);
this.setSize(400, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* The main method for <code>this</code> sample. The arguments are
* ignored.
*
* @param args
* Ignored.
* @throws Exception
* If some exception occured. Note that there is no special
* treatment of exception conditions in <code>this</code>
* sample code.
*/
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(new SubstanceLookAndFeel());
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new BackgroundComposite().setVisible(true);
}
});
}
}
Note - the screenshots below are taken after the "has overlay" checkbox is selected.
This sets the OVERLAY_PROPERTY on the scroll pane, making
the contents of the viewport partially visible through the scrollbars.
The initial state of the application (no custom background composite set on the
scroll pane). Note that even though the overlay property is set, the viewport contents
don't show through since the default composite is 100% opaque.
AlphaControlBackgroundComposite set with 50% opacity
in default state and 100% opacity in rollover / active state. Note that both scrollbars are in default
state, and the viewport contents show through (50% opacity).
The same background composite set and the mouse hovers over the right (increase) arrow button
of the horizontal scrollbar. Note that the scrollbar and the button are in active state and have
100% opacity (viewport is obscured).
|