在JPanel上绘制部分透明的PNG

时间:2013-07-22 04:42:34

标签: java swing png jpanel transparency

如何通过覆盖其void paint (Graphics g)方法将部分透明的图像绘制到JPanel上?我尝试了一种显而易见的方法,即加载图像然后使用((Graphics2D)g).drawImage(...),但这不起作用,互联网并没有告诉我太多。

1 个答案:

答案 0 :(得分:7)

JLabel能够执行此操作,但如果您想对面板/图片执行其他操作,则使用paintComponentGraphics#drawImage也是一种选择。

以此为基础......

enter image description here

我能够渲染这个......

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TransparentPNG {

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

    public TransparentPNG() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;

        public TestPane() {
            try {
                img = ImageIO.read(new File("Pony.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(), img.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            LinearGradientPaint lgp = new LinearGradientPaint(
                    new Point(0, 0),
                    new Point(0, getHeight()),
                    new float[]{0f, 1f},
                    new Color[]{Color.GREEN, Color.YELLOW});
            g2d.setPaint(lgp);
            g2d.fillRect(0, 0, getWidth(), getHeight());
            if (img != null) {
                int x = (getWidth() - img.getWidth()) / 2;
                int y = (getHeight() - img.getHeight()) / 2;
                g2d.drawImage(img, x, y, this);
            }
            g2d.dispose();
        }
    }
}

结帐How to use labelsPerforming Custom Painting