Java动态调整图像大小以适合gridlayout中的网格 [英] java resize image dynamically to fit grids in gridlayout

查看:124
本文介绍了Java动态调整图像大小以适合gridlayout中的网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JButton的自定义子类制作国际象棋类型的棋盘.我的问题是我的国际象棋图像太小了.有没有一种方法可以使图像缩放到恰好在我的网格布局中每个网格的大小?如果我调整Jframe的大小,那么网格也会改变大小.有没有一种方法可以使图像在调整整个帧的大小时动态调整大小?

I want to make a chess type board using a custom subclass of JButton. My problem is that my images of the chess pieces are a bit too small. Is there a way I can get the image to scale to exactly the size of each grid in my gridlayout? If I resize the Jframe, the grids will change size as well. Is there a way to get the image to resize dynamically upon resizing of the whole frame?

推荐答案

您对此有3个选择

1)使用Gimp,Photoshop等调整图像本身的大小.

1) Resize the images themselves using Gimp, Photoshop, etc.

2)动态创建图标

Image i = icon.getImage();
if(i != null){
   int width = (int)(size * fraction);
   int height =(int)(size*icon.getIconHeight()/icon.getIconWidth()*fraction);
   miniature = new ImageIcon(i.getScaledInstance(width, height, Image.SCALE_SMOOTH));
}

3)在框架的油漆上可以使用刻度

3) on the paint of your frame you can use scale

private void scaledDrawing(Graphics g, float scale){
   Graphics2D g2 = (Graphics2D) g;
   AffineTransform at = new AffineTransform();
   AffineTransform save = g2.getTransform();
   at.setToIdentity();
   at.scale(goa.getScale().x, goa.getScale().y);
   g2.transform(at);
   image.paintIcon(c, g2);
   g2.setTransform(save);
}

这篇关于Java动态调整图像大小以适合gridlayout中的网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆