//package paint;
/*
* WatermarkSimulator.java
* [url]http://today.java.net/pub/a/today/2005/07/12/visualizations.html?page=2[/url]
*/
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class WatermarkSimulator {
public static void main(String[] args) {
BufferedImage image = null;
JFrame frame = new JFrame("WatermarkSimulator");
frame.setSize(new Dimension(510, 450));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.getContentPane().setLayout(new GridBagLayout());
frame.getContentPane().add(new JButton("Hello"));
JWatermark.createWatermark(frame, "Development");
frame.setVisible(true);
}
}
class JWatermark extends JComponent{
private static final float OPACITY = 0.15f;
private static final double ROTATION = -(Math.PI / 4);
private Font font = UIManager.getFont("Label.font");
private String text = "";
public static Integer WATER_MARK_LAYER
= new Integer(JLayeredPane.FRAME_CONTENT_LAYER.intValue()
+ 1);
public JWatermark(String text) {
this.text = text;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Window window = SwingUtilities.getWindowAncestor(this);
Rectangle viewRect = window.getBounds();
int halfWidth = viewRect.width / 2;
int halfHeight = viewRect.height / 2;
Graphics2D graphics2D = (Graphics2D) g;
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, OPACITY);
graphics2D.setComposite(ac);
final int minSide = Math.min(viewRect.width, viewRect.height);
font = font.deriveFont((float)( minSide / 7));
graphics2D.setFont(font);
Rectangle textRect = new Rectangle();
graphics2D.setFont(font);
graphics2D.rotate(ROTATION, halfWidth, halfHeight);
SwingUtilities.layoutCompoundLabel(this,
graphics2D.getFontMetrics(), text, null,
SwingConstants.CENTER, SwingConstants.CENTER,
SwingConstants.CENTER, SwingConstants.CENTER,
viewRect, new Rectangle(), textRect,
0);
graphics2D.setColor(getForeground());
int string_x = halfWidth - (int) (textRect.width / 2);
int y = halfHeight + textRect.height / 7;
graphics2D.drawString(text, string_x, y);
}
public static void createWatermark(JFrame frame, String text){
JWatermark watermark = new JWatermark(text);
watermark.setOpaque(false);
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
watermark.setBounds(0, 0,
(int)screenSize.getWidth(),
(int)screenSize.getHeight());
watermark.setVisible(true);
watermark.setForeground( Color.BLUE );
final JLayeredPane jLayeredPane = frame.getLayeredPane();
jLayeredPane.add(watermark, WATER_MARK_LAYER, 0);
}
}