0

So I have a JFrame and I have it's opacity to a certain value (1.0f) in this case and what I want to do is after the mouse is released I want the opacity of the window to change. How can I do this? I tried using the AWTUtilities.setWindowOpacitybut that just gave me an error. Thanks in advance. Here is the current code.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

import static java.awt.GraphicsDevice.WindowTranslucency.*;

public class WindowOne extends JFrame {

    public WindowOne() {

        super("TranslucentWindow");

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        double width = screenSize.getWidth();
        double height = screenSize.getHeight();


        getContentPane().addMouseListener(new MouseAdapter() {

            @Override
        public void mousePressed(MouseEvent e) {

            Shared.xPressed = getXPosition();
            Shared.yPressed = getYPosition();

            System.out.println(Shared.xPressed + " " + Shared.yPressed);

        }
    });

    getContentPane().addMouseListener(new MouseAdapter() {

        @Override
        public void mouseReleased(MouseEvent e) {

            Shared.xReleased = getXPosition();
            Shared.yReleased = getYPosition();

            System.out.println(Shared.xReleased + " " + Shared.yReleased);

        }
    });

    getContentPane().setLayout(new GridBagLayout());

    setLocation(0, 0);
    setSize((int)width,(int)height);
    getRootPane().setWindowDecorationStyle(JRootPane.NONE);
    setVisible(true);
}

public static void main(String[] args) {

    GraphicsEnvironment ge = 
        GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();

    if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) {
        System.err.println(
            "Translucency is not supported");
            System.exit(0);
    }

    JFrame.setDefaultLookAndFeelDecorated(true);

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            WindowOne tw = new WindowOne();

            tw.setOpacity(1.0f);

            tw.setVisible(true);
        }
    });

    }

public static int getXPosition() {

    PointerInfo location = MouseInfo.getPointerInfo();
    int x = (int) location.getLocation().getX();

    return x;
}

public static int getYPosition() {

    PointerInfo location = MouseInfo.getPointerInfo();
    int y = (int) location.getLocation().getY(); 

    return y;
}

}

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Maple
  • 17
  • 6
  • In JFrame Class there is a `setOpacity()` method, have you try with that? – jac1013 Jun 23 '13 at 16:50
  • 1
    You may be confounding _opacity_ and _transparency_, as suggested [here](http://stackoverflow.com/a/3518047/230513). – trashgod Jun 23 '13 at 20:51
  • I want to change the opacity, not transparency. And the setOpacity() was exactly what I was looking for. Thanks a lot. – Maple Jun 23 '13 at 23:45

0 Answers0