Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Awt Line Animation in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Line Animation in Java

Advertisement
In this example we are creating an animated line. The color of line is changing alternatively.

Line Animation in Java

     

In this example we are creating an animated line. The color of  line is changing alternatively.

We use drawLine() to draw a line and setColor() method to set the color of the line. We pass four values in drawLine() method in which three  last values are fixed while value of  first x  is changing. The rang of the value is -500 to 600. The color of the line is changing by setColor() method. To remove old color and set new one or  in other words to refresh the color of line we are using repaint() method. To display the output we are creating new html files in which we are calling the .class file (of the source file) by using applet tag. We define the height and width of the window for output.

Here is the code of the program :

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.*;
public class AnimationLine extends JApplet

 public void init() 
 {
 getContentPane().add(new AnimationLinePanel())
 setSize(800,500);
 }
 public class AnimationLinePanel extends JPanel
 {
 int linex,linem = 1
 public AnimationLinePanel()
 {
 setBackground(Color.black)
 }
 public void paintComponent(Graphics page)
 {
 super.paintComponent(page);
 page.setColor(Color.red)
 page.drawLine(linem,200,310,400)
 if(linex > 500)
 {
 linem = -500;
 page.setColor(Color.green)
 page.drawLine(linem,200,310,400)
 }
 if(linex < 0)
 {
 linem = 600;
 page.setColor(Color.yellow)
 page.drawLine(linem,200,310,400)
 }
 linex += linem; 
 repaint()
 }
 }

<html>
<body>
<APPLET ALIGN=
"CENTER" CODE="AnimationLine.class" 
WIDTH=
"800" HEIGHT="500">
</APPLET>
</body>
</html>

OutPut of The Example:

Download this example.

Download this example.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 19, 2007

Related Tutorials

Discuss: Line Animation in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com