Hej
Hur kan man få Radioknapparna att starta samt stoppa animeringen, vänliga
hälsningar
Micke C
import java.io.*;
import java.math.*;
import java.util.Random;
import java.applet.Applet;
import java.awt.*;
public class Ar extends Applet implements Runnable {
CheckboxGroup radioGroup1;
// The radio buttons to be selected
Checkbox radio1;
Checkbox radio2;
Thread animation;
int locx,locy,locx1,locy1,locx2,locy2; // location of Argon
double epsAr = 112;
double sigAr = 362;
double r,r2,r3;
double V,V1,V2,V3;
double tempx;
double tempy;
static final byte UP = 0; // direction of motion
static final byte DOWN = 1;
static final byte LEFT = 2;
static final byte RIGHT = 3;
byte state; // state the rect is in
// length of pausing interval
static final int REFRESH_RATE = 10; // in ms
// applet methods:
public void init() {
// Tell the applet not to use a layout manager.
setLayout(null);
System.out.println(">> init <<");
Random generator = new Random();
locx = generator.nextInt(300); // Generator Random
parameters of pos.
locy = generator.nextInt(300);
locx1 = generator.nextInt(300);
locy1 = generator.nextInt(300);
locx2 = generator.nextInt(300);
locy2 = generator.nextInt(300);
state = UP;
}
public void start() {
// initialize the radio buttons group
radioGroup1 = new CheckboxGroup();
// first radio button. Gives the label text, tells to which
// group it belongs and sets the default state (unselected)
radio1 = new Checkbox("Start", radioGroup1,false);
// same but selected
radio2 = new Checkbox("Stopp", radioGroup1,true);
radio1.setBounds(0,350,50,30);
radio2.setBounds(50,350,50,30);
// now that all is set we can add these components to the applet
//add(okButton1);
//add(okButton2);
add(radio1);
add(radio2);
System.out.println(">> start <<");
animation = new Thread(this);
if (animation != null) {
animation.start();
}
}
public void paint (Graphics page)
{
setBackground (Color.white); // Color Background
tempx = (locx-locx1);
tempy = (locy-locy1);
r = (Math.sqrt((Math.pow((tempx),2))+(Math.pow((tempy),2))));
r2 = (Math.sqrt((Math.pow((locx-locx2),2))+(Math.pow((locy-locy2),2))));
r3 =
(Math.sqrt((Math.pow((locx1-locx2),2))+(Math.pow((locy1-locy2),2))));
V1 = (4*epsAr*((Math.pow((sigAr/r),12))-(Math.pow((sigAr/r),6))));
V2 = (4*epsAr*((Math.pow((sigAr/r2),12))-(Math.pow((sigAr/r2),6))));
V3 = (4*epsAr*((Math.pow((sigAr/r3),12))-(Math.pow((sigAr/r3),6))));
V = (V1+V2+V3);
if(V<0){
locy = (locy1*2);
locx = (locx1*2);
}
if(V>0){
locy = (locy2/2);
locx = (locx2/2);
}
if(V == 0){
locy = locy;
locx = locx;
}
if(locy>300){
locy = (locy-300);
if(locx>300)
locx = (locx-300);
}
if(locy<1){
locy = (locy+300);
}
if(locx1<1){
locx = (locx+300);
}
tempx = (locx-locx1);
tempy = (locy-locy1);
r = (Math.sqrt((Math.pow((tempx),2))+(Math.pow((tempy),2))));
r2 = (Math.sqrt((Math.pow((locx-locx2),2))+(Math.pow((locy-locy2),2))));
r3 =
(Math.sqrt((Math.pow((locx1-locx2),2))+(Math.pow((locy1-locy2),2))));
V1 = (4*epsAr*((Math.pow((sigAr/r),12))-(Math.pow((sigAr/r),6))));
V2 = (4*epsAr*((Math.pow((sigAr/r2),12))-(Math.pow((sigAr/r2),6))));
V3 = (4*epsAr*((Math.pow((sigAr/r3),12))-(Math.pow((sigAr/r3),6))));
V = (V1+V2+V3);
if(V<0){
locy1 = (locy/2);
locx1 = (locx/2);
}
if(V>0){
locy1 = (locy*2);
locx1 = (locx*2);
}
if(V == 0){
locy1 = locy1;
locx1 = locx1;
}
if(locy1>300){
locy1 = (locy1-300);
if(locx1>300)
locx1 = (locx1-300);
}
if(locy<1){
locy1 = (locy1+300);
}
if(locx1<1){
locx1 = (locx1+300);
}
tempx = (locx-locx1);
tempy = (locy-locy1);
r = (Math.sqrt((Math.pow((tempx),2))+(Math.pow((tempy),2))));
r2 = (Math.sqrt((Math.pow((locx-locx2),2))+(Math.pow((locy-locy2),2))));
r3 =
(Math.sqrt((Math.pow((locx1-locx2),2))+(Math.pow((locy1-locy2),2))));
V1 = (4*epsAr*((Math.pow((sigAr/r),12))-(Math.pow((sigAr/r),6))));
V2 = (4*epsAr*((Math.pow((sigAr/r2),12))-(Math.pow((sigAr/r2),6))));
V3 = (4*epsAr*((Math.pow((sigAr/r3),12))-(Math.pow((sigAr/r3),6))));
V = (V1+V2+V3);
if(V<0){
locy2 = (locy*2);
locx2 = (locx*2);
}
if(V>0){
locy2 = (locy/2);
locx2 = (locx/2);
}
if(V == 0){
locy2 = locy2;
locx2 = locx2;
}
if(locy1>300){
locy2 = (locy2-300);
if(locx1>300)
locx2 = (locx2-300);
}
if(locy<1){
locy2 = (locy2+300);
}
if(locx1<1){
locx2 = (locx2+300);
}
page.setColor (Color.red);
page.fillOval (locx, locy, 8, 8); // Argon fild
page.fillOval (locx1, locy1, 8, 8); // Argon fild
page.fillOval (locx2, locy2, 8, 8); // Argon fild
}
// update the center MDsim
void updateMDsim() {
switch (state) {
case DOWN:
locy += 1;
if (locy >= 300) {
state = UP;
}
break;
case UP:
locy -= 1;
if (locy <= 1) {
state = RIGHT;
}
break;
case RIGHT:
locx += 1;
if (locx >= 300) {
state = LEFT;
}
break;
case LEFT:
locx -= 1;
if (locx <= 1) {
state = DOWN;
}
break;
}
}
public void run() {
while (true) {
repaint();
updateMDsim();
try {
Thread.sleep (REFRESH_RATE);
} catch (Exception exc) { };
}
}
public void stop() {
System.out.println(">> stop <<");
if (animation != null) {
animation.stop();
animation = null;
}
}
}
|