[英]Java resizing center of image
前几天我在看别的东西,感觉我记得跑过一种方法来调整图像的大小,但保持图像两侧的区域不变,这样无论长度如何,图像的圆形边缘看起来总是一样的的缩放图像。 经过一番谷歌搜索后,我找不到我记得看到的东西,所以我想知道这是否可能以及是否有人有信息。
具体来说,我所拥有的是一个自定义边框,它看起来像一个带有 label 的悬挂标签。 图像顶部为方形,底部边缘为圆形。 无论标签的长度如何,我都想保持底部圆角,但将中心拉伸约 90% 以容纳更大的标签。 我假设它会在这段代码中我可以输入一些插图的地方? 在调整大小的方法中。
titleLabel = new JLabel(title){
public void paint(Graphics g){
g.drawImage(tabImg, 0, 0, this.getSize().width, this.getSize().height, null);
g.drawString(this.getText(), 1, this.getFont().getSize());
}
};
好的,鉴于缺乏可接受的方法来做到这一点,我想出了这个解决方案,并将代码发布给任何想要做类似事情的人。 此代码将拍摄一张图像并将其分为 9 个部分。 4个角将被单独留下。 4 条边的中间将沿边拉伸或压缩。 中心部分将在两个方向上拉伸或压缩。 当然,对我来说,这个 class 的目的是压缩带有圆角的较大图像,但保留圆角,当图像被简单地缩小时,圆角几乎消失了。 显然,这对于像图片这样的图像没有什么好处,但对于具有自定义绘画和圆角边缘的组件,这似乎工作得很好。
此 class 没有构造函数,您只需调用更改后的图像即可。 用途是
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.