program Body

use std_type
Use japi

implicit none
intrinsic :: int,sin

real(kind=DP),parameter :: PI = 3.141592653
integer, Parameter :: N = 500
integer,PARAMETER :: WIDTH = 600
integer,PARAMETER :: HIGH = 400
integer :: frame,canvas,i,xi,yi
real(kind=DP) :: dx
real(kind=DP),dimension(N) :: X,Y

dx = (2*PI)/(N-1)
X(1) = 0.0_DP
Y(1) = 0.0_DP
do i=2,N
    X(i) = X(i-1)+dx
    Y(i) = sin(X(i))
end do
    

IF( .NOT. j_start()) THEN
  PRINT*, "can't connect to JAPI server"
  CALL j_quit()
  stop
END IF


frame = j_frame("Body")
call j_setsize(frame,WIDTH+20,HIGH+20)
call j_setnamedcolorbg(frame,J_WHITE)

canvas = j_canvas(frame,WIDTH,HIGH)
! svisla poloha vzhledem k "frame"
! svisle 30, ne 10 !!!
call j_setpos(canvas,10,30)

call j_show(frame)
! call j_pack(frame)

call j_setnamedcolor(canvas,j_blue)
call j_drawline(canvas,0,HIGH/2,WIDTH,HIGH/2)
call j_drawline(canvas,0,0,0,HIGH)

call j_setnamedcolor(canvas,j_red)

call j_show(frame)
call Cekej(1)

do i=1,N
    xi = int((X(i)*WIDTH)/(2*PI))
    yi = int((sin(X(i))*HIGH)/2.2_DP)+HIGH/2
    call j_drawcircle(canvas,xi,yi,2)
end do

call j_show(frame)
	
do
    if(j_nextaction() /= frame) exit
end do
          
call j_quit()

contains

subroutine Cekej(sekund)
    ! Po uplynuti "sekund" vraci "true"
    integer,intent(in) :: sekund
    integer :: cnt1,cnt2

    call system_clock(cnt1)
    do 
        call system_clock(cnt2)
        if (abs(cnt2-cnt1)>=sekund) exit
    end do
end subroutine Cekej

end program Body

