Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

java - Creating a dot/pixel outside of a JFrame, any location on the screen

I would like the be able to display points on the screen for my Java application. At some point I want to be able to let the user move the points and drag them around. Currently I am having trouble trying to draw a single dot/pixel on the screen outside of a JFrame.

Any ways to draw outside of your JFrame area?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You might create a full screen JFrame with full transparency, and draw to a panel or image on that. Much like this, which uses a custom panel to create a 'flashlight' effect for the mouse. We can see wherever the mouse is pointing, the rest of the screen is darkened.

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...