domingo, 22 de abril de 2018

JavaFX - carregando barra horizontal e pausando

Embora o javaFX já tenha um progress bar
profissional pronto para ser implementado,
resolvi criar esta aqui manualmente,
isto é didático e quem sabe servirá para 

complementar algum programa no futuro.
O progress bar é pausado e reiniciado

automaticamente, vale a pena conferir.
 
Veja abaixo uma imagem do programa em execução:


Veja abaixo um vídeo com o programa funcionando:



Veja abaixo o código do programa: 


import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.event.ActionEvent;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.EventHandler;
import javafx.util.Duration;
import javafx.animation.*;
import javafx.application.Application;
////////////////////////////////////////////////////////////////////////////////
public class PROJETO extends Application {
     public static StringBuilder st = new StringBuilder ( );
     private static Timeline     videoTick, videoclick_1;
     static int a = 0, i, k = 0, load = 100, x1 = 190, y = 0;
     public static int TAM = 10;
     static Canvas canvas = new Canvas ( 620, 350 );
     static GraphicsContext ctx = canvas.getGraphicsContext2D ( );
     static Pane ro_ot = new Pane ( );
     static Scene sce_ne = new Scene ( ro_ot, 620, 350 );
     static String str_3 = "";
     public static StringBuilder st1 = new StringBuilder ( );
////////////////////////////////////////////////////////////////////////////
     public static void Informe ( ) {
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 13 ) );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "Por: ", 200, 275 );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Samuel Lima", 240, 275 );
         ctx.setFill ( Color.BLACK );
         ctx.fillText ( "sa_sp10@hotmail.com", 200, 290 );
         ctx.setFill ( Color.RED );
         ctx.fillText ( " MUITO OBRIGADO", 250, 320 );
     }
     // ////////////////////////////////////////////////////////////////////////
     //Método usado como clock criado por mim
     public static int Sleep ( int x ) {
         while ( k < 100 ) {
              Duration duration = Duration.millis ( x );
              videoTick = new Timeline ( new KeyFrame ( duration,
                        new EventHandler < ActionEvent > ( ) {
                   public void handle ( ActionEvent actionEvent ) {
                        //System.out.print ( k );
                        k++;
                        if ( k == 10 ) {
                            videoTick.pause ( );
                            Sleep_2 ( 2000 );
                            st = new StringBuilder ( );
                            ctx.clearRect ( 180, 130, 400, 30 );
                            str_3 = "1";
                            a = str_3.length ( );
                            str_3 = str_3.substring ( 0, a );
                            st1.append ( str_3 );
                            k = 0;
                        }     
                        ctx.clearRect ( 200, 140, 300, 200 );
                        ctx.setFill ( Color.RED );
                        ctx.fillText (  " " + k, 240, 150 );
                        st.append ( "▀" );
                        ctx.setFill ( Color.rgb ( 4, 238, 144 ) );
                        ctx.fillText (  " " + st, 190, 140 );
                        Informe ( );
                   }
              } ) );
              break;
         }
         videoTick.setCycleCount ( Animation.INDEFINITE );
         videoTick.playFromStart ( );
         return k;
     }
     // ////////////////////////////////////////////////////////////////////////
     //Método usado como clock criado por mim
     public static int Sleep_2 ( int x ) {
         while ( y < 100 ) {
              Duration duration = Duration.millis ( x );
              videoclick_1 = new Timeline ( new KeyFrame ( duration,
                        new EventHandler < ActionEvent > ( ) {
                   public void handle ( ActionEvent actionEvent ) {
                        //System.out.print ( y );
                        y++;
                        videoclick_1.pause ( );  
                        st = new StringBuilder ( );
                        ctx.clearRect ( 180, 130, 300, 200 );
                        //Converte StringBuilder em String
                        str_3 = st1.toString ( );
                        //Converte String para inteiro
                        i = Integer.parseInt ( str_3 );
                        System.out.print ( i );
                        st1 = new StringBuilder ( );
                        if ( i == 1 )
                            videoTick.play ( );
                   }
              } ) );
              break;
         }
         videoclick_1.setCycleCount ( Animation.INDEFINITE );
         videoclick_1.playFromStart ( );
         return k;
     }
     // ////////////////////////////////////////////////////////////////////////
     public void start ( Stage stage ) {
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
         stage.setTitle ( "JAVAFX - CARREGANDO BARRA E PAUSANDO" );
         //Criando moldura e dando efeitos
         ro_ot.setStyle ( "-fx-padding: 5;" + "-fx-border-style: solid inside;"
                   + "-fx-border-width: 12;" + "-fx-border-insets: 5;"
                   + "-fx-border-radius: 5;" + "-fx-border-color: black;");
         ctx.setFill ( Color.RED );
         ctx.fillText ( "JAVAFX - CARREGANDO BARRA E PAUSANDO", 150, 50 );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Barra horizontal sendo carregada", 190, 80 );
         Sleep ( load );
         ro_ot.getChildren ( ).addAll ( canvas );
         stage.setScene ( sce_ne );
         stage.show ( );
     }
     // ////////////////////////////////////////////////////////////////////////
     public static void main ( String [ ] args ) {
         Application.launch ( args );
     }
}