Java 直接在屏幕上绘图

Java 直接在屏幕上绘图,java,windows-xp,Java,Windows Xp,在Java中,是否可以直接在屏幕上绘制形状和内容,使桌面保持可见,并可以单击形状下方的任何内容?我正在使用Windows XP。谢谢。是的,这是可能的。但这不是一件小事 如果您正在寻找“桌面小部件”: 有关半透明和成型窗口,请参见和 有关更多实验GUI信息,请参阅 您可能需要或创建这些窗口/效果,但也需要提供“点击”功能 *需要Java 6u10。基本上,你可以在一个框架内画出你想要的任何东西,设置线程链接中提到的属性,在你没有画的任何地方(或画出透明像素),桌面都是可见的 (我会考虑使用不同于

在Java中,是否可以直接在屏幕上绘制形状和内容,使桌面保持可见,并可以单击形状下方的任何内容?我正在使用Windows XP。谢谢。

是的,这是可能的。但这不是一件小事

如果您正在寻找“桌面小部件”:

有关半透明和成型窗口,请参见和

有关更多实验GUI信息,请参阅

您可能需要或创建这些窗口/效果,但也需要提供“点击”功能

*需要Java 6u10。基本上,你可以在一个框架内画出你想要的任何东西,设置线程链接中提到的属性,在你没有画的任何地方(或画出透明像素),桌面都是可见的


(我会考虑使用不同于java的语言/框架,因为桌面小部件在java中很容易实现(也许甚至JavaFX?))

< P>我有一个非常奇怪的程序。有一个类,
DirectPaintingDevice
。在这里;代码下面有解释:

package direct_painting_device;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.function.Consumer;

import javax.swing.JFrame;
import javax.swing.JPanel;

public final class DirectPaintingDevice {
    public final Graphics2D graphics;
    private BufferedImage image;
    private final ArrayList<JFrame> frames = new ArrayList<>(0);
    private final ArrayList<Runnable> actions = new ArrayList<>(0);

    public DirectPaintingDevice() {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        this.image = new BufferedImage(screen.width, screen.height,
                BufferedImage.TYPE_INT_ARGB);
        this.graphics = this.image.createGraphics();
        Color color = this.graphics.getColor();
        this.graphics.setColor(new Color(0, true));
        this.graphics.fillRect(0, 0, screen.width, screen.height);
        this.graphics.setColor(color);
    }

    public synchronized void update() {
        this.frames.forEach(JFrame::dispose);
        this.frames.clear();
        ArrayList<Point> done_points = new ArrayList<>(0);
        for (int x = 0; x < this.image.getWidth(); x++) {
            for (int y = 0; y < this.image.getHeight(); y++) {
                if (this.image.getRGB(x, y) >> 24 == 0
                        || done_points.contains(new Point(x, y))) {
                    continue;
                }
                final int cx = x;
                final int cy = y;
                int w;
                for (w = 0; w < this.image.getWidth() - x
                        && this.image.getRGB(x + w, y) >> 24 == this.image
                                .getRGB(x, y) >> 24; w++) {
                    // Empty loop
                }
                int h;
                outer: for (h = 0; h < this.image.getHeight() - y; h++) {
                    for (int cw = 0; cw < w; cw++) {
                        if (this.image.getRGB(x + cw, h + y) >> 24 != this.image
                                .getRGB(x, y) >> 24) {
                            break outer;
                        }
                    }
                    for (int i = 0; i < w; i++) {
                        done_points.add(new Point(i + x, h + y));
                    }
                }
                final int cw = w;
                final int ch = h;
                JFrame frame = new JFrame();
                frame.setUndecorated(true);
                frame.setDefaultCloseOperation(0);
                frame.setSize(w, h);
                frame.setLocation(x, y);
                frame.setOpacity((int) (1 - ((double) (this.image.getRGB(x, y) >> 24) / 505)));
                frame.toFront();
                frame.setAlwaysOnTop(true);
                frame.add(new JPanel() {
                    private static final long serialVersionUID = 6246612303574731899L;

                    public void paintComponent(Graphics g) {
                        g.drawImage(DirectPaintingDevice.this.image
                                .getSubimage(cx, cy, cw, ch), 0, 0, null);
                    }
                });
                frame.setVisible(true);
                System.out.println("Created Frame : [" + x + ", " + y + ", "
                        + w + ", " + h + "];");
                this.frames.add(frame);
            }
        }
        this.actions.forEach(Runnable::run);
    }

    public synchronized void allFrames(Consumer<JFrame> action) {
        synchronized (this) {
            this.frames.forEach(action);
        }
    }

    public synchronized void exec(Consumer<Graphics2D> action) {
        new Thread(() -> {
            synchronized (DirectPaintingDevice.this) {
                action.accept(DirectPaintingDevice.this.graphics);
            }
        }).start();
    }

    public void onUpdate(Runnable action) {
        this.actions.add(action);
    }

    public synchronized void setImage(BufferedImage image) {
        this.image = image;
    }

    public static void main(String... args) throws IOException {
        DirectPaintingDevice device = new DirectPaintingDevice();
        device.graphics.setColor(Color.BLACK);
        device.graphics.fillRect(0, 0, 50, 50);
        device.graphics.fillRect(0, 50, 50, 50);
        device.graphics.fillRect(0, 100, 50, 50);
        device.graphics.fillRect(50, 0, 50, 50);
        device.graphics.fillRect(50, 100, 50, 50);
        device.graphics.fillRect(100, 0, 50, 50);
        device.graphics.fillRect(100, 50, 50, 50);
        device.graphics.fillRect(100, 100, 50, 50);
        device.graphics.fillRect(0, 70, 100, 10);
        device.graphics.fillRect(70, 0, 10, 100);
        device.update();
    };
}
包装直接喷漆装置;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.Point;
导入java.awt.Toolkit;
导入java.awt.image.buffereImage;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.function.Consumer;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共最终类DirectPaintingDevice{
公共最终图形2D图形;
私有缓冲图像;
私有最终ArrayList帧=新ArrayList(0);
私有最终ArrayList操作=新建ArrayList(0);
public directingpaintingdevice(){
维度屏幕=Toolkit.getDefaultToolkit().getScreenSize();
this.image=新的缓冲区图像(screen.width、screen.height、,
BuffereImage.TYPE_INT_ARGB);
this.graphics=this.image.createGraphics();
Color Color=this.graphics.getColor();
this.graphics.setColor(新颜色(0,true));
this.graphics.fillRect(0,0,screen.width,screen.height);
this.graphics.setColor(颜色);
}
公共同步的无效更新(){
this.frames.forEach(JFrame::dispose);
this.frames.clear();
ArrayList done_points=新的ArrayList(0);
对于(int x=0;x>24==0
||完成点。包含(新点(x,y))){
继续;
}
最终int cx=x;
最终整数cy=y;
int w;
对于(w=0;w>24==this.image
.getRGB(x,y)>>24;w++){
//空循环
}
int-h;
外部:用于(h=0;h>24!=this.image
.getRGB(x,y)>>24){
打破外部;
}
}
对于(int i=0;i>24)/505));
frame.toFront();
frame.setAlwaysOnTop(真);
frame.add(新的JPanel(){
私有静态最终长serialVersionUID=6246612303574731899L;
公共组件(图形g){
g、 drawImage(DirectPaintingDevice.this.image
.getSubimage(cx、cy、cw、ch)、0、0、null);
}
});
frame.setVisible(true);
System.out.println(“创建的帧:[“+x+”,“+y+”,”
+w+“,“+h+”];”;
this.frames.add(frame);
}
}
this.actions.forEach(Runnable::run);
}
公共同步作废所有帧(消费者操作){
已同步(此){
这个.frames.forEach(动作);
}
}
公共同步作废执行官(消费者行动){
新线程(()->{
已同步(DirectPaintingDevice.this){
接受(DirectPaintingDevice.this.graphics);
}
}).start();
}
公共void onUpdate(可运行操作){
this.actions.add(action);
}
公共同步的void setImage(BuffereImage映像){
这个图像=图像;
}
公共静态void main(字符串…参数)引发IOException{
DirectPaintingDevice=新的DirectPaintingDevice();
设备。图形。设置颜色(颜色。黑色);
设备.graphics.fillRect(0,0,50,50);
设备.图形.fillRect(0,50,50,50);
设备.图形.fillRect(0,100,50,50);
设备.图形.fillRect(50,0,50,50);
设备.图形.fillRect(50,100,50,50);
设备.图形.fillRect(100,0,50,50);
设备.图形.fillRect(100,50,50,50);
设备.图形.fillRect(100,100,50,50);
设备.图形.fillRect(0,70,100,10);
设备.图形.fillRect(70,0,10100);
device.update();
};
}
创建一个
DirectPaintingDevice
,并使用
device.graphics…
使用
Graphics2D绘制东西