Java AWT Draw on Translucent Frame
NickName:Joe Ask DateTime:2015-11-06T04:26:10

Java AWT Draw on Translucent Frame

I am having trouble drawing on a translucent frame. When the "alphaValue" is 255 everything works as expected. But I need a translucent frame. I created a small test class below that demonstrates the problem. As you can see the "MIDDLE" rectangle appears all the time. But the "DRAW" rectangle only appears when "alphaValue" is 255. When it is <=254 you can see via print lines that the method is still called, but the image does not appear to refresh. Thank you in advance for any help.

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class TransparencyTest {
    private static Point startPoint = new Point();
    private static Point endPoint = new Point();

    public static void main(String[] args) {
        new TransparencyTest().test();
    }

    @SuppressWarnings("serial")
    private void test() {
        int alphaValue = 255;

        Frame myFrame = new Frame();
        myFrame.setUndecorated(true);
        myFrame.setBackground(new Color(0, 0, 0, alphaValue));
        myFrame.setSize(800, 800);

        Panel myPanel = new Panel() {
            public void paint(Graphics g) {
                super.paint(g);
                System.out.println("PAINT");
                g.setColor(new Color(255, 0, 0, 255));
                if(startPoint.equals(new Point())) {
                    System.out.println("MIDDLE");
                    g.drawRect(200, 200, 400, 400);
                }
                else {
                    System.out.println("DRAW");
                    g.drawRect(
                            (int)Math.min(startPoint.getX(), endPoint.getX()),
                            (int)Math.min(startPoint.getY(), endPoint.getY()),
                            (int)Math.abs(startPoint.getX() - endPoint.getX()),
                            (int)Math.abs(startPoint.getY() - endPoint.getY())
                    );
                }
            }
        };
        myFrame.add(myPanel);

        MouseAdapter myMouseAdapter = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                startPoint = e.getPoint();
            }

            public void mouseDragged(MouseEvent e) {
                endPoint = e.getPoint();
                myPanel.repaint();
            }
        };
        myPanel.addMouseListener(myMouseAdapter);
        myPanel.addMouseMotionListener(myMouseAdapter);

        myFrame.setVisible(true);
    }
}

Copyright Notice:Content Author:「Joe」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/33554128/java-awt-draw-on-translucent-frame

More about “Java AWT Draw on Translucent Frame” related questions

Java AWT Draw on Translucent Frame

I am having trouble drawing on a translucent frame. When the "alphaValue" is 255 everything works as expected. But I need a translucent frame. I created a small test class below that demonstrate...

Show Detail

Translucent Glass Pane over java.awt.Panel

Hi have a Swing User Interface composed by 4 different plots (extensions of javax.swing.JPanel). One of these plots is built using jzy3d 1.0.0 library and returns a component of type CanvasNewtAwt (

Show Detail

Clojure (let [frame (java.awt.Frame.)]) within un-invoked function causes AWT to be started

Whilst going through the Joy of Clojure book, I've succeedding in defining a function that, when invoked, will create and draw on a java.awt.Frame. (defn draw-frame [f x y] (let [frame (java.awt.

Show Detail

Translucent JPopupMenu inside a Translucent Window - alternative?

I'm not sure if this is possible, but is there a way to safely allow popups to be translucent even when the parent container is also translucent? If not, what would be wise alternative to use or ...

Show Detail

Hosting ActiveX controls on a Java AWT Frame?

Is there a way to display a Flash or Adobe Reader ActiveX control in a Java AWT frame? I'm porting a large old J++ application to standard Java and it needs to host a certain ActiveX control on an...

Show Detail

Re-paint on translucent frame/panel/component.

I'm trying to create a translucent window with Java on OSX and add a JLabel to it. This JLabel changes its text every second.... However the component is not repainting well. How can I solve ...

Show Detail

How to draw circle using java.awt.Graphics?

I'm just trying to draw circle using the drawOval() method and it shows only small square when I run the program. I was trying to add the constructor to the Surface class but it didn't work as well...

Show Detail

Can't create translucent window

I'm trying to make translucent window, I've read many posts that say that it doesn't work in Java 7 at all, some solved this problem, but their ways don't work for me. If I try to make panel transl...

Show Detail

Translucent window with Windows look and feel?

I'm playing with JDK7's translucent window support and find that it doesn't work well with Windows look and feel. Here's my code: JFrame.setDefaultLookAndFeelDecorated(true); SwingUtilities.invok...

Show Detail

Is it possible to have a translucent windows in java 7 including a title bar?

Related to this question: Is The Java Tutorials Translucent Window example giving trouble to those playing with jdk7? with jdk1.6.0_26 I seem to be able to apply translucency to a JFrame, but not so

Show Detail