java swing中的java swing部分透明度显示为黑色

时间:2016-08-14 19:36:34

标签: java swing jpanel

我正在尝试使用“child”JPanel创建一个基座JPanels,其中包含透明矩形,用于显示基座JPanel的背景。 我似乎只能在我的孩子和基地JPanel中创建透明的矩形。有没有办法告诉java只能在我的孩子JPanels上绘制不透明的部分JPanel并忽略透明部分?

简而言之,我希望图片中的黑色矩形为青色(基础JPanel的背景颜色)。

JFrame

的图片

http://i.imgur.com/eFKs2d7.png

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.beans.Transient;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;


public class Showcase extends JFrame{

    public static void main(String[] args) {
        Showcase window = new Showcase();
    }

    public Showcase() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel contentPane = new JPanel();
        contentPane.setBackground(Color.CYAN);
        setContentPane(contentPane);


        final TestPane tP = new TestPane();
        contentPane.add(tP);
        tP.setBackground(Color.BLUE);

        setVisible(true);
        pack();
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(500, 300);
    }

    public class TestPane extends JPanel{

        @Override
        protected void paintComponent(Graphics g) {         
            super.paintComponent(g);        
            Graphics2D g2 = (Graphics2D) g;
            g2.setBackground(new Color(0, 0, 0, 0));

            g2.clearRect(0, 0, 50,50);
            g2.dispose();
        }

        @Override
        @Transient
        public Dimension getPreferredSize() {
            return new Dimension(100, 100);
        }
    }
}

2 个答案:

答案 0 :(得分:2)

  

有没有办法告诉java只绘制不透明的部分

首先,您需要使面板不透明,以便绘制父背景。

然后你可以使用Area类来创建你想要绘制的Shape

public class TestPane extends JPanel
{
    public TestPane()
    {
        setOpaque( false ); // to make it transparent.
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
/*
        Graphics2D g2 = (Graphics2D) g;
        g2.setBackground(new Color(0, 0, 0, 0));

        g2.clearRect(0, 0, 50,50);
        g2.dispose();
*/
        Graphics2D g2d = (Graphics2D) g.create();

        Shape outer = new Rectangle(0, 0, getWidth(), getHeight());
        Shape inner = new Rectangle(0, 0, 50, 50);
        Area area = new Area( outer );
        area.subtract( new Area(inner) );

        g2d.setColor( getBackground() );
        g2d.fill(area);
        g2d.dispose();
    }

    @Override
    @Transient
    public Dimension getPreferredSize() {
        return new Dimension(100, 100);
    }
}

答案 1 :(得分:1)

您可以使用setOpaque()使JPanel的背景透明,但是您无法绘制想要透明的矩形。

另一种方法是制作BufferedImage并在该图片上绘制矩形,然后将BufferedImage添加到JPanel

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.beans.Transient;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;


public class Showcase extends JFrame {

    BufferedImage img;

    public static void main(String[] args) {
        Showcase window = new Showcase();
    }

    public Showcase() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel contentPane = new JPanel();
        contentPane.setBackground(Color.CYAN);
        setContentPane(contentPane);


        final TestPane tP = new TestPane();
        contentPane.add(tP);

        // make new buffered image
        img = new BufferedImage(tP.getPreferredSize().width, 
                tP.getPreferredSize().height, BufferedImage.TYPE_INT_ARGB);

        setVisible(true);
        pack();
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(500, 300);
    }

    public class TestPane extends JPanel{

        @Override
        protected void paintComponent(Graphics g) {              
            Graphics2D g2 = (Graphics2D) g;

            Graphics2D imgG2 = img.createGraphics();

            // make img background transparent
            imgG2.setBackground(new Color(0,0,0,0));

            imgG2.setColor(Color.red);
            imgG2.fillRect(0, 0, 100, 100);

            imgG2.clearRect(0, 0, 50, 50);

            imgG2.dispose();

            // draw buffered image to jpanel
            g2.drawImage(img, 0, 0, null);

            g2.dispose();

            super.paintComponent(g);   
        }

        @Override
        @Transient
        public Dimension getPreferredSize() {
            return new Dimension(100, 100);
        }
    }
}

最终结果如下:

example