Reputation: 1
So I have an assignment where I need to create a catalog. The catalog needs to have a list, an image and a description. My entire code works, so I have no issue with the coding as such.
I do have an issue with the image size. How do I take care of images on a java gui program to make them all into one size when it is running. Please let me know :D
Upvotes: 0
Views: 1242
Reputation: 7324
There is a method (image = imgobj.getScaledInstance(width, height, hints)
) in awt.Image class which provides re-sizing capabilities very nicely, I always use this to re-size my images when I need. Please see here some examples :-), I hope it will work for you, it is the most convenient way to scale images I have ever seen. create a method pass the image to the method and size of the image you want and return the image back in return to reuse the code ;)
Upvotes: 0
Reputation: 285405
When you read in an image, create a new BufferedImage that is the exact size that you desire, get it's Graphics object via getGraphics()
, draw the original image into the new image using Graphics#drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
where x and y are 0 and width and height are from the dimensions of the new image, dispose()
of the Graphics object, and then display the new Image as an ImageIcon in a JLabel. Make sure though that the original image is the same size or larger than the new one, else your images will look gawd-awful.
For example, and note that this code may not be exactly correct since I don't have my IDE up:
BufferedImage originalImage = ImageIO.read(something); // read in original image
// create new empty image of desired size
BufferedImage newImage = new BufferedImage(desiredWidth, desiredHeight, BufferedImage.TYPE_INT_ARGB);
Graphics g = newImage.getGraphics(); // get its graphics object
// draw old image into new image
g.drawImage(originalImage, 0, 0, desiredWidth, desiredHeight, null);
g.dispose(); // get rid of Graphics object
// create ImageIcon and put in JLabel to display
Icon newIcon = new ImageIcon(newImage);
myJLabel.setIcon(newIcon);
Upvotes: 1
Reputation: 364
I would propably create a JPanel to draw on one Image, and then work with the method:
myPanel.setSize(new Dimension(x,y))
or
myPanel.setPreferredSize(new Dimension....)
Upvotes: 0