一些软件将显示器用作清晰的白板进行绘图。白板是透明的,在透明白板下面可以看到桌面
那么这怎么可能呢?
我的意思是在 Java 中是否像将 JFrame 设置为屏幕大小并使其透明地绘制或者这是如何完成的?
我们有类似的要求,所以我们试图了解如何做到这一点?
这是一种方法:创建一个透明窗口,在其上放置一个JLabel
显示BufferedImage
. 当用户按下并拖动鼠标时,您在 BufferedImage 上绘制并重新绘制标签以更新内容。
一些示例代码开始:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class TestTransparentFrame extends JLabel {
private boolean drawing = false;
private Point lastPoint;
private BufferedImage image;
public TestTransparentFrame() {
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
updateImage();
}
});
MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
drawing = true;
lastPoint = e.getPoint();
}
@Override
public void mouseReleased(MouseEvent e) {
drawing = false;
lastPoint = null;
}
@Override
public void mouseExited(MouseEvent e) {
drawing = false;
lastPoint = null;
}
@Override
public void mouseDragged(MouseEvent e) {
if (drawing && lastPoint != null) {
Graphics graphics = image.getGraphics();
graphics.setColor(Color.RED);
((Graphics2D) graphics).setStroke(new BasicStroke(5.0f));
graphics.drawLine(lastPoint.x, lastPoint.y, e.getX(), e.getY());
repaint();
lastPoint = e.getPoint();
}
}
};
addMouseListener(mouseAdapter);
addMouseMotionListener(mouseAdapter);
}
protected void updateImage() {
if (image == null || image.getWidth() < getWidth() || image.getHeight() < getHeight()) {
if (image == null) {
image = getGraphicsConfiguration().createCompatibleImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
} else {
BufferedImage newImage = getGraphicsConfiguration().createCompatibleImage(getWidth(), getHeight(),
BufferedImage.TYPE_INT_ARGB);
newImage.getGraphics().drawImage(image, 0, 0, null);
image = newImage;
}
} else {
image = image.getSubimage(0, 0, getWidth(), getHeight());
}
setIcon(new ImageIcon(image));
}
protected void initUI() {
JFrame frame = new JFrame(TestTransparentFrame.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setBackground(new Color(255, 255, 255, 1));
TestTransparentFrame comp = new TestTransparentFrame();
frame.setContentPane(comp);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestTransparentFrame().initUI();
}
});
}
}
结果: