好的,鉴于缺乏可接受的方法来做到这一点,我想出了这个解决方案,并将代码发布给任何想要做类似事情的人。此代码将拍摄一张图像并将其分为 9 个部分。4个角将被单独留下。4 条边的中间将沿边拉伸或压缩。中心部分将在两个方向上拉伸或压缩。当然,对我来说,这门课的重点是用圆角压缩更大的图像,但保留圆角,当图像被简单地缩小时,圆角几乎消失了。显然,这对于像图片这样的图像没有什么好处,但对于具有自定义绘画和圆角边缘的组件,这似乎工作得很好。
此类没有构造函数,您只需调用更改后的图像即可。用途是
StretchedImage.stretch(image, new Insets(t,l,b,r), new Dimension(w,h), BufferedImage.TYPE_INT_ARGB);
这将返回一个拉伸到所需尺寸的图像,其中角保持不变,并且仅使用 insets 参数在一维中修改边以确定在单个维度中修改的边缘周围的量。遍历图像列表可能有一种棘手的方法,但这样我可以更好地了解发生了什么。
public class StretchedImage{
public static Image stretch(Image image, Insets ins, Dimension dim, int hints){
//debugcode
//System.out.println(dim);
//load image into bufferedImage
BufferedImage bi = toBufferedImage(image, hints);
//create 2d bufferedImage array to hold the 9 images
Image[][] img = new Image[3][3];
//split Image into 9 subsections
img[0][0] = bi.getSubimage(0, 0, ins.left, ins.top);
img[0][1] = bi.getSubimage(ins.left, 0, bi.getWidth() - ins.left - ins.right, ins.top);
img[0][2] = bi.getSubimage(bi.getWidth() - ins.right, 0, ins.right, ins.top);
img[1][0] = bi.getSubimage(0, ins.top, ins.left, bi.getHeight() - ins.top - ins.bottom);
img[1][1] = bi.getSubimage(ins.left, ins.top, bi.getWidth() - ins.left - ins.right, bi.getHeight() - ins.top - ins.bottom);
img[1][2] = bi.getSubimage(bi.getWidth() - ins.right, ins.top, ins.right, bi.getHeight() - ins.top - ins.bottom);
img[2][0] = bi.getSubimage(0, bi.getHeight() - ins.bottom, ins.left, ins.bottom);
img[2][1] = bi.getSubimage(ins.left, bi.getHeight() - ins.bottom, bi.getWidth() - ins.left - ins.right, ins.bottom);
img[2][2] = bi.getSubimage(bi.getWidth() - ins.right, bi.getHeight() - ins.bottom, ins.right, ins.bottom);
//determine the width and height of the sections that will be stretched
//only the center section will have both dimensions changed
int w = dim.width - ins.left - ins.right;
int h = dim.height - ins.top - ins.bottom;
//Stretch the proper sections
img[0][1] = img[0][1].getScaledInstance(w, img[0][1].getHeight(null), hints);
img[1][0] = img[1][0].getScaledInstance(img[1][0].getWidth(null), h, hints);
img[1][1] = img[1][1].getScaledInstance(w, h, hints);
img[1][2] = img[1][2].getScaledInstance(img[1][2].getWidth(null), h, hints);
img[2][1] = img[2][1].getScaledInstance(w, img[2][1].getHeight(null), hints);
//for loop is debug code
//for(int i = 0; i < 3; i++){
// for(int j = 0; j < 3; j++){
// System.out.println(i + " " + j + " " + img[i][j].getWidth() + "," + img[i][j].getHeight());
// }
//}
//create a new bufferedImage to hold the final image
BufferedImage finalImage = new BufferedImage(dim.width, dim.height, hints);
Graphics g = finalImage.getGraphics();
//draw the peices to the final image
g.drawImage(img[0][0], 0, 0, null);
g.drawImage(img[0][1], ins.left, 0, null);
g.drawImage(img[0][2], dim.width - ins.right, 0, null);
g.drawImage(img[1][0], 0, ins.top, null);
g.drawImage(img[1][1], ins.left, ins.top, null);
g.drawImage(img[1][2], dim.width - ins.right, ins.top, null);
g.drawImage(img[2][0], 0, dim.height - ins.bottom, null);
g.drawImage(img[2][1], ins.left, dim.height - ins.bottom, null);
g.drawImage(img[2][2], dim.width - ins.right, dim.height - ins.bottom, null);
return (Image)finalImage;
}
// This method returns a buffered image with the contents of an image
public static BufferedImage toBufferedImage(Image image, int hints) {
BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), hints);
bi.getGraphics().drawImage(image, 0, 0, null);
return bi;
}
}