Programming with Junior: sine+cosine=circle

Two examples are presented: in the first one, a ball draws a circle as result of being moved by two distinct Junior reactive programs; the second example shows the use of broadcast events and of dynamic creation of objects.

These two examples are also coded using fair threads; you can click here to see what the two examples coded in FairThreads look like.

### Demo1

Two fair threads are moving the same ball; one thread follows a sine path, and the other a cosine path. An applet is created which adds a third fair thread, for painting traces of the ball. At each phase, color of the ball changes, to visualize ball moves. One gets a circle; this is basically because the two reactive behaviors share common instants. threads is fair.

### The Ball

class Ball
{
double x, y, angleX = 0, angleY = 0;
int radius = 8, steps = 100, scale = 5;
Color color;

public Ball(int x,int y,Color color){
this.x = x; this.y = y; this.color = color;
}
public Ball(int x,int y){ this(x,y,ColorBall.nextColor()); }

public void paint(Graphics g){
g.setColor(color);
}

public void sine(){
angleY += (2*Math.PI/steps);
y += scale*Math.sin(angleY);
}
public void cosine(){
angleX += (2*Math.PI/steps);
x += scale*Math.sin(angleX+Math.PI/2);
}
}

### Sine Action

class Sin implements Action
{
public void execute(Environment env){
ball.sine();
}
}

### The Reactive Applet

class ReactiveApplet extends DoubleBufferedApplet implements Runnable
{
Machine machine = Jr.SafeMachine();

public void run(){
while(true){
machine.react();
paint(getGraphics());
}
}
}

### The Applet

public class SinCos extends ReactiveApplet
{
Ball ball = new Ball(startx,starty);

public void paint(Graphics g) {
super.paint(g);
ball.paint(g);
ball.color = ColorBall.nextColor();
}

Program sine(Ball ball){
}
Program cosine(Ball ball){
}
Program circle(Ball ball){
return Jr.Par(sine(ball),cosine(ball));
}

public void init(){
super.init();
}
}

result

Adding a new behavior that move the ball following a cosine path, one gets a Lissajous figure (produced by two cosine and one sine threads):

public void init(){
super.init();
}
result

### Demo2

Balls are now dynamically created; they have two associated behaviors which are waiting for an event to start moving (following a sine or a cosine path). A button is used to create new balls at random positions; the sine button broadcasts the sine event to all behaviors; the cosine button broadcasts the cosine event; the sine+cosine button broadcasts both sine and cosine events.

### Mouse click -> event

{
Machine machine;
Vector events;
this.machine = machine; this.events = events;
}

public void mouseClicked(MouseEvent evt){
Enumeration enum = events.elements();
while(enum.hasMoreElements()){
String event = (String)enum.nextElement();
machine.generate(Jr.StringIdentifier(event));
}
}
}

### The applet

public class DynSinCos extends ReactiveApplet
{
int rand(int n){ return (int)(Math.random()*1000)%n; }

Vector items = new Vector();

public void paint(Graphics g){
synchronized (items) {
Enumeration enum = items.elements();
while(enum.hasMoreElements()) ((Ball)enum.nextElement()).paint(g);
}
}

void createBall(){
Ball ball =
new Ball(rand(size().width),rand(size().height));

Program sin =
Jr.Seq(Jr.Await("sin"),
Jr.Loop(Jr.Seq(Jr.Atom(new Sin()),Jr.Stop())));
Program cos =
Jr.Seq(Jr.Await("cos"),
Jr.Loop(Jr.Seq(Jr.Atom(new Cos()),Jr.Stop())));

}

public void init(){
super.init();
Button sin = new Button("sin");
Button cos = new Button("cos");
Button sincos = new Button("sin+cos");
Button start = new Button("new ball");

Vector params = new Vector();

params = new Vector();

params = new Vector();

Panel p = new Panel();
p.setBackground(Color.lightGray);
setLayout(new BorderLayout());
p.setLayout(new GridLayout(1,5,5,20));

createBall();
}
}
result