Tuesday, March 11, 2014

inverted pendulum using applet in java



import java.applet.Applet;
import java.awt.*;
import java.awt.color.*;
import javax.swing.*;
import javax.swing.event.*;
public class pendulum extends Applet implements Runnable {
double x=500,y=500,m=225;
Thread th;
public void init(){
setSize(500,500);
}
public void paint(Graphics g)
{
setBackground(Color.blue);
g.setColor(Color.red);
g.fillOval((int)x, (int)y, 50, 50);
g.drawLine(130,150,(int)x+25, (int)y+25);

}
public void start()
{
th=new Thread(this);
th.start();
}
public void run()
{
int turn=0;
while(true)
{

if(turn==0)
{
int count=0;
while(count<=90)
{
count++;
m=Math.toRadians(m);
x=100+100*Math.cos(m);
y=100+100*Math.sin(m);

repaint();
m=Math.toDegrees(m);m++;
try
{
Thread.sleep(10);
}
catch(InterruptedException ex)
{
}
}
turn=1;
}
else
{
int count=0;
while(count<=90)
{
m=Math.toRadians(m);
x=100+100*Math.cos(m);
y=100+100*Math.sin(m);
count++;
repaint();
m=Math.toDegrees(m);m--;
try
{
Thread.sleep(10);
}
catch(InterruptedException ex)
{
}
}


turn=0;
}
}
}

}

No comments:

Post a Comment

Contributors

Translate