如何将9补丁图像用作JPanel上的背景? [英] How use a 9-patch image as background on a JPanel?

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

问题描述

我正在寻找一种使用 9-patch 文件的方式javax.swing.JPanel的背景. 因此,当JPanel的尺寸更改时,背景图像会自动调整其大小.

I'm searching a way for use a 9-patch file as background for javax.swing.JPanel. So that the background image automatically resizes itself when the dimensions of the JPanel change.

有可能吗?还是我必须创建图像的所有部分,并在侦听器触发时手动调整其中一些的大小(如下面的代码所示)?

Is it possible? Or I have to create all the pieces of the image and manually resize some of them when the listener triggers (like in the piece of code that follows)?

public class JPanelWithNinePatchBackground extends JPanel { 
{ 
    /* Define all parts of the background like BufferedImage */

    addComponentListener(new ComponentAdapter() { 
        public void componentResized(ComponentEvent e) { 

            // Resize images . . . 

            JPanelWithBackground.this.repaint(); 
        } 
    });

    /* . . . */

}

推荐答案

是的,请使用 RCTile 是一个例子.

Yes, use either of the drawImage() implementations that let you specify the corners of the destination rectangle. The method will complete faster if the source and destination rectangles match in size. RCTile is an example.

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

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