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.
// CARMOVE.JAVA
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) {
}
}
}
// CARMOVE.JAVA
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) {
}
}
}
No comments:
Post a Comment