media @ VU
applet-math-lissa.jva
applet-math-lissa.jva
/ applet-math-lissa
// Lissagous figures, Evgeny Demidov, 16 Jan 2003
import java.awt.*;
public class applet-math-lissa extends java.applet.Applet implements Runnable {
final int Max=10*360, M1=Max-1, lbSize=50;
int[] arrSin=new int[Max+1];
int height, width, iYo=0, parM=1, parN=2, delay=10;
Thread ScopeTrace;
Choice chDelay, chM, chN;
Label lbDelay, lbM, lbN;
Image buffImage;
Graphics buffGraphics;
public void init() {
height= Integer.parseInt(getParameter("height"));
width = Integer.parseInt(getParameter("width"));
int radius = width / 2; int m2 = Max/2;
double SiF=Math.sin(3.14159265/m2), CoF=Math.cos(3.14159265/m2);
double Si=-SiF, Co=CoF;
for (int i=0; i<=m2; i++) {
Si=Si*CoF+Co*SiF; Co=Co*CoF-Si*SiF;
int s= (int) (radius*Si);
arrSin[i]=radius+s; arrSin[i+m2]=radius-s;
}
chM = new Choice(); chN = new Choice();
for (int i=1; i<10; i++) {
chM.addItem(Integer.toString(i));
chN.addItem(Integer.toString(i));
}
chN.select(parN-1);
lbM = new Label("M", Label.RIGHT); add(lbM); add(chM);
lbN = new Label("N", Label.RIGHT); add(lbN); add(chN);
lbDelay = new Label("Delay(ms)", Label.RIGHT); add(lbDelay);
chDelay = new Choice();
chDelay.addItem("0"); chDelay.addItem("10");
chDelay.addItem("20"); chDelay.addItem("50");
chDelay.addItem("100"); chDelay.addItem("200");
chDelay.addItem("500"); chDelay.addItem("999");
chDelay.select("10"); add(chDelay);
buffImage = createImage(width, width);
buffGraphics = buffImage.getGraphics();
}
public void paint(Graphics g) {
long cntTime = System.currentTimeMillis();
int iX,iY, iXo=0, aX,aY, aXo=arrSin[0],aYo=arrSin[iYo];
buffGraphics.clearRect(0, 0, width, width);
for (int j=Max; j>0; j--) {
iX=(iXo+parM) % Max; iY=(iYo+parN) % M1;
aX=arrSin[iX]; aY=arrSin[iY];
buffGraphics.drawLine( aXo,aYo, aX,aY);
iXo=iX; iYo=iY; aXo=aX; aYo=aY;
}
g.drawImage(buffImage, 0, lbSize, this);
// showStatus( "time=" + (System.currentTimeMillis()-cntTime) );
}
public boolean action(Event evt, Object obj) {
if(evt.target instanceof Choice) {
if(evt.target.equals(chDelay))
delay=Integer.parseInt( chDelay.getSelectedItem() );
else if(evt.target.equals(chM)) parM=chM.getSelectedIndex()+1;
else if(evt.target.equals(chN)) parN=chN.getSelectedIndex()+1;
else return false;
return true;
}
return false;
}
public void update(Graphics g) { paint(g); }
public void run() {
while(true) {
repaint();
try { Thread.sleep( delay );
} catch (InterruptedException e) { }
}
}
public void start() {
ScopeTrace = new Thread(this);
ScopeTrace.start();
}
public void stop() { ScopeTrace.stop(); }
}
(C) A. Eliëns
2/9/2007
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.