java - 在 JFrame 之外的屏幕上的任何位置创建点/像素

标签 java swing awt fullscreen

我希望能够在我的 Java 应用程序的屏幕上显示点。在某些时候,我希望能够让用户移动点并将它们拖来拖去。目前我在尝试在 JFrame 之外的屏幕上绘制单个点/像素时遇到问题。

有什么方法可以在 JFrame 区域之外绘制?

最佳答案

您可以创建一个具有完全透明度的全屏 JFrame,然后在其上绘制面板或图像。很像这个,它使用自定义面板为鼠标创建“手电筒”效果。我们可以看到鼠标指向的地方,屏幕的其余部分变暗了。

Translucent Window Test

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Point2D;
import javax.swing.*;
import javax.swing.event.*;

public class TranslucentWindowTest {

    public static void main(String[] args) {
        // Determine if the GraphicsDevice supports translucency.
        GraphicsEnvironment ge =
                GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();

        // If translucent windows aren't supported, exit.
        if (
                !gd.isWindowTranslucencySupported(
                GraphicsDevice.WindowTranslucency.TRANSLUCENT) ||
                !gd.isWindowTranslucencySupported(
                GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT)
                ) {
            System.err.println(
                    "Translucency is not fully supported");
            System.exit(1);
        }

        Runnable r = new Runnable() {

            @Override
            public void run() {

                final JFrame f = new JFrame();
                f.setUndecorated(true);
                f.setAlwaysOnTop(true);
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setExtendedState(JFrame.MAXIMIZED_BOTH);
                // first set the frame invisible..
                f.setBackground(new Color(0, 0, 0, 0));

                final SpinnerNumberModel snm = new 
                        SpinnerNumberModel(98, 0, 100, 1);

                // create a custom panel to create moment by moment 
                // screen painting
                JPanel panel = new JPanel() {

                    float[] fractions = {.2f,.8f};
                    Color[] colors = {
                        new Color(255,255,255,0),
                        new Color(0,0,0,250)
                    };

                    @Override
                    protected void paintComponent(Graphics g) {
                        Point2D pnt = MouseInfo.getPointerInfo().getLocation();
                        if (g instanceof Graphics2D) {
                            colors[1] = new Color(
                                    0f,0f,0f,snm.getNumber().floatValue()/100f);

                            Graphics2D g2d = (Graphics2D) g;
                            Paint p = new RadialGradientPaint(
                                    pnt, 200f, fractions, colors,  
                                    MultipleGradientPaint.CycleMethod.NO_CYCLE);
                            g2d.setPaint(p);
                            g2d.fillRect(0, 0, getWidth(), getHeight());
                        }
                    }
                };
                f.setContentPane(panel);

                ActionListener mouseAnimate = new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        f.repaint();
                    }
                };
                Timer timer = new Timer(50,mouseAnimate);
                f.pack();

                JPanel spinners = new JPanel(
                        new FlowLayout(5, 5, FlowLayout.CENTER));

                f.setOpacity(snm.getNumber().intValue() / 100f);
                JSpinner translucency = new JSpinner(snm);
                spinners.add(new JLabel("Translucency"));
                spinners.add(translucency);
                ChangeListener trListener = new ChangeListener() {

                    @Override
                    public void stateChanged(ChangeEvent e) {
                        f.setOpacity(snm.getNumber().intValue() / 100f);
                    }
                };
                translucency.addChangeListener(trListener);

                f.setVisible(true);

                // run timer as long as the dialog is open..
                timer.start();
                JOptionPane.showMessageDialog(f, spinners);
                timer.stop();

                f.setVisible(false);
                f.dispose();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

关于java - 在 JFrame 之外的屏幕上的任何位置创建点/像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18244769/

相关文章:

Java开源帮助台+工作流项目

java - Java 中的内存问题/错误

java - Android 抛出 DeadObjectException : Transaction failed on small parcel; remote process probably died just in HUAWEI Android 8. 0.0

java - 图表/可视化java工具

java - 使用调度程序的多个按键

java - 将值从 jsp 页面传递到另一个页面时出错

java - 方法在简单的 JFrame 中返回 null

java - 如何将控制台的输出显示到 JTextArea

java - 当 Toolkit.getDefaultToolkit() 抛出 AWTError 时,如何在 Java 中读取图像?

java - 碰撞检查方法适用于 Mac,但不适用于 PC