Swing: resize Image but keep same size

北城以北 提交于 2020-01-17 01:18:46

问题


I want to be able to do something similar to resize canvas in gimp

I want to generate a bunch of images to a certain width.

I used

int width = (int)(size * fraction);
int height =(int)(size*icon.getIconHeight()/icon.getIconWidth()*fraction);
miniature = new ImageIcon(i.getScaledInstance(width, height, Image.SCALE_SMOOTH));

this goes well while I'm doing fraction 1 but I have 3 images that have the same source but are different size (1, 2/3, 1/3)

the problem is

I have and image A I want to create B, C and D such as the drawing inside respect the follow ratio B = A C = 2/3 A D = 1/3 A

but the image stays the same dimension A = B = C = D


回答1:


Assuming I'm following what you are trying to do...

Create a JLabel for each image:

JLable l = new JLabel(miniture);

Create a JLabel with a GridLayout:

JPanel p = new JPanel(new GridLayout());

Add the labels to the panel:

p.add(l);

The GridLayout will ensure that all the JLabels will have the same height and width.



来源:https://stackoverflow.com/questions/6297003/swing-resize-image-but-keep-same-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!