gpt4 book ai didi

java - 使用桌面作为透明白板

转载 作者:行者123 更新时间:2023-11-30 07:16:17 25 4
gpt4 key购买 nike

某些软件将显示器用作清晰的白板进行绘图。白板是透明的,透明白板下可以看到桌面

这怎么可能呢?

我的意思是在 Java 中是否像将 JFrame 设置为屏幕大小并使其透明以进行绘制,或者这是如何完成的?

我们有类似的要求,所以我们正在尝试了解如何做到这一点?

最佳答案

这是一种实现方法:创建一个透明窗口,在其上放置一个显示 BufferedImageJLabel。当用户按下并拖动鼠标时,您在 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();
}
});
}

}

结果:

enter image description here

关于java - 使用桌面作为透明白板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17152163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com