我遇到了一个问题。 我的图像太大,所以它放大了相应的JMenuItem。 我不想开发自行车
ImageIcon image = new ImageIcon(new ImageIcon("/home/template/img.jpg") .getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT));有没有其他方法可以实现这一点?
I've encountered a problem. My image is too big so it enlarges the corresponding JMenuItem. I don't want to develop bicycles like
ImageIcon image = new ImageIcon(new ImageIcon("/home/template/img.jpg") .getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT));Is there any other way to accomplish that?
最满意答案
如果你不想在代码中缩放图像(我认为这就是你在这个问题上的意思?)为什么不把这个尺寸的源图像制作成开头? 最简单的解决方案有时是最好的!
另外,getScaledInstance()通常是一个坏主意。 这解释了为什么,并提供更好的选项来调整图像大小。 如果你在其他地方使用图像,那么值得阅读该文章并使用更好的技术(如graphics.drawImage())对其进行缩放,但如果您只是在菜单中使用它,则可能调整源图像的大小最好的事情要做。
如果你打算调整大小:
BufferedImage image = ImageIO.read("img.jpg"); BufferedImage ret = new BufferedImage(32,32,BufferedImage.TYPE_RGB); ret.getGraphics().drawImage(image,0,0,32,32,null);像这样的东西应该给你的形象,你可以使用ret创建你的ImageIcon。
If you don't want to scale the image in code (I presume that's what you mean in the question?) why not just make the source image that size to start with? The simplest solution is sometimes the best!
Also, getScaledInstance() is generally a bad idea. This explains why and gives better options for resizing the image. If you're using the image elsewhere then it's worth reading up on that article and scaling it using a better technique (such as graphics.drawImage()) but if you're just using it in the menu then resizing the source image is probably the best thing to do.
If you are going to resize:
BufferedImage image = ImageIO.read("img.jpg"); BufferedImage ret = new BufferedImage(32,32,BufferedImage.TYPE_RGB); ret.getGraphics().drawImage(image,0,0,32,32,null);Something like that should give you the image, you can then create your ImageIcon using ret.
更多推荐
发布评论