如何在Jframe中添加重复背景图像?

时间:2013-05-25 14:50:16

标签: java swing jframe background-image

如何在Jframe中添加重复背景图像?

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class myGUI extends JFrame {

    /**
     * Create the frame.
     */
    public myGUI() {    
        super.setResizable(false);
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        super.setUndecorated(true);
        super.setExtendedState(JFrame.MAXIMIZED_BOTH);
        super.setVisible(true);
    }

}

我尝试过很多例子,但没有找到带有重复背景的代码。

更新,这是我的GUI和ImagePanel。 GUI:

import java.awt.BorderLayout;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class GUI extends JFrame {

    private JPanel logPanel = new LogPane();
    private JPanel logotipPanel = new LogotipPane();
    ImagePanel mainPanel = new ImagePanel(Toolkit.getDefaultToolkit().getImage( "C:/Users/Owner/workspace/Blackjack/bin/bg.png" ));

    /**
     * Create the frame.
     */
    public GUI() {

        logPanel.setOpaque(false);
        logotipPanel.setOpaque(false);
        mainPanel.add(logPanel, BorderLayout.EAST);
        mainPanel.add(logotipPanel, BorderLayout.NORTH);

        add(mainPanel);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        setUndecorated(true);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setVisible(true);

    }

}

ImagePanel:

import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;

@SuppressWarnings("serial")
class ImagePanel extends JPanel {
    private Image image;

    ImagePanel(Image image) {
        this.image = image;
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        int iw = image.getWidth(this);
        int ih = image.getHeight(this);
        if (iw > 0 && ih > 0) {
            for (int x = 0; x < getWidth(); x += iw) {
                for (int y = 0; y < getHeight(); y += ih) {
                    g.drawImage(image, x, y, iw, ih, this);
                }
            }
        }
    }

}

所以,背景问题有重复解决,但是这样我就不能使用所有的BorderLayouts了,因为我将面板添加到ImagePanel而不是JFrame GUI。

1 个答案:

答案 0 :(得分:2)

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URL;

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

public class Main {
    public static void main(String[] args) throws IOException {
        final Image image = ImageIO.read(new File("logo.png"));
        final JFrame frame = new JFrame();
        JPanel imagePanel = new ImagePanel(image);
         imagePanel.setLayout(new BorderLayout()); // setting layout as BorderLayout
        JPanel anotherPanel = new JPanel();  /// multiple panel, 
        anotherPanel.setSize(100, 290);
        anotherPanel.setOpaque(false); // THIS IS VERY MUCH IMPORTANT
        anotherPanel.add(new JButton("Alpesh Gediya"));
        imagePanel.add(anotherPanel); //add panel to ImagePanel
        frame.add(imagePanel);
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

@SuppressWarnings("serial")
class ImagePanel extends JPanel {
    private Image image;
    private boolean tile;

    ImagePanel(Image image) {
        this.image = image;
        this.tile = false;
        final JCheckBox checkBox = new JCheckBox();
        checkBox.setAction(new AbstractAction("Tile") {
            public void actionPerformed(ActionEvent e) {
                tile = checkBox.isSelected();
                repaint();
            }
        });
        add(checkBox, BorderLayout.SOUTH);
    };

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (tile) {
            int iw = image.getWidth(this);
            int ih = image.getHeight(this);
            if (iw > 0 && ih > 0) {
                for (int x = 0; x < getWidth(); x += iw) {
                    for (int y = 0; y < getHeight(); y += ih) {
                        g.drawImage(image, x, y, iw, ih, this);
                    }
                }
            }
        } else {
            g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
        }
    }
}

编辑:

imagePanel.setLayout(new BorderLayout()); // setting layout as BorderLayout for panel as
                                          // Jpanel have FlowLayout as Default LayoutManager.