Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: translucent windows support is this really supported in majority of OS?

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default translucent windows support is this really supported in majority of OS?

    Hi.

    After a lot of struggling I have finally code of per pixel transparency which works: JFrame with translucent gradient background + 1 opaque button. BUT! I works only (among other tested OS/JDK) in JDK7 and windows 7. Alpha channel is totally missing with windows 10, XFCE, both using JDK8.

    So the question is:
    is this feature actually working and I'm just missing some magical switches in windows / xfce? Or it's rather considered as unstable feature?

    Note: code in question is just a prototype, what I'm really after is ability "to see through some glass" and being able to paint on it, regardless what applications (not just mine, therefore no glasspane) are running below. If there is nice solution I can use instead you can recommend, please do! (note2: screenshotting using robot could be considered as plan-b, but really insufficient one, as it disallows to see changes below "glass").

    Thanks!

    EDIT, the code(I stole it somewhere and did some minor fixing changes. But again on windows7 JDK7 it works perfectly.):

     
    import static java.awt.GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT;
     
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
     
    public class Trans3 extends JFrame {
        public Trans3() {
            super("GradientTranslucentWindow");
            setUndecorated(true);
            setBackground(new Color(0, 0, 0, 0));
            setSize(new Dimension(300, 200));
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            JPanel panel = new JPanel() {
                @Override
                protected void paintComponent(Graphics g) {
                    if (g instanceof Graphics2D) {
                        final int R = 240;
                        final int G = 240;
                        final int B = 240;
                        Paint p =
                                new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0),
                                        0.0f, getHeight(), new Color(R, G, B, 255), true);
                        Graphics2D g2d = (Graphics2D) g;
                        g2d.setPaint(p);
                        g2d.fillRect(0, 0, getWidth(), getHeight());
                    }
                }
            };
            setContentPane(panel);
            setLayout(new GridBagLayout());
            JButton button = new JButton("exit");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    setVisible(false);
                    dispose();
                }
            });
            add(button);
        }
     
        public static void main(String[] args) {
            // Determine what the GraphicsDevice can support.
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gd = ge.getDefaultScreenDevice();
            boolean isPerPixelTranslucencySupported = gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);
            //If translucent windows aren't supported, exit.
            if (!isPerPixelTranslucencySupported) {
                System.out.println("Per-pixel translucency is not supported");
                System.exit(0);
            }
            JFrame.setDefaultLookAndFeelDecorated(false);
            // Create the GUI on the event-dispatching thread
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    Trans3 gtw = new Trans3();
                    // Display the window.
                    gtw.setVisible(true);
                }
            });
        }
    }
    Last edited by alfonz19; October 7th, 2017 at 10:24 AM. Reason: adding code

  2. #2
    Junior Member
    Join Date
    Oct 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: translucent windows support is this really supported in majority of OS?

    resolved for both windows 10 and xfce.

    1. windows: I found this question on on stackoverflow:
    https://stackoverflow.com/questions/...-in-windows-10
    which I built in idea and it worked! So this example is proper example for windows 10. Next, I retried some examples which did not work previously on win 10 and found they are working now as well! That came to me as a shock, since one cannot mistake black background with transparent one. Maybe I did some minor changes after testing on win 10, which made it working now, I don't know. Nevertheless it works now.

    2. linux&xfce. Even code in link above did not work. So I tried to look for potential problems in system setting and one of tested setting was "turned off compositor" (compositors are sometimes turned of because they'are known to cause some graphical issues in xfce). With compositor turned back on, java transparency work OK.

    so this resolves this topic. Thanks to all readers trying to help me.
    M.
    Last edited by alfonz19; October 8th, 2017 at 04:44 PM. Reason: typos

Similar Threads

  1. Setting background color to translucent?!?!
    By syregnar86 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 23rd, 2013, 08:58 AM
  2. Gradient Translucent Window
    By Duvan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2011, 01:43 PM
  3. Replies: 1
    Last Post: September 30th, 2010, 03:36 PM

Tags for this Thread