直接在屏幕上绘图

时间:2010-06-15 17:07:20

标签: java windows-xp

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

2 个答案:

答案 0 :(得分:4)

是的,这是可能的。但这不是一项微不足道的任务。

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

请见thisthis link*了解半透明&形状的窗户

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

您可能需要JNAJNI来创建这些窗口/效果,还需要提供“点击”功能。

*需要Java 6u10。您基本上可以在框架中绘制任何想要内部的内容,设置链接线程中提到的属性以及未绘制的任何位置(或绘制透明像素),桌面将可见。

(我考虑使用与Java不同的语言/框架,因为桌面小部件在Java中实现起来并不容易[甚至可能是JavaFX?]

答案 1 :(得分:1)

我有一个非常奇怪的程序。有一个班级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();
    };
}

创建DirectPaintingDevice,并使用device.graphics...使用Graphics2D对象绘制内容。然后,使用device.update();更新窗口。它创建了许多矩形JFrame。

但是,请注意,此代码 非常 缓慢且滞后。