(Copy And Paste Source Code Below Into Wordprocessing Software Like Notepad. Save it as java file. For file name, just use file name like given. Compile and execute it)
***********************************************
COMPLETE SOURCE CODE FOR ReadImageFileInJava.java
import java.awt.Image;
import java.awt.Toolkit;
public class ReadImageFileInJava
{
public static void main(String[]args)
{
//READ AN IMAGE FILE, sample_picture.jpg
//METHOD getImage() CAN READ IMAGE FILE WITH FORMAT gif, jpg and png
Image a=Toolkit.getDefaultToolkit().getImage("sample_picture.jpg");
}
}
GET WIDTH AND HEIGHT FOR AN IMAGE IN JAVA
***********************************************
COMPLETE SOURCE CODE FOR ReadImageFileInJava.java
import java.awt.Image;
import javax.swing.ImageIcon;
import java.awt.Toolkit;
public class ReadImageFileInJava
{
public static void main(String[]args)
{
//READ AN IMAGE FILE, sample_picture.jpg
//METHOD getImage() CAN READ IMAGE FILE WITH FORMAT gif, jpg and png
Image a=Toolkit.getDefaultToolkit().getImage("sample_picture.jpg");
ImageIcon b=new ImageIcon(a);
//GET PICTURE'S WIDTH
int c=b.getIconWidth();
System.out.println("WIDTH : "+c+" pixels");
//GET PICTURE'S HEIGHT
int d=b.getIconHeight();
System.out.println("HEIGHT : "+d+" pixels");
}
}
PUT IMAGE INTO A CONTAINER IN JAVA
***********************************************
COMPLETE SOURCE CODE FOR ReadImageFileInJava.java
import java.awt.Graphics;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
public class ReadImageFileInJava
{
public static void main(String[]args)
{
//READ AN IMAGE FILE, sample_picture.jpg
//METHOD getImage() CAN READ IMAGE FILE WITH FORMAT gif, jpg and png
Image a=Toolkit.getDefaultToolkit().getImage("sample_picture.jpg");
ImageIcon b=new ImageIcon(a);
//GET PICTURE'S WIDTH
int c=b.getIconWidth();
System.out.println("WIDTH : "+c+" pixels");
//GET PICTURE'S HEIGHT
int d=b.getIconHeight();
System.out.println("HEIGHT : "+d+" pixels");
FrameWithPicture e=new FrameWithPicture();
e.setSize(c,d);
e.setVisible(true);
e.setResizable(false);
}
}
//CLASS FrameWithPicture
class FrameWithPicture extends Frame
{
//FOR FRAME CLOSE BUTTON
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};
public FrameWithPicture()
{
//FOR FRAME'S TITLE
super("DISPLAY IMAGE");
//ADD WINDOW LISTENER FOR FRAME
addWindowListener(wl);
}
//PAINT IMAGE(sample_picture.jpg) INTO FRAME
//OVERWRITE METHOD PAINT
public void paint(Graphics g)
{
super.paint(g);
Image a=Toolkit.getDefaultToolkit().getImage("sample_picture.jpg");
g.drawImage(a, 0, 0,null);
}
}
SCALE IMAGE IN JAVA
***********************************************
COMPLETE SOURCE CODE FOR ScaleImageInJava.java
import java.awt.geom.AffineTransform;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
public class ScaleImageInJava
{
public static void main(String[]args)
{
FrameWithPicture e=new FrameWithPicture();
e.setSize(400,400);
e.setVisible(true);
//e.setResizable(false);
}
}
//CLASS FrameWithPicture
class FrameWithPicture extends Frame
{
//FOR FRAME CLOSE BUTTON
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};
public FrameWithPicture()
{
//FOR FRAME'S TITLE
super("DISPLAY IMAGE");
//ADD WINDOW LISTENER FOR FRAME
addWindowListener(wl);
}
//PAINT IMAGE(sample_picture.jpg) INTO FRAME
public void paint(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
AffineTransform at=new AffineTransform();
//THIS WILL SCALE THE IMAGE
//YOU CAN TRY CHANGE THIS VALUE
//CURRENT VALUE IS 1 AND 1
at.scale(1,1);
Image a=Toolkit.getDefaultToolkit().getImage("sample_picture.jpg");
g2d.drawImage(a,at,this);
}
}
SHEAR IMAGE IN JAVA
***********************************************
COMPLETE SOURCE CODE FOR ShearImageInJava.java
import java.awt.geom.AffineTransform;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
public class ShearImageInJava
{
public static void main(String[]args)
{
FrameWithPicture e=new FrameWithPicture();
e.setSize(400,400);
e.setVisible(true);
}
}
//CLASS FrameWithPicture
class FrameWithPicture extends Frame
{
//FOR FRAME CLOSE BUTTON
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};
public FrameWithPicture()
{
//FOR FRAME'S TITLE
super("DISPLAY IMAGE");
//ADD WINDOW LISTENER FOR FRAME
addWindowListener(wl);
}
//PAINT IMAGE(sample_picture.jpg) INTO FRAME
public void paint(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
AffineTransform at=new AffineTransform();
//THIS WILL SHEAR THE IMAGE
//YOU CAN TRY CHANGE THIS VALUE
//CURRENT VALUE IS 0.2 AND 1
at.shear(0.2,1);
Image a=Toolkit.getDefaultToolkit().getImage("sample_picture.jpg");
g2d.drawImage(a,at,this);
}
}
TRANSLATE IMAGE IN JAVA (CHANGE IMAGE LOCATION)
***********************************************
COMPLETE SOURCE CODE FOR TranslateImageInJava.java
import java.awt.geom.AffineTransform;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
public class TranslateImageInJava
{
public static void main(String[]args)
{
FrameWithPicture e=new FrameWithPicture();
e.setSize(400,400);
e.setVisible(true);
}
}
//CLASS FrameWithPicture
class FrameWithPicture extends Frame
{
//FOR FRAME CLOSE BUTTON
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};
public FrameWithPicture()
{
//FOR FRAME'S TITLE
super("DISPLAY IMAGE");
//ADD WINDOW LISTENER FOR FRAME
addWindowListener(wl);
}
//PAINT IMAGE(sample_picture.jpg) INTO FRAME
public void paint(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
AffineTransform at=new AffineTransform();
//THIS WILL TRANSLATE THE IMAGE
//YOU CAN TRY CHANGE THIS VALUE
//CURRENT VALUE IS 20 AND 60
at.translate(20,60);
Image a=Toolkit.getDefaultToolkit().getImage("sample_picture.jpg");
g2d.drawImage(a,at,this);
}
}
ROTATE IMAGE IN JAVA (ANGLE OF ROTATION IS IN RADIANS VALUE)
***********************************************
COMPLETE SOURCE CODE FOR RotateImageInJava.java
import java.awt.geom.AffineTransform;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
public class RotateImageInJava
{
public static void main(String[]args)
{
FrameWithPicture e=new FrameWithPicture();
e.setSize(400,400);
e.setVisible(true);
}
}
//CLASS FrameWithPicture
class FrameWithPicture extends Frame
{
//FOR FRAME CLOSE BUTTON
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};
public FrameWithPicture()
{
//FOR FRAME'S TITLE
super("DISPLAY IMAGE");
//ADD WINDOW LISTENER FOR FRAME
addWindowListener(wl);
}
//PAINT IMAGE(sample_picture.jpg) INTO FRAME
public void paint(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
AffineTransform at=new AffineTransform();
//THIS WILL ROTATE THE IMAGE
//YOU CAN TRY CHANGE THIS VALUE
//THIS VALUE IS BASE ON radians value(ANGLE OF THE ROTATION IS IN RADIANS)
//YOU ALSO CAN SET IT TO NEGATIVE VALUE LIKE -Math.PI/10
at.rotate(Math.PI/10);
Image a=Toolkit.getDefaultToolkit().getImage("sample_picture.jpg");
g2d.drawImage(a,at,this);
}
}
CONVERT IMAGE INTO GRAYSCALE IN JAVA
***********************************************
COMPLETE SOURCE CODE FOR GrayscaleImage.java
import java.awt.Graphics;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
public class GrayscaleImage
{
public static void main(String[]args)
{
//READ AN IMAGE FILE, sample_picture.jpg
//METHOD getImage() CAN READ IMAGE FILE WITH FORMAT gif, jpg and png
Image a=Toolkit.getDefaultToolkit().getImage("sample_picture.jpg");
ImageIcon b=new ImageIcon(a);
//GET PICTURE'S WIDTH
int c=b.getIconWidth();
System.out.println("WIDTH : "+c+" pixels");
//GET PICTURE'S HEIGHT
int d=b.getIconHeight();
System.out.println("HEIGHT : "+d+" pixels");
FrameWithPicture e=new FrameWithPicture();
e.setSize(c,d);
e.setVisible(true);
e.setResizable(false);
}
}
//CLASS FrameWithPicture
class FrameWithPicture extends Frame
{
//FOR FRAME CLOSE BUTTON
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};
public FrameWithPicture()
{
//FOR FRAME'S TITLE
super("DISPLAY IMAGE");
//ADD WINDOW LISTENER FOR FRAME
addWindowListener(wl);
}
//PAINT IMAGE(sample_picture.jpg) INTO FRAME
public void paint(Graphics g)
{
super.paint(g);
ImageIcon d=new ImageIcon("sample_picture.jpg");
Image a=d.getImage();
int imageWidth=d.getIconWidth();//GET IMAGE'S WIDTH
int imageHeight=d.getIconHeight();//GET IMAGE'S HEIGHT
//CREATE BufferedImage OBJECT AND SET TYPE OF CREATED IMAGE IS GRAYSCALE(BufferedImage.TYPE_BYTE_GRAY)
BufferedImage bi=new BufferedImage(imageWidth,imageHeight,BufferedImage.TYPE_BYTE_GRAY);
Graphics g2=bi.getGraphics();
g2.drawImage(a,0,0,null);//draw color image(a) into buffered image(bi)
g.drawImage(bi, 0, 0,null);//draw buffered image(bi) into frame
}
}