使用java怎么编写一个包含水印的记事本程序?目前记事本功能已经实现,水印添加部分移植到记事本程序中之后,运行没有显示,各位指导一下。
以下是添加水印的部分代码
@Override
// public class MyPanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
// Get the size of the text area
int textAreaWidth = jf.getWidth();
int textAreaHeight = jf.getHeight();
// int textAreaWidth = 800;
// int textAreaHeight = 800;
// Create a new BufferedImage to hold the watermark and text
BufferedImage bufferedImage = new BufferedImage(textAreaWidth, textAreaHeight,
BufferedImage.TYPE_INT_ARGB);
// Draw the text onto the buffered image
Graphics2D g2d = bufferedImage.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setFont(watermarkFont);
g2d.setColor(watermarkColor);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
// 将文字倾斜30度
g2d.rotate(Math.toRadians(-30), jf.getWidth() / 2, jf.getHeight() / 2);
// g2d.rotate(Math.toRadians(-30), 800 / 2, 800 / 2);
g2d.drawString(watermarkText, 20, textAreaHeight - 210);
// Draw the text area onto the buffered image
jf.print(g2d);
// Draw the buffered image onto the main window
g.drawImage(bufferedImage, 100, 100, null);
// 释放资源
// g2d.dispose();
}