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.color.ColorScheme;
import org.jvnet.substance.theme.*;
/**
* Test application that shows the use of the
* {@link SubstanceLookAndFeel#getDefaultColorScheme()} API.
*
* @author Kirill Grouchnikov
* @see SubstanceLookAndFeel#getDefaultColorScheme()
*/
public class GetDefaultColorScheme extends JFrame {
/**
* Creates the main frame for <code>this</code> sample.
*/
public GetDefaultColorScheme() {
super("Get default color scheme");
this.setLayout(new BorderLayout());
final JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
int width = getWidth();
int height = getHeight();
// paint the panel with the colors of the current
// default color scheme
ColorScheme scheme = SubstanceLookAndFeel
.getDefaultColorScheme();
Paint paint = new GradientPaint(0, 0, scheme
.getUltraLightColor(), width, height, scheme
.getLightColor());
Graphics2D g2 = (Graphics2D) g.create();
g2.setPaint(paint);
g2.fillRect(0, 0, width, height);
g2.dispose();
}
};
this.add(panel, BorderLayout.CENTER);
JPanel controls = new JPanel(new FlowLayout(FlowLayout.RIGHT));
final JButton setAqua = new JButton("Set Aqua");
setAqua.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Set Aqua theme
SubstanceLookAndFeel
.setCurrentTheme(new SubstanceAquaTheme());
SwingUtilities
.updateComponentTreeUI(GetDefaultColorScheme.this);
}
});
}
});
controls.add(setAqua);
final JButton setUltramarine = new JButton("Set Ultramarine");
setUltramarine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Set Ultramarine theme
SubstanceLookAndFeel
.setCurrentTheme(new SubstanceUltramarineTheme());
SwingUtilities
.updateComponentTreeUI(GetDefaultColorScheme.this);
}
});
}
});
controls.add(setUltramarine);
this.add(controls, BorderLayout.SOUTH);
// register theme change listener to repaint the panel on theme change
SubstanceLookAndFeel
.registerThemeChangeListener(new ThemeChangeListener() {
public void themeChanged() {
panel.repaint();
}
});
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 GetDefaultColorScheme().setVisible(true);
}
});
}
}
The screenshot below shows application frame with main panel colored
with the default color scheme of default theme (Aqua):
The screenshot below shows application frame with main panel colored
with the default color scheme of the currently
set theme (Ultramarine):
|