简体   繁体   中英

Swing: resize Image but keep same size

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 AI want to create B, C and D such as the drawing inside respect the follow ratio B = A C = 2/3 AD = 1/3 A

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM