Вы находитесь на странице: 1из 4

/** Harmonic oscillator **/

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.applet.*;

public class Oscillator extends Applet implements Runnable, ActionListener


{
private static final int W = 400, H = 220; //width and hight
private int px, py = H/2; //coordinates we use to draw the spring
private double x; //position
private double m = 1., k = 0.25; //mass, spring stifness
private double w = Math.sqrt(2*k/m); //angular frequency
private double amp = -0.8; //amplitude
private int fr = 0; //friction
private double dt = 0.05; //timestep
private int step = 0;//intial step
private int xPoints1[] = new int[10]; //xcoordinates for spring1
private int yPoints1[] = new int[10]; //ycoordinates for spring1
private int xPoints2[] = new int[10]; //xcoordinates for spring2
private int yPoints2[] = new int[10]; //ycoordinates for spring2
private boolean stop = false; //go/pause
Button go, plus, min;
Button plus2,min2;
Thread t;
private Graphics g, dbg;
private Image dbi;

public void init()


{
setLayout(new FlowLayout());
Canvas can = new Canvas();
can.setBackground(Color.white);
can.setSize(W,H);
add(can);

Panel pan = new Panel();


pan.setLayout(new FlowLayout());
pan.setSize(W+1,H);
pan.setBackground(Color.green);
pan.setFont(new Font("Verdana",Font.PLAIN,10));

plus = new Button("Spring +");


min = new Button("Spring -");
plus2 = new Button("Friction ++");
min2 = new Button("Friction --");
go = new Button("Go/Pause");
plus.setSize(10,80);
min.setSize(10,80);
plus2.setSize(10,80);
min2.setSize(10,80);
go.setSize(80,25);
plus.addActionListener(this);
min.addActionListener(this);
plus2.addActionListener(this);
min2.addActionListener(this);
go.addActionListener(this);
pan.add(go); pan.add(plus); pan.add(min);
pan.add(plus2); pan.add(min2);
add(pan);

g = can.getGraphics();
dbi = createImage(W,H);
dbg = dbi.getGraphics();
}

public void run() //method run is to move the code


{
try{Thread.sleep(100);} catch(InterruptedException exc){}
while(Thread.currentThread() ==t)
{
while(stop){try{Thread.sleep(20);}catch(InterruptedException exc)
{}}

try{Thread.sleep(2);} catch(InterruptedException exc){}

x = amp*Math.exp(-0.1*fr*step*dt)*Math.cos(w*step*dt); //new x

pixels(); //calculate coordinates of the spring


paint(); //paint new frame
step++;
}//whil
}//run

public void intial() //method for setting initial values


{
t = null;
stop = false;
step = 0;
//omega = Math.sqrt(2*D/m);
}

public void pixels() //method for calculating pixelcoords


{
int length, currentLength1, currentLength2; //spring lengths
double d1, d2; //coordinates for springs

length = W/2-10;
px = (int)Math.round((double)W/2 + (length-40)*x); //mass position
currentLength1 = px-20;
currentLength2 = W-px-20;
d1 = currentLength1/16.;
d2 = currentLength2/16.;

xPoints1[0] = 11; yPoints1[0] = H/2;


xPoints1[1] = (int)(11+d1); yPoints1[1] = H/2+7;
xPoints1[2] = (int)(11+3*d1); yPoints1[2] = H/2-7;
xPoints1[3] = (int)(11+5*d1); yPoints1[3] = H/2+7;
xPoints1[4] = (int)(11+7*d1); yPoints1[4] = H/2-7;
xPoints1[5] = (int)(11+9*d1); yPoints1[5] = H/2+7;
xPoints1[6] = (int)(11+11*d1); yPoints1[6] = H/2-7;
xPoints1[7] = (int)(11+13*d1); yPoints1[7] = H/2+7;
xPoints1[8] = (int)(11+15*d1); yPoints1[8] = H/2-7;
xPoints1[9] = px-10; yPoints1[9] = H/2;

xPoints2[0] = W-12; yPoints2[0] = H/2;


xPoints2[1] = (int)(W-11-d2); yPoints2[1] = H/2+7;
xPoints2[2] = (int)(W-11-3*d2); yPoints2[2] = H/2-7;
xPoints2[3] = (int)(W-11-5*d2); yPoints2[3] = H/2+7;
xPoints2[4] = (int)(W-11-7*d2); yPoints2[4] = H/2-7;
xPoints2[5] = (int)(W-11-9*d2); yPoints2[5] = H/2+7;
xPoints2[6] = (int)(W-11-11*d2); yPoints2[6] = H/2-7;
xPoints2[7] = (int)(W-11-13*d2); yPoints2[7] = H/2+7;
xPoints2[8] = (int)(W-11-15*d2); yPoints2[8] = H/2-7;
xPoints2[9] = px+10; yPoints2[9] = H/2;
}//pixels()

public void paint()


{super.paint(dbg);
dbg.setColor(Color.white);
dbg.fillRect(0,0,W,H);
dbg.setColor(Color.black);
dbg.drawLine(10,H/2-30,10,H/2+30);
dbg.drawLine(W-11,H/2-30,W-11,H/2+30);
dbg.drawPolyline(xPoints1,yPoints1,10);
dbg.drawPolyline(xPoints2,yPoints2,10);
dbg.fillOval(px-10,py-10,21,21);

g.drawImage(dbi,0,0,this);
}//paint()

public void actionPerformed(ActionEvent evt)


{
if(evt.getActionCommand() == go.getActionCommand())
{ stop = !stop; }

if(evt.getActionCommand() == plus.getActionCommand())
{
k= k*2; intial(); paint(); start();
}
if(evt.getActionCommand() == min.getActionCommand())
{
k = k/2; intial(); paint(); start();
}
if(evt.getActionCommand() == plus2.getActionCommand())
{
fr++; intial(); paint(); start();
}
if(evt.getActionCommand() == min2.getActionCommand())
{
if(x > 0)
{ fr--; intial(); paint(); start(); }
}
}//actionPerformed()

public void start()


{
if(t == null)
{
t= new Thread(this);
t.start();
}
}//start()
public void stop()
{
t = null;
}//stop()

Вам также может понравиться