Skip to content

My first EJS simulation: Stopwatch

August 10, 2011

I created my first EJS Stopwatch simulation.

My project manager asked me to do a stopwatch simulation using Easy Java Simulation. And he gave me some constraints. That are

1. Stopwatch should be resizeable

2. It should be placed on position what we given

3. It should have 300 ticks. Each 5th tick is bigger, and each 25th tick is bigger than 5th ticks. We give the numbers to 25th ticks (5, 10, 25 …..)

4. It should be real analog stopwatch. It must match with the second speed of system time.

Design:

my stop will looks like below

The Elements which I have used:

2D drawables –  segment set. text set, arrow, 2d shape

Model:

Variables:

size, position, tick size, tick position, minute hand size, second hand size, text , text angle, degree, degree increment, radian

Initialization:

double StopwatchTempAngle = 2 * Math.PI / 300 ;         //Angle between two ticks. This angle will be used to find position.
int StopwatchIndex ;                                   //Iteration index

//Draw Ticks for Second hand
for(StopwatchIndex = 0 ; StopwatchIndex < 300; StopwatchIndex++) {    StopwatchTickSecondX[StopwatchIndex] = StopwatchPositionX + StopwatchSize * Math.sin(StopwatchIndex * StopwatchTempAngle) ;        //X position of Second hand in circle       StopwatchTickSecondY[StopwatchIndex] = StopwatchPositionY + StopwatchSize * Math.cos(StopwatchIndex * StopwatchTempAngle) ;        //Y position of Second hand in circle      if(StopwatchIndex % 25 == 0)  {      StopwatchTickSecondSizeX[StopwatchIndex]  =   - (StopwatchSize/8) * Math.sin(StopwatchIndex * StopwatchTempAngle) ;    //Second hand tick size X   StopwatchTickSecondSizeY[StopwatchIndex]  =   - (StopwatchSize/8) * Math.cos(StopwatchIndex * StopwatchTempAngle) ;    //Second hand tick size Y           StopwatchTextSecondX[StopwatchIndex/25] = StopwatchTickSecondX[StopwatchIndex] + 1.2 * StopwatchTickSecondSizeX[StopwatchIndex] ;           //Second text X   StopwatchTextSecondY[StopwatchIndex/25] = StopwatchTickSecondY[StopwatchIndex] + 1.2 * StopwatchTickSecondSizeY[StopwatchIndex] ;           //Second text Y      if(StopwatchIndex > 0) StopwatchText[StopwatchIndex/25] = StopwatchIndex/5 + "";                    //Text in each 25th position
  else StopwatchText[0] = "0";                                                                    //Text in 0th position

  StopwatchTextAngle[StopwatchIndex/25] = - StopwatchIndex * StopwatchTempAngle;                  //Text transformation angle
 }

 else if(StopwatchIndex % 5 == 0)
 {

  StopwatchTickSecondSizeX[StopwatchIndex]  =  - (StopwatchSize/10) * Math.sin(StopwatchIndex * StopwatchTempAngle) ;    //Second hand tick size X
  StopwatchTickSecondSizeY[StopwatchIndex]  =  - (StopwatchSize/10) * Math.cos(StopwatchIndex * StopwatchTempAngle) ;    //Second hand tick size Y
  }

 else
 {
  StopwatchTickSecondSizeX[StopwatchIndex]  =   - (StopwatchSize/14) * Math.sin(StopwatchIndex * StopwatchTempAngle) ;    //Second hand tick size X
  StopwatchTickSecondSizeY[StopwatchIndex]  =   - (StopwatchSize/14) * Math.cos(StopwatchIndex * StopwatchTempAngle) ;    //Second hand tick size Y

 }
}

//Draw Ticks for Minute hand
StopwatchTempAngle = 2 * Math.PI / 60 ;         //Angle between two ticks. This angle will be used to find position.

for(StopwatchIndex = 0 ; StopwatchIndex < 60; StopwatchIndex++) {    StopwatchTickMinuteX[StopwatchIndex] = StopwatchPositionX + (StopwatchSize/2) * Math.sin(StopwatchIndex * StopwatchTempAngle) ;        //X position of Minute hand in circle       StopwatchTickMinuteY[StopwatchIndex] = (StopwatchPositionY+(StopwatchSize/4)) + (StopwatchSize/2) * Math.cos(StopwatchIndex * StopwatchTempAngle) ;        //Y position of Minute hand in circle      if(StopwatchIndex % 5 == 0)  {      StopwatchTickMinuteSizeX[StopwatchIndex]  =   - (StopwatchSize/12) * Math.sin(StopwatchIndex * StopwatchTempAngle) ;    //Minute hand tick size X   StopwatchTickMinuteSizeY[StopwatchIndex]  =   - (StopwatchSize/12) * Math.cos(StopwatchIndex * StopwatchTempAngle) ;    //Minute hand tick size Y      StopwatchTextMinuteX[StopwatchIndex/5] = StopwatchTickMinuteX[StopwatchIndex] + 1.2 * StopwatchTickMinuteSizeX[StopwatchIndex] ;           //Minute text X   StopwatchTextMinuteY[StopwatchIndex/5] = StopwatchTickMinuteY[StopwatchIndex] + 1.2 * StopwatchTickMinuteSizeY[StopwatchIndex] ;           //Minute text Y      if(StopwatchIndex > 0) StopwatchText[StopwatchIndex/5] = StopwatchIndex + "";                    //Text in each 5th position
  else StopwatchText[0] = "0";                                                                    //Text in 0th position

  StopwatchTextAngle[StopwatchIndex/5] = - StopwatchIndex * StopwatchTempAngle;                  //Text transformation angle
 }

 else
 {
  StopwatchTickMinuteSizeX[StopwatchIndex]  =   - (StopwatchSize/25) * Math.sin(StopwatchIndex * StopwatchTempAngle) ;    //Minute hand tick size X
  StopwatchTickMinuteSizeY[StopwatchIndex]  =   - (StopwatchSize/25) * Math.cos(StopwatchIndex * StopwatchTempAngle) ;    //Minute hand tick size Y

 }
}

//initialize second hand degree to 0
StopwatchDegreeSecond = 0 ;

Evolution:

FPS (Frame Per Second) – 10

SPD(Steps per Display) – 1

Code:

StopwatchDegreeSecond = StopwatchDegreeSecond + StopwatchDegreeIncrement ;

Fixed Relation:

//Compute Second hand angle
StopwatchRadianSecond = StopwatchDegreeSecond * Math.PI / 180 ; 

//Compute Secon hand position
StopwatchSecondHandX = StopwatchSize * Math.sin(StopwatchRadianSecond) ;
StopwatchSecondHandY = StopwatchSize * Math.cos(StopwatchRadianSecond) ;

//Compute Minute Hand position

StopwatchMinuteHandX = (StopwatchSize/2) * (Math.sin(StopwatchRadianSecond/60)) ;
StopwatchMinuteHandY = (StopwatchSize/2) * (Math.cos(StopwatchRadianSecond/60)) ;

Download Jar file and EJS source file from here

How to run:

Open you terminal and type

java -jar Stopwatch.jar

There are two buttons start/pause, reset
To start stopwatch click on start button.

Download EJS source file from here:

How to reuse this stopwatch in other simulation :

Copy all the page from stopwatch EJS and paste in other simulation. OR manually create variables.

Future Work:

This simulation works perfectly. But currently it has computations. which means that lots loops are used , math function are used. So it taking more computational cost. So I will be work on that, will be post updated stopwatch soon………

2 Comments leave one →
  1. August 10, 2011 3:25 pm

    Its great dude….

    Keep rocking….

    One day you will visualize your future too…

    All the best….

  2. mani permalink
    August 15, 2011 9:39 am

    great work

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: