将图像设置为JPanel背景的最简单方法

Jos*_*tin 16 java swing background image jpanel

如何在不创建新类或方法的情况下将背景图像添加到JPanel,只需将其与JPanel的其余属性一起插入即可?

我试图使用图像设置JPanel的背景,但是,我发现的每个示例似乎都建议使用自己的类扩展面板.

我一直在寻找一种方法来简单地添加图像,而无需创建一个全新的类并在同一个方法中(尝试保持组织和简单).

以下是设置JPanel的方法示例:

public static JPanel drawGamePanel(){
    //Create game panel and attributes
    JPanel gamePanel = new JPanel();
    Image background = Toolkit.getDefaultToolkit().createImage("Background.png");
    gamePanel.drawImage(background, 0, 0, null);
    //Set Return
    return gamePanel;
}
Run Code Online (Sandbox Code Playgroud)

Sag*_*age 26

我试图使用图像设置JPanel的背景,但是,我发现的每个示例似乎都建议使用自己的类来扩展面板

是的,你将不得不扩展JPanel和覆盖paintcomponent(Graphics g)这样做的功能.

@Override
  protected void paintComponent(Graphics g) {

    super.paintComponent(g);
        g.drawImage(bgImage, 0, 0, null);
}
Run Code Online (Sandbox Code Playgroud)

我一直在寻找一种方法来简单地添加图像,而无需创建一个全新的类并在同一个方法中(尝试保持组织和简单).

您可以使用其他组件,允许直接添加图像作为图标,例如,JLabel如果您需要.

ImageIcon icon = new ImageIcon(imgURL); 
JLabel thumb = new JLabel();
thumb.setIcon(icon);
Run Code Online (Sandbox Code Playgroud)

但是在试图保持组织和简单的支架中再次!是什么让你认为只是创造一个新的课程将导致你一个混乱的世界?


cam*_*ckr 15

将图像设置为JPanel背景的最简单方法

不要使用JPanel.只需使用带有Icon的JLabel,就可以不需要自定义代码.

有关详细信息,请参阅背景面板以及将在具有3种不同绘画选项的JPanel上绘制图像的解决方案:

  1. 缩放
  2. 瓷砖
  3. 实际


Max*_*tin 5

据我所知,您可以这样做的paintComponent方法是覆盖需要继承的方法JPanel

 @Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g); // paint the background image and scale it to fill the entire space
    g.drawImage(/*....*/);
}
Run Code Online (Sandbox Code Playgroud)

创建第二个自定义JPanel并放置的另一种方式(有点复杂)是您主要的背景

图像面板

public class ImagePanel extends JPanel
{
private static final long serialVersionUID = 1L;
private Image image = null;
private int iWidth2;
private int iHeight2;

public ImagePanel(Image image)
{
    this.image = image;
    this.iWidth2 = image.getWidth(this)/2;
    this.iHeight2 = image.getHeight(this)/2;
}


public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    if (image != null)
    {
        int x = this.getParent().getWidth()/2 - iWidth2;
        int y = this.getParent().getHeight()/2 - iHeight2;
        g.drawImage(image,x,y,this);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

空面板

public class EmptyPanel extends JPanel{

private static final long serialVersionUID = 1L;

public EmptyPanel() {
    super();
    init();
}

@Override
public boolean isOptimizedDrawingEnabled() {
    return false;
}


public void init(){

    LayoutManager overlay = new OverlayLayout(this);
    this.setLayout(overlay);

    ImagePanel iPanel = new ImagePanel(new IconToImage(IconFactory.BG_CENTER).getImage());
    iPanel.setLayout(new BorderLayout());   
    this.add(iPanel);
    iPanel.setOpaque(false);                
}
}
Run Code Online (Sandbox Code Playgroud)

图标到图像

public class IconToImage {

Icon icon;
Image image;


public IconToImage(Icon icon) {
    this.icon = icon;
    image = iconToImage();
}

public Image iconToImage() { 
    if (icon instanceof ImageIcon) { 
        return ((ImageIcon)icon).getImage(); 
    } else { 
        int w = icon.getIconWidth(); 
        int h = icon.getIconHeight(); 
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
        GraphicsDevice gd = ge.getDefaultScreenDevice(); 
        GraphicsConfiguration gc = gd.getDefaultConfiguration(); 
        BufferedImage image = gc.createCompatibleImage(w, h); 
        Graphics2D g = image.createGraphics(); 
        icon.paintIcon(null, g, 0, 0); 
        g.dispose(); 
        return image; 
    } 
}


/**
 * @return the image
 */
public Image getImage() {
    return image;
}
}
Run Code Online (Sandbox Code Playgroud)