Here is a Simple Animation Program in Java.
import java.awt.*;
import java.awt.event.*;
public class Animation extends Frame implements ActionListener,Runnable
{
Button bStart,bExit;
int i,j;
Thread t;
public Animation()
{
bStart = new Button("Start");
bExit = new Button("Exit");
setLayout(new FlowLayout());
add(bStart);
add(bExit);
bStart.addActionListener(this);
bExit.addActionListener(this);
}
public void run()
{
try
{
j = 0;
for(i = 0 ; i < 100 ; i = i + 2)
{
paint(getGraphics());
t.sleep(100);
}
i=50;
for(j = 0 ; j < 100 ; j = j + 2)
{
paint(getGraphics());
t.sleep(100);
}
}
catch(Exception e){}
}
public void start1()
{
t = new Thread(this);
try
{
t.start();
}
catch(Exception e)
{
System.out.println("Exception Occured : " + e);
}
}
public void paint(Graphics g)
{
//Color c = new Color(i);
//setForeground(c);
//g.setColor(Color.BLUE);
g.clearRect(0,0,this.getHeight(),this.getWidth());
g.drawOval(50+j,50+i,30,30);
return;
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == bExit)
{
System.exit(1);
}
if(ae.getSource() == bStart)
{
System.out.println("Start Button Clicked...");
start1();
}
}
public static void main (String[] args)
{
Animation a1 = new Animation();
a1.setSize(400,400);
a1.show();
}
}
import java.awt.*;
import java.awt.event.*;
public class Animation extends Frame implements ActionListener,Runnable
{
Button bStart,bExit;
int i,j;
Thread t;
public Animation()
{
bStart = new Button("Start");
bExit = new Button("Exit");
setLayout(new FlowLayout());
add(bStart);
add(bExit);
bStart.addActionListener(this);
bExit.addActionListener(this);
}
public void run()
{
try
{
j = 0;
for(i = 0 ; i < 100 ; i = i + 2)
{
paint(getGraphics());
t.sleep(100);
}
i=50;
for(j = 0 ; j < 100 ; j = j + 2)
{
paint(getGraphics());
t.sleep(100);
}
}
catch(Exception e){}
}
public void start1()
{
t = new Thread(this);
try
{
t.start();
}
catch(Exception e)
{
System.out.println("Exception Occured : " + e);
}
}
public void paint(Graphics g)
{
//Color c = new Color(i);
//setForeground(c);
//g.setColor(Color.BLUE);
g.clearRect(0,0,this.getHeight(),this.getWidth());
g.drawOval(50+j,50+i,30,30);
return;
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == bExit)
{
System.exit(1);
}
if(ae.getSource() == bStart)
{
System.out.println("Start Button Clicked...");
start1();
}
}
public static void main (String[] args)
{
Animation a1 = new Animation();
a1.setSize(400,400);
a1.show();
}
}
No comments:
Post a Comment