gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:22:17 28 4
gpt4 key购买 nike

如何在 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。图形用户界面:

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

}

}

图像面板:

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。

最佳答案

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.

关于java - 如何在 Jframe 中添加重复背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16750709/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com