getWidth and getHeight are predefined abstract methods present in Image class. getWidth method determines the width of an image. getHeight method determines the height of an image. Return type of this two method are int type. Syntax- public abstract int get Width (java.awt.image. ImageObserver) public abstract int get Height java.awt.image. ImageObserver).
setFont and setColor are predefined abstract methods present in Graphics class. These methods are used to set the font and color of the graphics context respectively. Syntax-public abstract void setFont (java.awt.Font) public abstract void setColor (java.awt.Color).
Here is the Java Example for GetImageSizeExample:
import java.awt.*;
import java.awt.event.*;
import java.util.Locale;
public class GetImageSizeExample extends Frame
{
Image image;
String Picture = "DineshThakur.jpg";
String Name = "Dinesh Thakur";
int width,height;
public GetImageSizeExample()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void paint(Graphics g)
{
Toolkit tool = Toolkit.getDefaultToolkit();
image = tool.getImage(Picture);
width=image.getWidth(this);
height=image.getHeight(this);
this.setSize(width+320,height+250);
g.drawImage(image,150,120,this);
g.setColor(new Color(0,0,180));
g.setFont(new Font("Times New Roman",1,12));
g.drawString(Name.toUpperCase(Locale.ENGLISH),125,185);
g.setFont(new Font("Times new Roman",1,10));
g.drawString("My Pic Size: "+width+ "*"+height,135, height+160);
}
public static void main(String args[]) throws Exception
{
GetImageSizeExample GIS = new GetImageSizeExample();
GIS.setVisible(true);
GIS.setSize(350,250);
GIS.setLocation(200, 100);
}
}