如何使java JPanel和graphics2d透明?

发布于 2024-08-03 22:27:24 字数 154 浏览 2 评论 0原文

嗯,标题是很不言自明的。我想使用 java 构建两个面板,一层一层地放置在彼此之上。我希望顶层包含一个 JPanel,其中包含一个 Graphics2d 对象。我希望 JPanel 和graphics2d 都有透明背景(我仍然希望graphics2d 绘制的内容可见)。有谁知道如何做到这一点?

Well the title is quite self explanatory. I want to build two panels one on-top of each other in layers using java. I want the top layer to contain a JPanel which will contain a graphics2d object. I'd like both the JPanel and graphics2d to have transparent background (I still want the content drawn by the graphics2d visible). Does anyone have an idea how it can be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染年凉城似染瑾 2024-08-10 22:27:24

在 JPanel 上调用 setOpaque(false) - 这不会绘制 JPanel 的背景。

取决于您要重写以获取 Graphics2D 的方法(JPanel 不包含像组件一样的 Graphics2D 对象 - Graphics2D 对象用于绘制 JPanel) - 如果它是paintComponent(),您应该 阅读 JComponent 的 JavaDocs - 并首先调用 super.paintComponent(g) 以便遵守不透明度 - 然后进行其余的绘制。

工作示例:

package com.stackoverflow.opaque;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class OpaqueExample extends JFrame {

    private JLayeredPane layers;
    private JPanel up, down;
    private JButton toggleOpaque;

    public OpaqueExample() {
        layers = new JLayeredPane();

        down = new JPanel();
        down.setBackground(Color.GREEN);
        down.setBounds(100, 100, 200, 200);
        layers.add(down, new Integer(1));

        up = new JPanel() {
            public void paintComponent(Graphics og) {
                super.paintComponent(og);

                Graphics2D g = (Graphics2D)og;
                GradientPaint gradient = new GradientPaint(0, 0, Color.BLUE, 10, 0, 
                          Color.WHITE, true );

                Polygon poly = new Polygon();
                poly.addPoint(10, 10);
                poly.addPoint(100, 50);
                poly.addPoint(190, 10);
                poly.addPoint(150, 100);
                poly.addPoint(190, 190);
                poly.addPoint(100, 150);
                poly.addPoint(10, 190);
                poly.addPoint(50, 100);
                poly.addPoint(10, 10);

                g.setPaint(gradient);
                g.fill(poly);

                g.setPaint(Color.BLACK);
                g.draw(poly);
           }
        };
        up.setBackground(Color.RED);
        up.setBounds(150, 150, 200, 200);
        layers.add(up, new Integer(2));

        getContentPane().add(layers, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        toggleOpaque = new JButton("Toggle Opaque");
        toggleOpaque.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                up.setOpaque(!up.isOpaque());
                layers.repaint();
            }
        });
        buttonPanel.add(toggleOpaque);

        getContentPane().add(buttonPanel, BorderLayout.EAST);
    } 

    public static void main(String[] args) {
        JFrame f = new OpaqueExample();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(500, 500);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

Call setOpaque(false) on the JPanel - that will not paint the JPanel's background.

Depending on what method you're overriding to get at the Graphics2D (JPanel's don't contain a Graphics2D object like a component - a Graphics2D object is used to paint the JPanel) - if it's paintComponent() you should read the JavaDocs for JComponent - and call super.paintComponent(g) first so that opacity is honored - and then do the rest of your painting.

Working example:

package com.stackoverflow.opaque;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class OpaqueExample extends JFrame {

    private JLayeredPane layers;
    private JPanel up, down;
    private JButton toggleOpaque;

    public OpaqueExample() {
        layers = new JLayeredPane();

        down = new JPanel();
        down.setBackground(Color.GREEN);
        down.setBounds(100, 100, 200, 200);
        layers.add(down, new Integer(1));

        up = new JPanel() {
            public void paintComponent(Graphics og) {
                super.paintComponent(og);

                Graphics2D g = (Graphics2D)og;
                GradientPaint gradient = new GradientPaint(0, 0, Color.BLUE, 10, 0, 
                          Color.WHITE, true );

                Polygon poly = new Polygon();
                poly.addPoint(10, 10);
                poly.addPoint(100, 50);
                poly.addPoint(190, 10);
                poly.addPoint(150, 100);
                poly.addPoint(190, 190);
                poly.addPoint(100, 150);
                poly.addPoint(10, 190);
                poly.addPoint(50, 100);
                poly.addPoint(10, 10);

                g.setPaint(gradient);
                g.fill(poly);

                g.setPaint(Color.BLACK);
                g.draw(poly);
           }
        };
        up.setBackground(Color.RED);
        up.setBounds(150, 150, 200, 200);
        layers.add(up, new Integer(2));

        getContentPane().add(layers, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        toggleOpaque = new JButton("Toggle Opaque");
        toggleOpaque.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                up.setOpaque(!up.isOpaque());
                layers.repaint();
            }
        });
        buttonPanel.add(toggleOpaque);

        getContentPane().add(buttonPanel, BorderLayout.EAST);
    } 

    public static void main(String[] args) {
        JFrame f = new OpaqueExample();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(500, 500);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文