半透明框架上的Java AWT绘图

时间:2015-11-05 20:26:10

标签: java awt

我在使用半透明框架时遇到问题。当" alphaValue" 255是一切按预期工作。但我需要一个半透明的框架。我在下面创建了一个小测试类来演示这个问题。正如你可以看到" MIDDLE"矩形一直出现。但是" DRAW"矩形只出现在" alphaValue"当它是< = 254时,您可以通过打印行看到仍然调用该方法,但图像似乎没有刷新。提前感谢您的帮助。

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);
    }
}

1 个答案:

答案 0 :(得分:2)

AWT组件本身没有透明度的概念,它们总是不透明的。

您使用JPanel,使用setOpaque来控制不透明度(打开或关闭)。这将允许面板透视,然后您应该能够看到直接应用于框架的alpha效果...

Transparent

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TransparencyTest {

    private static Point startPoint = new Point();
    private static Point endPoint = new Point();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                new TransparencyTest().test();
            }
        });
    }

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

        JFrame myFrame = new JFrame();
        myFrame.setUndecorated(true);
        myFrame.setBackground(new Color(0, 0, 0, alphaValue));
//      myFrame.setOpacity(0.1f);
        myFrame.setSize(800, 800);
        myFrame.setLocation(100, 100);

        JPanel myPanel = new JPanel() {
            public void paintComponent(Graphics g) {
                super.paintComponent(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())
                    );
                }
            }
        };
        myPanel.setOpaque(false);
        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);
    }
}
相关问题