I've encountered a problem. My image is too big so it enlarges the corresponding JMenuItem. I don't want to develop bicycles like
ImageIcon image = new ImageIcon(new ImageIcon("/home/template/img.jpg")
.getImage().getScaledInstance(32, 32, Image.SCALE_DEFAULT));
Is there any other way to accomplish that?
If you don't want to scale the image in code (I presume that's what you mean in the question?), why not just make the source image that size to start with? The simplest solution is sometimes the best!
Also, getScaledInstance() is generally a bad idea. This explains why and gives better options for resizing the image. If you're using the image elsewhere, then it's worth reading up on that article and scaling it using a better technique (such as graphics.drawImage()). But if you're just using it in the menu, then resizing the source image is probably the best thing to do.
If you are going to resize:
BufferedImage image = ImageIO.read("img.jpg");
BufferedImage ret = new BufferedImage(32,32,BufferedImage.TYPE_RGB);
ret.getGraphics().drawImage(image,0,0,32,32,null);
Something like that should give you the image, you can then create your ImageIcon using ret.
Related
I added an image like this
BufferedImage eastFramerPicture = ImageIO.read(new File("src\\Images\\farmer.png"));
JLabel eastFarmer = new JLabel(new ImageIcon(eastFramerPicture));
but how can I make the image fill the panel, the image provided shows the problem that I am facing ( the colouring is used to indicate the regions of East, West, Center and south )
enter image description here.
What am I doing wrong ? Please if something is not clear comment it out, I will reply.
Thanks for your time
The answer will depend on a number of factors...
Do you want maintain the aspect ratio?
Do you want tile the image to fill the area
If you want to maintain the aspect ratio, do you want to fit or fill the available space.
The simple solution would be to use Image#getScaledInstance, but if you go that route, you should have a read of The Perils of Image.getScaledInstance
BufferedImage eastFramerPicture = ImageIO.read(getClass().getResource("/Images/farmer.png"));
Image scaled = eastFramerPicture.getScaledInstance(600, 600, Image.SCALE_SMOOTH);
You could also make use of a library like imgsclr
Example of maintaining ascept ratio
Example of tiling
Check out the Background Panel. You can display an image:
at its normal size
scaled
tiled
So try each option to see what you like best.
Or you could also use the Stretch Icon. The Icon in the label will be scaled to fill the available space.
Both solutions provide reusable code so you don't need to write custom code every time you have this type of requirement. The code is also dynamic and will adjust as the frame is resized.
Try to use setBounds(x,y,width,height);
I was trying to do really the same functionality of resizing images like in MS Word.
I want to resize BufferedImage but I’m losing some information during process of resizing.
I tried to implement two approaches, but both produced same result.
Before any resizing:
Picture after few resize actions in my application:
First approach:
image = Thumbnails.of(image).size(w,h).asBufferedImage();
Second approach:
image = toBufferedImage(image.getScaledInstance(w, h, Image.SCALE_SMOOTH));
image is instance of BufferedImage, w is new width of image and h is new hight of image
Any idea, what I’m doing wrong?
You're constantly losing information from your image after each resizing attempt. If you want solution (like in MS Word) you have to keep somewhere original image but show only resized copy.
The best solution would be creating an object to store original image and making resized copy on demand. You can improve quality of this solution adding simple cache so you don't actually generate another resized copy of your image for every request but only if your application demands image with different height or width than last time.
I hope I helped you a bit.
Could anyone explain to me in noob way what the difference is betweeen ImageIcon and Image classes/objects in Java? Thanks
Their nature and application is different. Image is an abstract superclass of all classes that represent graphical images. ImageIcon is an implementation of Icon interface that uses Image as its source.
Edit: Think of an Image as something that could be rendered and an ImageIcon as something that will be rendered as an Icon when its paintIcon() method is called.
Edit: The links above will take you to the JDK 6 api. These links will take you to the JDK 8 api: Image and ImageIcon.
You can scale and save Image, but you can't do it with ImageIcon. For creating pictures in your GUI you usually have to use ImageIcon, but if you don't wanna do that, Image should be better.
hy,
I've a little probleme with a graphics. I've an image witch I resize like this :
Image BigImage = img.getScaledInstance(115, 154, java.awt.Image.SCALE_SMOOTH);
and I tried ti draw it on a graphic:
g2.drawImage(BigImage, 0, 0, null);
it's unfortunately not working.
it only work if i add ImageIcon te = new ImageIcon(BigImage);
Image BigImage = img.getScaledInstance(115, 154, java.awt.Image.SCALE_SMOOTH);
ImageIcon te = new ImageIcon(BigImage);
g2.drawImage(BigImage, 0, 0, null);
Do you have any idea why? Have you another way to do it?
I can't say right now why your code doesn't work, but you can directly draw a scaled instance of the image using this drawImage method. It worked for me.
The documenttion for Image.getScaledInstance() states in part..
The new Image object may be loaded asynchronously even if the original source image has already been loaded completely.
(My emphasis)
One way to correct the problem is to add the image to a MediaTracker to ensure it is loaded before rendering. But most importantly, do not do any of this from within the paint()/paintComponent() method, which is what I suspect given the code snippet. The instantiation of the ImageIcon was probably slowing down the execution just enough for the image to load.
Another tip I will offer is that you will get better help sooner if you post an SSCCE. This is particularly useful for image based examples, since if you'd hot-linked to an image available on the net, we could've seen just how big the image was that was being rescaled. ;)
i'm trying to create a program that generates images for use as multi-screen backgrounds, i'm doing this targeted at windows (in my case, 7 so that basically i can get images to change without seeing the same image on two different screens)
in my program, i read multiple image input files and compile them into a single output image that is the total size of the desktop (including black areas not seen on screens)
my question is, what class/methods are good for cropping/resizing/pasting into a new image in java because i'm coming across so many image manipulation classes and they all seem to do one tiny thing.
i will not be modifying any of the images beyond resize or crop and putting it into a certain position in the new (initially blank) image.
code can be made available as i plan to release it at some later point for whoever may like/need it.
thank you in advance, if this question has been answered, my apologies but i DID have a look around.
I do not know if this is the best method, but it is quite easy:
// load an image
Image image = javax.imageio.ImageIO.read(new File("someimage.png");
// resize it
image = image.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
// create a new image to render to
BufferedImage newimg = new BufferedImage(200,100,BufferedImage.TYPE_INT_ARGB);
// get graphics to draw..
Graphics2D graphics =newimg.createGraphics();
//draw the other image on it
graphics.drawImage(image,0,0,null);
graphics.drawImage(image,100,0,null);
graphics.fillOval(20,20,40,40); //making it a bit ugly ;)
//export the new image
ImageIO.write(newimg,"png",new File("output.png"));
//done!
For simplicity I dropped all checks, exception handling, etc.