2 votes

JFrame avec un arrière-plan transparent et flou

Je voudrais faire un flou sur l'arrière-plan du JFrame qui est transparent pour montrer ce qui se passe en dessous, mais je n'ai aucune idée de la façon dont je peux flouter l'arrière-plan et éviter le scintillement. Ce que je veux obtenir, c'est un arrière-plan transparent qui soit légèrement flou, mais qui montre toujours la "vue en direct" de la fenêtre située en dessous, et non pas une image statique floue qui ne change pas. Gardez à l'esprit que la fenêtre peut prendre la taille du plein écran.

J'espère l'avoir décrit correctement, car je suis encore un débutant en Java Graphics.

Le cadre transparent en code :

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;

public class BlurredBackgroundWindow {

    public static void main(String[] args) {
        new BlurredBackgroundWindow().drawGUI();
    }

    public void drawGUI() {
        myJFrame frm = new myJFrame();
        frm.setTitle("BlurredBackgroundWindow");
        frm.setSize(480, 360);
        frm.setUndecorated(true);
        frm.setBackground(new Color(0,0,0,1));
        frm.setLocationRelativeTo(null);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frm.setVisible(true);
    }

    class myJFrame extends JFrame {

        public void paint(Graphics g) {
            super.paint(g);

            // Doing the rest of painting here
        }

    }

}

Note - Le cadre est complètement transparent et pour voir un certain effet, changez la couleur à eg.

frm.setBackground(new Color(0,100,0,100));

Merci pour toute aide

EDIT 1 : Voici l'effet en action AVEC le scintillement que je veux éviter, mais je ne sais pas vraiment comment... Celui-ci ne fonctionne que pour une fenêtre en plein écran.

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;

public class BlurredBackgroundWindow {

    public static int FPS = 2;
    private BufferedImage temp = null;
    private BufferedImage out = null;
    private BufferedImage image = null;

    myJFrame frm = new myJFrame();

    public static void main(String[] args) {
        new BlurredBackgroundWindow().drawGUI();
    }

    public void drawGUI() {
        frm.setTitle("BlurredBackgroundWindow");
        frm.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frm.setUndecorated(true);
        frm.setBackground(new Color(0,0,0,1));
        frm.setLocationRelativeTo(null);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frm.setVisible(true);

        Timer bcg = new Timer();
        bcg.schedule(new TimerTask() {

            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Rectangle screenRectangle = new Rectangle(screenSize);
            Robot robot;

            public void run() {

                System.out.println("Repaint");
                frm.repaint();
                try {
                    robot = new Robot();
                    image = robot.createScreenCapture(screenRectangle);
                } catch (AWTException e) {}
                frm.repaint();
                // Save the blurred image for debugging
                /*try {
                    ImageIO.write(image, "png", new File("C:\\test.png"));
                } catch (IOException e) {
                    e.printStackTrace();
                }*/

            }

        }, 0, (int) (1000f/FPS));
    }

    class myJFrame extends JFrame {

        public void paint(Graphics g) {
            super.paint(g);

            if(image != null) {
                   float[] matrix = {
                            0.111f, 0.111f, 0.111f, 
                            0.111f, 0.111f, 0.111f, 
                            0.111f, 0.111f, 0.111f, 
                        };
                        BufferedImageOp op = new ConvolveOp( new Kernel(3, 3, matrix) );
                        temp = op.filter(image, out);
                        out = temp;
                g.drawImage(out,0,0,null);
                temp=null;out=null;image=null;
            }

        }

    }

}

0voto

Insane Coder Points 1516

Vous pouvez le faire très facilement et il y a plusieurs façons de le faire, comme le montre l'exemple de code suivant.

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsDevice.WindowTranslucency;
import java.awt.GraphicsEnvironment;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class transparentWindow extends JFrame {

public transparentWindow() {
    // TODO Auto-generated constructor stub
    //JFrame jfrm=new JFrame("Transparent Window");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300,200);
    getContentPane().setLayout(new FlowLayout());
    //setBackground(new Color(0,0,0,0));

    add(new JButton("Enter"));
    setOpacity(0.7f);
    setVisible(true);
}
public static void main(String[] args) {
    // TODO Auto-generated method stub
    GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd=ge.getDefaultScreenDevice();
    if(!gd.isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT))
    {
        System.out.println("Transparency not supported");
        System.exit(0);
    }
    JFrame.setDefaultLookAndFeelDecorated(true);
    SwingUtilities.invokeLater(new Runnable(){public void run(){new transparentWindow();}});
}

}

Avec cela, vous pouvez même regarder la vidéo en direct à travers elle. Vous pouvez ajuster le niveau de transparence aussi bien.la sortie est comme suit :-

enter image description here

0voto

Insane Coder Points 1516

Vous pouvez utiliser une méthode légèrement différente, comme indiqué dans le code suivant :-.

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsDevice.WindowTranslucency;
import java.awt.GraphicsEnvironment;
import java.awt.Paint;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class transparent extends JFrame {

public transparent() {
    super("Transparent Window");
    setBackground(new Color(0,0,0,0));

    //setting it causes the frame to be transparent .Hence both panel and frame are transparent.
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300,200);
    getContentPane().setLayout(new FlowLayout());

    JPanel jp=new JPanel(){
        public void paintComponent(Graphics g)
        {
            //super.paintComponent(g);
            Graphics2D g2=(Graphics2D)g;
            Paint gp=new GradientPaint(0, 0, new Color(100,20,210,105), 0, 200, new Color(80,20,40,105));
            g2.setPaint(gp);
            g2.fillRect(0, 0, getWidth(),getHeight());
        }
    };
    //setOpacity(0.6f);
    setContentPane(jp);
    JButton jbtn=new JButton("Enter");
    jp.add(jbtn);
    setVisible(true);
}
public static void main(String[] args) {
    // TODO Auto-generated method stub
    GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd=ge.getDefaultScreenDevice();
    if(!gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT))
    {
        System.out.println("Per-pixel Transency not supported");
        System.exit(0);
    }
    JFrame.setDefaultLookAndFeelDecorated(true);

    //setting it to true causes the look to be handled by look and feel. otherwise os look and feel is used
    //In other words ,it is,modify default look and feel-yes or no?
    SwingUtilities.invokeLater(new Runnable(){public void run(){new transparent();}});
}
/*public void paint(Graphics g)
{
    super.paint(g);
    Graphics2D g2=(Graphics2D)g;
    GradientPaint gp=new GradientPaint(0, 0, new Color(20,20,210,30), 300, 200, new Color(10,20,40,255),true);
    g2.setPaint(gp);
    g2.fillRect(0, 0, getWidth(),getHeight());
}*/
}

Voici la sortie obtenue, qui est bien meilleure et vitreuse :-.

enter image description here

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X