Java调整图像中心大小
前几天我在寻找其他东西,感觉我记得遇到了一种调整图像大小但保持图像两侧的区域不变的方法,这样无论长度如何,图像的圆形边缘看起来总是相同的缩放图像的。经过更多的谷歌搜索后,我找不到我记得看到的内容,所以我想知道这是否可能以及是否有人有信息。
具体来说,我拥有的是一个自定义边框,其看起来像一个带有标签的悬挂选项卡。该图像顶部为方形,底部边缘为圆形。我想保持底部圆角,无论其上标签的长度如何,但将中心拉伸约 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());
}
};
I was looking something else up the other day and feel like I remember running across a way to resize an image but keeping an area along the sides of the image unaltered so that the rounded edges of the images will always look the same no matter the length of the scaled image. After some more googling I cannot find what I remember seeing so I would like to know if this is possible and if anyone has and info.
Specifically what I have is a custom border with what is meant to look like a hanging tab with a label on it. The image is square on top and rounded on the bottom edges. I would like to keep the bottom rounded corners no matter the length of the labels on it but stretch the center ~90% to accommodate larger labels. I assume it would be in this code somewhere where i can enter some insets? in the resizing method.
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());
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,鉴于缺乏公认的方法来做到这一点,我提出了这个解决方案,并为其他想要做类似事情的人发布了代码。此代码将拍摄一张图像并将其分为 9 个部分。 4 个角将保持不变。 4条边的中间将沿着边被拉伸或压缩。中心部分将在两个方向上拉伸或压缩。当然,对我来说,这个课程的目的是压缩带有圆角的较大图像,但保留圆角,当图像简单地缩小时,圆角几乎消失。显然,这对于像图片这样的图像没有什么好处,但对于具有自定义绘画和圆角边缘的组件来说,这似乎效果很好。
此类没有构造函数,您只需调用更改后的图像即可。使用将是
这将返回拉伸到所需尺寸的图像,其中角保持相同,并且仅使用 insets 参数在一维中修改边,以确定在单个维度中修改的边缘周围的量。可能有一种棘手的方法来迭代图像列表,但这样我可以更好地了解发生了什么。
}
OK, given the lack of accepted method to do this I have come up with this solution and am posting the code for anyone else who would like to do something similar. This code will take an image and divide it into 9 sections. The 4 corners will be left alone. the middles of the 4 edges will be stretched or compressed along the edge. The center section will be stretched or compressed in both directions. Of course the point of this class for me was to compress a larger image with rounded corners but keep the rounded corners which nearly disappeared when the image was simply scaled down. Obviously this will do little good with an image like a picture but for components with custom painting and rounded edges this seems to be working good.
There is no constructor for this class you can just simply call for an altered image. the use would be
This will return an image stretched to the desired dimension with the corners remaining the same and the sides only being modified in 1 dimension using the insets parameter to determine the amount around the edge that is modified in a single dimension. There is probably a tricky way to iterate through the lists of images but this way I could better see what was going on.
}