Wednesday, July 17, 2013

Animation

With the EarthAnimation program you learn to make an image animated. In this program, the earth image is rotated according to many radian values so the animation performs. To rotate the image in Java, you need to use the rotate(double theta) method of the Graphics2D class.

animation in Java

EarthAnimation source code:

import java.awt.Label;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
class DrawArea extends Label implements ImageObserver{

double radian=0.0;
BufferedImage bi;
DrawArea(){

try{
bi=ImageIO.read(new File("d:/earth2.jpg")); //read image file
}catch(IOException ie){}
}
public void paint(Graphics g){

rotateImage(g);
try{
Thread.sleep(50);
}catch(InterruptedException ie){}

}

public void rotateImage(Graphics g){
Graphics2D g2d=(Graphics2D)g;
g2d.translate(450,350); //move the original coordinate to point(450,350)
g2d.rotate(radian); //rotate the image
g2d.translate(-bi.getWidth()/2,-bi.getHeight()/2); //move the coordinate back to point (bi.getWidth()/2,bi.getHeight()/2)
g2d.drawImage(bi,0,0,this); //show the rotated image
g2d.dispose(); //clean the g2d object
radian+=Math.PI/128; //update radian variable
repaint();//recall the paint method
}


}

class WindowShow extends Frame{
DrawArea da;
WindowShow(){

setSize(900,700); //set window size
setTitle("Earth animation"); //set window title
setVisible(true); //make sure the window is visible
setBackground(Color.BLACK); //set background color of window
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);

}
});

add(new DrawArea()); //add drawing area to the window
validate();
}
}

public class EarthAnimation {
public static void main(String[] args){
new WindowShow();

}
}

In the example code avoid, the Graphics2D object is constructed from the Graphics object by the following line of code:

Graphics2D g2d=(Graphics2D)g;


When you have the Graphics2D object, you do whatever you want with the you graphic elements. The translate (int x,int y) method of the Graphics2D is used to move the origin coordinate to a new coordinate. In this example, the origin coordinate (0,0) is moved to the coordinate (450,350). Now your drawing can be performed at the new point. The rotate (double theta) method rotates the drawing object (actually it rotates the axes of the coordinate system) according to the theta or radian value. Generally the radian value is calculated from the Math.PI. After the drawing object is rotated, the position of the object might be changed. To adjust the position to display drawing object so it displays at the point that we desire (the previous point that the image displayed before rotating) , you need to use the translate method again. But this time, you will assign the negative values to both x and y.

No comments:

Post a Comment