how to make a simple animation in java


Friends animation in java is no that hard ,try this to learn the basics of animation..

import javax.swing.*;
import java.awt.*;
public class Animation
 {
   int x=0;
   int y=0;
       public static void main(String[] args)
       {
        Animation gui=new Animation();
       gui.play();
     }
public void play()
   {
           JFrame frame=new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            DrawPanel draw=new DrawPanel();
            frame.getContentPane().add(draw);
             frame.setSize(500,500);
              frame.setVisibility(true);
             for(int i=0;i<150;i++)
              {
                 x++;
                 y++;
                 draw.repaint();  //tells the panel to redraw itself so we can see the circle in new location
              try{
               Thread.sleep(25);
                  }catch(Exception e)
                  {}
         }
     }

class DrawPanel extends JPanel{
     public void paintComponent(Graphics g)
       {
        g.setColor(Color.WHITE);
        g.fillRect(0,0,this.getWidth(),this.getHeight());
      g.setColor(Color.GREEN);
     g.fillOval(x,y,50,50);
}
simply compile and execute to see the simple animation in java

 

Advertisements

About tarasj

hi friends i to blog what i have learned .lets talk and share
This entry was posted in ..BASIC... Bookmark the permalink.

2 Responses to how to make a simple animation in java

  1. Khanh Trinh says:

    Hi dude, do you intend to study about programming android games or apps? 🙂

    • tarasj says:

      yea if become expert in java than it will not be big deal to go for android right ….thanks for the comment

Comments are closed.