透明JFrame在重绘时不清除

时间:2016-04-29 17:09:49

标签: java swing jframe

当我尝试重新绘制透明窗口并在其上绘制一个矩形时,前一个矩形将保留。目标是通过单击并移动鼠标来选择屏幕上的区域。 It'll look like this if you move your mouse for a while

通过删除透明度,它可以正常工作。

我在Stack Overflow上尝试了有关此主题的所有内容,但我无法在Windows和Linux上使用它。

主类

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

public class Main {
    private JFrame frame;
    private boolean pressing = false;
    private boolean selected = false;
    private ScreenSelectPanel p;

    public Main() {
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        frame = new JFrame("ScreenSelection");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(dim);
        frame.setUndecorated(true);

        frame.setContentPane(p = new ScreenSelectPanel());
        registerListeners();

        frame.getContentPane().setBackground(new Color(255, 255, 255,    0));
        frame.setBackground(new Color(255, 255, 255, 0));
        frame.setLayout(new BorderLayout());

        frame.setAlwaysOnTop(true);
        frame.setVisible(true);
    }

    private void registerListeners() {
        p.setFocusable(true);
        p.requestFocusInWindow();

        p.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                if (selected)
                    return;
                setLoc(e);
                p.repaint();

            }

            @Override
            public void mouseMoved(MouseEvent e) {
                if (selected)
                    return;
                setLoc(e);
                if (!pressing)
                    setStartLoc(e);
                p.repaint();
            }
        });
        p.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                setLoc(e);
                setStartLoc(e);
                p.repaint();
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                setLoc(e);
                setStartLoc(e);
                p.repaint();

            }
        });
    }

    public void setStartLoc(MouseEvent e) {
        p.mouseStartX = e.getX();
        p.mouseStartY = e.getY();
    }

    public void setLoc(MouseEvent e) {
        p.mouseX = e.getX();
        p.mouseY = e.getY();
    }

    public static void main(String[] args) {
        new Main();
    }
}

ScreenSelectPanel类

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;

public class ScreenSelectPanel extends JPanel {
    public int mouseX = 0;
    public int mouseY = 0;
    public int mouseStartX = 0;
    public int mouseStartY = 0;

    private Color borderColor;

    public ScreenSelectPanel() {
        setOpaque(false);
        borderColor = Color.BLACK;
    }

    public void setBorderColor(Color c) {
        this.borderColor = c;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(borderColor);
        Rectangle rect = new Rectangle();
        rect.setFrameFromDiagonal(new Point2D.Float(mouseStartX, mouseStartY), new Point2D.Float(mouseX, mouseY));
        Stroke dashed = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{9}, 0);
        g2d.setStroke(dashed);
        g2d.drawRect(rect.x, rect.y, rect.width, rect.height);

        g2d.dispose();
    }

}

谢谢:)

1 个答案:

答案 0 :(得分:1)

您无法在Swing组件中使用透明度。透明背景会导致这些类型的绘画问题。 Swing组件是不透明的或不透明的。

有关此问题的详细信息,请查看Backgrounds With Transparency。但是在这个原因中它不是问题,因为你试图在Swing面板上使用完全透明。

  

当我尝试重新绘制透明窗口并在其上绘制一个矩形时,前一个矩形将保留。

您发布的代码可以做任何事情(至少在Windows上)。当您将框架设置为完全透明时,Swing不再处理MouseEvents,而是由框架下方的应用程序处理。

我对您的代码进行了以下更改,它似乎对我有用:

//frame.getContentPane().setBackground(new Color(255, 255, 255,    0));
//frame.setBackground(new Color(255, 255, 255, 0));
frame.setBackground(new Color(255, 255, 255, 10));