1

How can i add repeat background image in 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);
    }

}

I tried many examples, but not found code that background with a repeat.

Update, that is my GUI and ImagePanel. The 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);

    }

}

The 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);
                }
            }
        }
    }

}

So, the matter with the background with repeat resolved, but in this way i cant use all of the BorderLayouts because i adding panels into ImagePanel and not to JFrame GUI.

4

1 回答 1

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.
于 2013-05-25T14:59:54.837 回答