java - 如何在 JPanel 上显示图像作为背景

标签 java image background jpanel

当我根据 JPanel 的大小重新缩放图像时,我在 JPanel 上显示图像时遇到问题。图像没有出现。

   public class createGUII extends JFrame{
        String [] background = {"c1.jpg","c2.jpg","c3.jpg","c4.jpg"};                       
        ArrayList<String> bgPicturesFiles   = new ArrayList<String>(Arrays.asList(background)); 


    JPanel panel;
    ImagePanel imgBg;

    public createGUII(){
        GridBagLayout m = new GridBagLayout();
        Container c = getContentPane();
        c.setLayout (m);
        GridBagConstraints con = new GridBagConstraints();

       //Panel for background
        panel = new JPanel();       
        panel.setSize(600, 600);
        con = new GridBagConstraints();
        con.anchor=GridBagConstraints.CENTER;
        con.gridy = 1;      con.gridx = 0;
        con.gridwidth = 1;  con.gridheight = 1;     
        m.setConstraints(panel, con);   
        c.add(panel);

       //randomized the image files
        Random r = new Random();                        
        int random = r.nextInt(bgPicturesFiles.size());

       //rescale the image according to the size of the JPanel 
        imgBg = new ImagePanel(new ImageIcon(bgPicturesFiles.get(random)).getImage().getScaledInstance(panel.getHeight(), panel.getWidth(),Image.SCALE_SMOOTH));            
        panel.add(imgBg);

        setResizable(false);
        setVisible(true);       
        setExtendedState(getExtendedState()|JFrame.MAXIMIZED_BOTH);             

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new createGUII();       
            }
        });
    }
}



class ImagePanel extends JPanel {
    private Image img;

    public ImagePanel(String img) {
        this(new ImageIcon(img).getImage());
    }

    public ImagePanel(Image img) {
        this.img = img;
        Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);
        setLayout(null);
        }

    public void paintComponent(Graphics g) {
        g.drawImage(img, 0, 0, null);
        }
}     

最佳答案

看看这个:JPanel background image , JPanel with background image, with other panels overlayed

第二个链接提到您必须为缩放做一些自定义绘画。这是个问题。我不会在 paintComponent 方法中每次都缩放图像,但如果自上次调用以来宽度和高度已更改,则一次缩放图像,在这种情况下,重新创建包含图像的 BufferedImage在调用父类(super class) paintComponent 之前,你每次都将其 blit,按比例放大到合适的大小(使用类似 Image scaling does not work when original image height & width is smaller the scaling height & width 的东西)。我可以看到一个问题,当您调用父类(super class) paintComponent 方法时,它可能会尝试用一种颜色填充面板,但您必须进行试验。

关于java - 如何在 JPanel 上显示图像作为背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4170463/

相关文章:

php - php和css中的随机背景

android : when using MediaPlayer to play background music, 音效过早停止

具有单一或重叠背景的 Android 幻灯片 View

java - 如何在 mysql SELECT 查询中使用文本字段输入

java - spring boot 和 tomcat - 找不到文件

php - move_uploaded_file - 防止旋转/忽略 exif 数据

c# - 如何从 System.Drawing.Imaging.ImageFormat 获取 contentType

java - 如何将 "á"等字符替换为相应的英文字母

java - 我在这段代码上得到了 StackOverFlowException,因为我的 JVM 不支持尾调用优化,对吧?

image - Octave ,套接字连接收到的显示图像不显示