program digits

use japi
  	
        integer :: frame,seven,panel,obj,i,v
        integer,dimension(1:4) :: led,mouse
        
        if( .not. j_start()) then
            print*, "can't connect to JAPI server"   
            goto 20
        endif

        frame = j_frame("Digits")
        call j_setborderlayout(frame)
        call j_setinsets(frame,j_getinsets(frame,J_TOP)+10,10,10,10)
        call j_setvgap(frame,10)

        panel = j_panel(frame)
        call j_setborderpos(panel,J_BOTTOM)
        call j_setflowlayout(panel,J_HORIZONTAL)
        call j_sethgap(panel,10)
        do i=4,1,-1
           led(i)=j_led(panel,J_ROUND,J_RED)
	   mouse(i)=j_mouselistener(led(i),J_RELEASED)
        enddo

        seven = j_sevensegment(frame,J_GREEN)

        call j_setsize(frame,150,250)
        call j_show(frame)
    
        obj=0

        mainloop: do
  
           obj=j_nextaction()
		
           do i=1,4
              if(obj == mouse(i)) then 
                  call j_setstate(led(i),.not. j_getstate(led(i)))
              endif
           enddo
           
           v=0
           if(j_getstate(led(4))) v=v+8
           if(j_getstate(led(3))) v=v+4
           if(j_getstate(led(2))) v=v+2
           if(j_getstate(led(1))) v=v+1
           call j_setvalue(seven,v)
    
           if(obj == frame) goto 20
        
        enddo mainloop

20      continue
        call j_quit()

        end program digits
