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

I am looking for a way to use 9-patch file as background for javax.swing.JPanel. So that the background image automatically changes when resizing JPanel.

Is it possible? Or do I need to create all parts of the image and manually resize some of them when the listener triggers (as in the following code fragment)?

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(); } }); /* . . . */ } 
+2
source share
1 answer

Yes, use any of drawImage() , which allows you to specify the corners of the destination rectangle. The method will be executed faster if the source and target rectangles are the same size. RCTile is an example.

+1
source

Source: https://habr.com/ru/post/958688/


All Articles