问题
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