How use a 9-patch image as background on a JPanel?

寵の児 提交于 2020-01-15 06:42:49

问题


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

    /* . . . */

}

回答1:


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.



来源:https://stackoverflow.com/questions/18161768/how-use-a-9-patch-image-as-background-on-a-jpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!