To Move a Shape say Oval from the bottom of the applet to the center, a VerticalScroll in an infinite loop

/*Java Program To Move a Shape say Oval from the bottom of the applet to the center,
 a verticalscroll in an infinite loop i.e. once the shape reaches the center it goes
 back to the bottom and repeats the path */

VerticalScroll.html


 <html>  
    <body>  
    <applet code = "Face.class" width = 500 height =500>
</applet>   
    </applet>  
    </body>  
    </html>  

Code for VerticalScroll.Java

import java.awt.*;
import java.applet.*;

public class VerticalScroll extends Applet implements Runnable
{
int x,y;
Dimension size;

public void init()
{
//set initial co-ordinates for message
x = 230;
y = 450;
//creates new thread
Thread t = new Thread(this);
t.start();
}
public void run()
{
while (true)
{
try
{
/*initialize size to contain the current size of the applet window */
size = getSize();

/*if co-ordinate of the oval is lesser than reset the height of
the applet the x coordinate to a value say 200 else decrement y */

if(y< 200)
y = 450;
y--;
Thread.sleep(20);
}
catch(InterruptedException e)
{
}
repaint();
}
}

public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(x,y,40,50);
}
}

To execute the applet by appletviewer tool, write in command prompt:

c:\>javac VerticalScroll.java
c:\>appletviewer VerticalScroll.html

Previous
Next Post »