![]() ![]() Then, we set the size of the window note that this size should be more than the image’s height and width to show the picture completely.įinally, to show the image, we create an object of JLabel and call the section class that sets the image as an icon. We set the layout of the frame object as FlowLayout, which arranges the components in a line. Once it’s done, we create a window to show the image using the JFrame object. Here, we use the Image.SCALE_DEFAULT function, which tells the class to use the default sampling algorithm. The getScaledInstance function takes three arguments: the first two are the width and height in pixels that we want to give the image, and the last argument is the algorithm to use for scaling. Once we get the bufferedImage object, we use it to call the getScaledInstance() method that creates a scaled version of the image. ![]() We get our image using the new File() method that calls the File class constructor and passes the image location as an argument in it. Here, the ImageIO is a class that contains static methods like read and write to perform Input and Output operations. To read the image, we use the ImageIO.read() function. The BufferedImage class extends the Image class representing the graphical images as an array of rectangular pixels. ![]() Here, we create an object bufferedImage of the BufferedImage class. In the first example, we resize an image and show it inside a frame without saving it anywhere. Resize and Show an Image in Java Using BufferedImage.getScaledInstance() In the following examples, we’ll demonstrate how you can resize an image in Java using three techniques. Java can perform several operations of different types like reading, writing, or deleting a file.
0 Comments
Leave a Reply. |