![]() You could override the paintIcon method on the icon itself - ImageIcon icon = new ImageIcon(. The preferred method of achieving the task at hand is going to depend on the requirements of whether the original image is going to be needed at a later time or not. However, as the JLabel is going to use a scaled-down image of the original, it would mean that the amount of memory required would be reduced (if that a concern) and original image would not have to be resized every time image is being displayed, as would be the case with overriding the paintComponent method. The limitation to this method is, that if the JLabel is resized in any way, then the image contained by the JLabel will not be resized. Image img icon.getImage () Now create a buffered image the same size as the image: BufferedImage bi new BufferedImage (img.getWidth (null), img. Now because of some specific requirement in the project i resize (enlarged/reduce) the same image and try to reload the image through the browse button. ![]() ![]() Now, one could create an ImageIcon to use on the JLabel. In the paintComponent method of JPanel I get the width, height of the image icon and set the preferred size of the panel to display the full image. Img.getScaledInstance(lblDisPic.getWidth(), lblDisPic.getHeight(), null) Then, the loaded image can be resized to the dimension of the JLabel, using Image.getScaledInstance. A base size denoting the size (in the user space) of the rendered image in 1x scale should be provided. Image img = ImageIO.read(fc.getSelectedFile()) As SVG icons can be scaled arbitrarily, they provide better results in HiDPI environments or when used in combination with bigger screen fonts (e.g., in presentation mode). One way could be to change the actionPerformed method, so the image will be loaded from the specified file, using ImageIO.read method to read the image, then resizing the image before an ImageIcon is created. Given the example code, one approach is to resize the image to display on the JLabel before the setIcon method is called.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |