import javax.swing.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
class Two {
public static void main(String args[]) throws IOException
{
BufferedImage img = ImageIO.read(new File("index.jpg"));
JFrame frmOne = new JFrame("FACEBOOK");
frmOne.setIconImage(img);
frmOne.setExtendedState(JFrame.MAXIMIZED_BOTH);
frmOne.setVisible(true);
}
}
In this code here, a JPG image is displayed instead of a JFrame
icon. However, I want to know about the functionality of the line BufferedImage img = ImageIO.read(new File("index.jpg"));
. Any help would be appreciated, I am new to swing.
The code BufferedImage img = ImageIO.read(new File("index.jpg"));
works as follows:
BufferedImage img = ...
says the rest of the line should be stored to the variable.
ImageIO.read(...
says read an image from given file
new File("index.jpg")
says find the File named "index.jpg"
So overall, it loads an image out of the index.jpg file and stores this image into the BufferedImage img
. You then later just call frmOne.setIconImage(img)
which takes the image you read out of the file, and sets it to be the frame's icon.
The setIconImage
method changes the image icon that is displayed in the top left side of the JFrame
and when switching between applications. Another choice is:
frame.setIconImage(new ImageIcon("filename").getImage());
BufferedImage img = ImageIO.read(new File("index.jpg"));
Analysis:
BufferedImage img
this creates a BufferedImage
variable called img
, We can think of this as an Object which holds the data needed for java to display an image, a BufferedImage
as per docs :
The
BufferedImage
subclass describes anImage
with an accessible buffer of image data. ABufferedImage
is comprised of aColorModel
and a Raster of image data.
ImageIO
This class basically contains methods to help us read and write image without having to write our own each time, As per docs :
A class containing static convenience methods for locating
ImageReaders
andImageWriters
, and performing simple encoding and decoding.
read(File input)
This is a public
static
method inside ImageIO
thus can be accessed without the new
keyword. It allows us to read in data of the file we want to use as a Image
and returns the data it read in (thus we save it in a variable) as per docs :
Returns a BufferedImage as the result of decoding a supplied File with an ImageReader chosen automatically from among those currently registered. The File is wrapped in an ImageInputStream.
Parameters: input - a File to read from.
Returns: a BufferedImage containing the decoded contents of the input, or null.
Throws: IllegalArgumentException - if input is null. IOException - if an error occurs during reading.
new File(String filename)
is a non static method in the class File and thus has to be accessed with a newly created instance ( new
). It allows us to create a reference to the file, so that we can perform operations on the File
instance (ie reading writing etc) as per docs :
Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname.
Parameters: pathname - A pathname string
Throws: NullPointerException - If the pathname argument is null
Now when you call setIconImage(img)
all the data we read from the file (which is our picture and was converted to an BufferedImage
) will be used to display the picture as the JFrame
s Icon.
Another way to do it is:
// Create frame
String title = "Frame Title";
JFrame frame = new JFrame(title);
// Set icon
Image icon = Toolkit.getDefaultToolkit().getImage("icon.gif");
frame.setIconImage(icon);
Need to mention that you'll have to handle an exception. IMHO, the code might be better written as so:
BufferedImage img = null;
try {
img = ImageIO.read(new File("youricon.png")); // get icon for JFrame
} catch (IOException e) {
e.printStackTrace();
}
frame.setIconImage(img); // set JFrame icon
Also, as you read the docs take note that .ico files are not handled by ImageIO(). Use .jpg, .png, .bmp or .gif files. See http://docs.oracle.com/javase/7/docs/api/javax/imageio/package-summary.html for further information.
HTH
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.