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.