Hello everyone..
Is it possible to change the size (i.e.)height and width of an image through java.If it is,please do me a favor.
Thanks.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hello everyone..
Is it possible to change the size (i.e.)height and width of an image through java.If it is,please do me a favor.
Thanks.
Short answer, yes.
Longer answer, yes, I have the following code in my ImageUtility class.
public static BufferedImage resizeImage(final Image image, int width, int height) { final BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); final Graphics2D graphics2D = bufferedImage.createGraphics(); graphics2D.setComposite(AlphaComposite.Src); graphics2D.drawImage(image, 0, 0, width, height, null); graphics2D.dispose(); return bufferedImage; }
// Json
I use this method.
public static BufferedImage resizeImage(final Image image, int width, int height) {
final BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
final Graphics2D graphics2D = bufferedImage.createGraphics();
graphics2D.setComposite(AlphaComposite.Src);
graphics2D.drawImage(image, 0, 0, width, height, null);
graphics2D.dispose();
return bufferedImage;
}
Also i called this method when loading the image like,
Image imag = ((ImageIcon)ImageStore.applet_loadedImages.get("bg-2.png")).getImage();
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int width =(int) d.getWidth();
int height =(int) d.getHeight();
resizeImage(imag,width,height);
But its not resizing the image.In my code image is in label.Is anything wrong in my code?
The resizeImage method will return you a new image which you will have to use, it doesn't change your current image.
Image imag = ((ImageIcon)ImageStore.applet_loadedImages.get("bg-2.png")).getImage(); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); int width =(int) d.getWidth(); int height =(int) d.getHeight(); final BufferedImage newImage = resizeImage(imag,width,height);
Now use the newImage variable.
// Json
Thank u all.Its working.