This program is only for beginners in Java. Most of you may have imagined it is very difficult of it takes a lot of time to grasp the essence of animation in Java. Well this program here dispels all those fears. It is a very basic (and dare I say an ugly) animation of a red car which moves from left to right.
COPY CODE BELOW START
// CARMOVE.JAVA// MOVING CAR
// Created by Karthik Sampath on 28/4/2010.
// Copyright 2010, Karthik. All rights reserved.
// The copyright to the computer program(s) herein
// is the property of Karthik Sampath, India. The
// program(s) may be used and/or copied only with the
// written permission of Karthik Sampath or in accordance
// with the terms and conditions stipulated in the
// agreement/contract under which the program(s) have
// been supplied. This copyright notice must not be
// removed.
// Copyright 2010, Karthik. All rights reserved.
// The copyright to the computer program(s) herein
// is the property of Karthik Sampath, India. The
// program(s) may be used and/or copied only with the
// written permission of Karthik Sampath or in accordance
// with the terms and conditions stipulated in the
// agreement/contract under which the program(s) have
// been supplied. This copyright notice must not be
// removed.
// Published in www.MyTricksTime.com
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
public class test extends JApplet {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new test());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1024, 739);
frame.setVisible(true);
}
public void paint(Graphics g) {
try {
int j = 0;
while (j < 500) {
g.setColor(Color.white);
g.fillRect(0, 0, 5000, 5000);
g.setColor(Color.red);
g.fillRect(250 + j, 250, 100, 50);
g.setColor(Color.black);
g.fillOval(260 + j, 300, 20, 20);
g.fillOval(320 + j, 300, 20, 20);
j++;
Thread.sleep(2);
}
} catch (InterruptedException e) {
}
}
}
END
COPY CODE END
Enjoy Guys! and don’t forget to post your comments. � MyTricksTime.com