Create a disappearing JPanel?

白昼怎懂夜的黑 提交于 2019-12-12 03:14:19

问题


I am trying to create an extended JPanel that acts as a way to highlight some region of the screen. I have taken some code from this SO answer but would like to extend it further though I am not sure how to go about it.

I would like to be able to have my JPanel (MatchAreaPanel below) disappear after a given timeout is reached. That is the JPanel sets its visible property to false and subsequently disposes of itself.

What would be the best way to go about doing this?

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

public class MatchAreaPanel extends JPanel
{
    public MatchAreaPanel()
    {

    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setColor(new Color(128, 128, 128, 64));
        g2d.fillRect(0, 0, getWidth(), getHeight());

        float dash1[] = {10.0f};
        BasicStroke dashed = new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
        g2d.setColor(Color.BLACK);
        g2d.setStroke(dashed);
        g2d.drawRect(0, 0, getWidth() - 3, getHeight() - 3);
        g2d.dispose();
    }
}

回答1:


You could use a Swing Timer to simply schedule a callback after a given delay and close the associated window or hide the component based on your needs, for example...

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                Rectangle bounds = getVirtualBounds();
                Random rnd = new Random();
                int x = bounds.x + (rnd.nextInt(bounds.width) - 100);
                int y = bounds.y + (rnd.nextInt(bounds.height) - 100);

                MatchAreaPanel pane = new MatchAreaPanel();
                JWindow frame = new JWindow();
                frame.setBackground(new Color(0, 0, 0, 0));
                frame.add(pane);
                frame.setBounds(x, y, 100, 100);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                pane.start();
            }
        });
    }

    public static Rectangle getVirtualBounds() {

        Rectangle bounds = new Rectangle(0, 0, 0, 0);

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        bounds.add(gd.getDefaultConfiguration().getBounds());

        return bounds;

    }

    public class MatchAreaPanel extends JPanel {

        public MatchAreaPanel() {
            setOpaque(false);
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    SwingUtilities.windowForComponent(MatchAreaPanel.this).dispose();
                }
            });
        }

        public void start() {
            Timer timer = new Timer(5000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    SwingUtilities.windowForComponent(MatchAreaPanel.this).dispose();
                }
            });
            timer.start();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(new Color(128, 128, 128, 64));
            g2d.fillRect(0, 0, getWidth(), getHeight());

            float dash1[] = {10.0f};
            BasicStroke dashed = new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
            g2d.setColor(Color.BLACK);
            g2d.setStroke(dashed);
            g2d.drawRect(0, 0, getWidth() - 3, getHeight() - 3);
            g2d.dispose();
        }
    }

}

See How to use Swing Timers for more details

Updated...

Now, simply "hiding" the panel is, boring, it's also possible for the user to miss the panel, as suddenly showing up is no guarantee that the user will see it, so instead, you could add in a fade out effect.

In this example, you can fade the panel out by clicking it (but I did this as part of my testing, so you don't need it) or after the specified time out...

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                Rectangle bounds = getVirtualBounds();
                Random rnd = new Random();
                int x = bounds.x + (rnd.nextInt(bounds.width) - 100);
                int y = bounds.y + (rnd.nextInt(bounds.height) - 100);

                MatchAreaPanel pane = new MatchAreaPanel();
                JWindow frame = new JWindow();
                frame.setBackground(new Color(0, 0, 0, 0));
                frame.add(pane);
                frame.setBounds(x, y, 100, 100);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                pane.start();
            }
        });
    }

    public static Rectangle getVirtualBounds() {

        Rectangle bounds = new Rectangle(0, 0, 0, 0);

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        bounds.add(gd.getDefaultConfiguration().getBounds());

        return bounds;

    }

    public static class MatchAreaPanel extends JPanel {

        protected static final long FADE_OUT_TIME = 2500;

        private float alpha = 1f;
        private long fadeStartAt;
        private Timer fadeTimer;
        private Timer waitTimer;

        public MatchAreaPanel() {
            setOpaque(false);
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    fadeOut();
                }
            });
            fadeTimer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    long runTime = System.currentTimeMillis() - fadeStartAt;
                    float progress = 0f;
                    if (runTime >= FADE_OUT_TIME) {
                        progress = 1f;
                    } else {
                        progress = (float) runTime / (float) FADE_OUT_TIME;
                        if (progress > 1f) {
                            progress = 1f;
                        }
                    }
                    alpha = 1f - progress;

                    if (progress >= 1f) {
                        ((Timer) e.getSource()).stop();
                        SwingUtilities.windowForComponent(MatchAreaPanel.this).dispose();
                    }
                    repaint();
                }
            });
            waitTimer = new Timer(5000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    ((Timer) e.getSource()).stop();
                    fadeOut();
                }
            });
        }

        protected void fadeOut() {
            waitTimer.stop();
            fadeStartAt = System.currentTimeMillis();
            fadeTimer.start();
        }

        public void start() {
            if (!waitTimer.isRunning() && !fadeTimer.isRunning()) {
                waitTimer.start();
            }
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setComposite(AlphaComposite.SrcOver.derive(alpha));
            g2d.setColor(new Color(128, 128, 128, 64));
            g2d.fillRect(0, 0, getWidth(), getHeight());

            float dash1[] = {10.0f};
            BasicStroke dashed = new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
            g2d.setColor(Color.BLACK);
            g2d.setStroke(dashed);
            g2d.drawRect(0, 0, getWidth() - 3, getHeight() - 3);
            g2d.dispose();
        }
    }

}


来源:https://stackoverflow.com/questions/34845342/create-a-disappearing-jpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!