import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jvnet.substance.SubstanceLookAndFeel;
import org.jvnet.substance.utils.SubstanceConstants.ImageWatermarkKind;
import org.jvnet.substance.watermark.SubstanceImageWatermark;
/**
* Test application that shows the use of the
* {@link SubstanceLookAndFeel#getImageWatermarkOpacity()} and
* {@link SubstanceLookAndFeel#setImageWatermarkOpacity(float)} APIs.
*
* @author Kirill Grouchnikov
* @see SubstanceLookAndFeel#getImageWatermarkOpacity()
* @see SubstanceLookAndFeel#setImageWatermarkOpacity(float)
*/
public class GetSetImageWatermarkOpacity extends JFrame {
/**
* Creates the main frame for <code>this</code> sample.
*/
public GetSetImageWatermarkOpacity() {
super("Get / set image watermark opacity");
this.setLayout(new BorderLayout());
JPanel panel = new JPanel(new FlowLayout());
// create a slider to control the image opacity. The slider goes
// from 0 to 100, while the image opacity goes from 0.0 to 1.0.
final JSlider imageWatermarkOpacitySlider = new JSlider(0, 100,
(int) (100 * SubstanceLookAndFeel.getImageWatermarkOpacity()));
imageWatermarkOpacitySlider.setPaintLabels(true);
imageWatermarkOpacitySlider.setPaintTicks(true);
imageWatermarkOpacitySlider.setMajorTickSpacing(20);
imageWatermarkOpacitySlider.setMinorTickSpacing(5);
imageWatermarkOpacitySlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
// if the value is adjusting - ignore. This is done
// to make CPU usage better.
if (imageWatermarkOpacitySlider.getValueIsAdjusting())
return;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Set the image watermark opacity and repaint.
SubstanceLookAndFeel
.setImageWatermarkOpacity(imageWatermarkOpacitySlider
.getValue() / 100.0f);
repaint();
}
});
}
});
panel.add(imageWatermarkOpacitySlider);
this.add(panel, BorderLayout.CENTER);
JPanel controls = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton getImageWatermarkOpacity = new JButton(
"Get current image watermark opacity");
getImageWatermarkOpacity.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(GetSetImageWatermarkOpacity.this,
"Current image watermark opacity is "
+ SubstanceLookAndFeel
.getImageWatermarkOpacity());
}
});
controls.add(getImageWatermarkOpacity);
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());
SubstanceLookAndFeel.setCurrentWatermark(new SubstanceImageWatermark(
GetSetImageWatermarkOpacity.class
.getResourceAsStream("dukeplug.gif")));
SubstanceLookAndFeel.setImageWatermarkKind(ImageWatermarkKind.APP_TILE);
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GetSetImageWatermarkOpacity().setVisible(true);
}
});
}
}
The screenshot below shows the result of calling this API
for the default image watermark opacity (no call to
setImageWatermarkOpacity() has been made.
Note that the image watermark is 20% opaque:
The screenshot below shows the result of calling this API
after
setImageWatermarkOpacity() has been called
with 0.5. Note that the image watermark is much more opaque:
The screenshot below shows the result of calling this API
after
setImageWatermarkOpacity() has been called
with 0.06. Note that the image watermark is much less opaque:
|