Panneaux transparents Java et effet personnalisé sur les panneaux


Je veux avoir des panneaux transparents dans mon interface graphique (si comme les en-têtes de fenêtre Windows 7, c'est mieux).

Avant d'avoir utilisé com.sun.awt.AWTUtilities comme

AWTUtilities.setWindowOpacity(frame, (float)0.90);

, Mais son paramètre est une fenêtre comme JFrame et ne pouvait pas être utilisé pour JPanel.

Je veux aussi avoir des effets sur JPanel ou JLabel par exemple la luminescence, comme c'est le cas sur les boutons d'en-tête Windows 7. Tout autre effet intéressant est également utile pour moi.

entrez la description de l'image ici

Author: Radiodef, 2011-08-09

2 answers

Veuillez consulter les tutoriels Comment créer des Fenêtres Translucides et en Forme et*Comment créer des fenêtres Translucides et en forme *. Suivez les liens vers d'excellents exemples de dépôts par @camickr.

Par exemple

import java.awt.event.*;
import java.awt.Color;
import java.awt.AlphaComposite;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;

public class ButtonTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ButtonTest().createAndShowGUI();
            }
        });
    }

    private JFrame frame;
    private JButton opaqueButton1;
    private JButton opaqueButton2;
    private SoftJButton softButton1;
    private SoftJButton softButton2;

    public void createAndShowGUI() {
        opaqueButton1 = new JButton("Opaque Button");
        opaqueButton2 = new JButton("Opaque Button");
        softButton1 = new SoftJButton("Transparent Button");
        softButton2 = new SoftJButton("Transparent Button");
        opaqueButton1.setBackground(Color.GREEN);
        softButton1.setBackground(Color.GREEN);
        frame = new JFrame();
        frame.getContentPane().setLayout(new java.awt.GridLayout(2, 2, 10, 10));
        frame.add(opaqueButton1);
        frame.add(softButton1);
        frame.add(opaqueButton2);
        frame.add(softButton2);
        frame.setSize(567, 350);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        Timer alphaChanger = new Timer(30, new ActionListener() {

            private float incrementer = -.03f;

            @Override
            public void actionPerformed(ActionEvent e) {
                float newAlpha = softButton1.getAlpha() + incrementer;
                if (newAlpha < 0) {
                    newAlpha = 0;
                    incrementer = -incrementer;
                } else if (newAlpha > 1f) {
                    newAlpha = 1f;
                    incrementer = -incrementer;
                }
                softButton1.setAlpha(newAlpha);
                softButton2.setAlpha(newAlpha);
            }
        });
        alphaChanger.start();
        Timer uiChanger = new Timer(3500, new ActionListener() {

            private LookAndFeelInfo[] laf = UIManager.getInstalledLookAndFeels();
            private int index = 1;

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    UIManager.setLookAndFeel(laf[index].getClassName());
                    SwingUtilities.updateComponentTreeUI(frame);
                } catch (Exception exc) {
                    exc.printStackTrace();
                }
                index = (index + 1) % laf.length;
            }
        });
        uiChanger.start();
    }

    public static class SoftJButton extends JButton {
        private static final JButton lafDeterminer = new JButton();
        private static final long serialVersionUID = 1L;
        private boolean rectangularLAF;
        private float alpha = 1f;

        public SoftJButton() {
            this(null, null);
        }

        public SoftJButton(String text) {
            this(text, null);
        }

        public SoftJButton(String text, Icon icon) {
            super(text, icon);

            setOpaque(false);
            setFocusPainted(false);
        }

        public float getAlpha() {
            return alpha;
        }

        public void setAlpha(float alpha) {
            this.alpha = alpha;
            repaint();
        }

        @Override
        public void paintComponent(java.awt.Graphics g) {
            java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
            if (rectangularLAF && isBackgroundSet()) {
                Color c = getBackground();
                g2.setColor(c);
                g.fillRect(0, 0, getWidth(), getHeight());
            }
            super.paintComponent(g2);
        }

        @Override
        public void updateUI() {
            super.updateUI();
            lafDeterminer.updateUI();
            rectangularLAF = lafDeterminer.isOpaque();
        }
    }
}
 7
Author: mKorbel, 2017-05-23 11:43:57

Si vous avez le temps, je vous recommande de passer par ce Filty Rich Clients. En utilisant ce livre, vous pouvez apprendre à créer de superbes effets visuels et animés avec Swing et Java 2D. Apprenez les fondamentaux des graphiques et de l'animation ainsi que des techniques de rendu avancées.

MODIFIER: Pour créer des panneaux transparents, appelez

setOpaque(false)

Il passera outre la peinture de l'arrière-plan à son parent, qui peut dessiner son propre arrière-plan.

Vous pouvez faire une capture d'écran, puis l'utiliser pour peindre l'arrière-plan du panneau.

 3
Author: u449355, 2011-08-09 08:54:28