有图像作为JPanel的背景 [英] Having images as background of JPanel

查看:123
本文介绍了有图像作为JPanel的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中新的,我目前正在创建带有图形游戏。我有这个类,从的JFrame 延伸。在这个类中,我有一个需要作为背景的​​图片许多的JP​​anel 秒。据我所知,能在JPanel中画的图片,我需要从JPanel的扩展和类的的paintComponent 方法就做好了一个单独的类。但我不想做单独的类每个的JP​​anel ,我有太多的人;并与事实我只关心与背景。我怎样才能做到这一点?它是用一个匿名内部类?怎么样?

为了更好地理解我提供了一些code:

 公共GUI延伸的JFrame {
  私人JPanel的X;
  ...  公共GUI(){
   X =新JPanel();
   //将图像背景为x
  }


解决方案

为什么不把一个类,需要一个图片 ??

 公共类ImagePane继承JPanel {    私人形象画像;    公共ImagePane(形象画像){
        this.image =图像;
    }    @覆盖
    公共尺寸的get preferredSize(){
        返回形象== NULL?新尺寸(0,0):新的Dimension(image.getWidth(本),image.getHeight(本));
    }    @覆盖
    保护无效paintComponent(图形G){
        super.paintComponent方法(G);
        Graphics2D的G2D =(Graphics2D的)g.create();
        g2d.drawImage(图像,0,0,这一点);
        g2d.dispose();
    }
}

您甚至会提供有关它应涂提示。

这种方式,你可以简单地在任何你需要的时候创建一个实例

更新

<击>另一个问题是,为什么?

您可以只使用一个的JLabel 这将绘制你的图标没有任何额外的工作...

请参阅如何使用标签了解更多详情..

这实际上是一个糟糕的主意,因为的JLabel 不使用计算它的preferred大小时,它的子组件,它仅使用图像和大小当确定文本属性是preferred大小,这可能会导致被错误地大小的组件

I am new in Java and I am currently creating a game with graphics. I have this class that extends from JFrame. In this class, I have many JPanels that needs an image as background. As I know, to be able to paint images in the JPanel, I need to have a separate class that extends from JPanel and that class's paintComponent method will do the work. But I don't want to make separate classes for each JPanel, I have too many of them; and with the fact that I am only concerned with the background. How can I do this? is it with an anonymous inner class? How?

For better understanding I provided some code:

public GUI extends JFrame {
  private JPanel x;
  ...  

  public GUI() {
   x = new JPanel();
   // put an image background to x
  }

解决方案

Why not make a single class that takes a Image??

public class ImagePane extends JPanel {

    private Image image;

    public ImagePane(Image image) {
        this.image = image;
    }

    @Override
    public Dimension getPreferredSize() {
        return image == null ? new Dimension(0, 0) : new Dimension(image.getWidth(this), image.getHeight(this));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.drawImage(image, 0, 0, this);
        g2d.dispose();
    }
}

You would even provide hints about where it should be painted.

This way, you could simply create an instance when ever you needed it

Updated

The other question is, why?

You could just use a JLabel which will paint the icon for you without any additional work...

See How to use labels for more details...

This is actually a bad idea, as JLabel does NOT use it's child components when calculating it's preferred size, it only uses the size of the image and the text properties when determining it's preferred size, this can result in the component been sized incorrectly

这篇关于有图像作为JPanel的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆