domingo, 22 de abril de 2018

JavaFX - carregando barra horizontal

Embora o javaFX já tenha um progresso bar profissional
pronto para ser implementado, resolvi criar esta aqui
manualmente, isto é didático e quem sabe servirá
para complementar algum programa no futuro.

Veja abaixo um vídeo com o programa funcionando:



Veja abaixo o código do programa: 


import javafx.application.Application;

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.BorderPane;
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.concurrent.Task;

////////////////////////////////////////////////////////////////////////////////
public class PROJETO extends Application {
     public static StringBuilder st = new StringBuilder ( );
     public static StringBuilder st1 = new StringBuilder ( );
     private static Timeline     videoTick, videoclick_1;
     static int a = 0,  k = 0, load = 100, x1 = 190, y = 0;
     public static int TAM = 10;
     static Canvas canvas = new Canvas ( 540, 350 );
     static GraphicsContext ctx = canvas.getGraphicsContext2D ( );
     static BorderPane ro_ot = new BorderPane ( );
     static Scene sce_ne = new Scene ( ro_ot, 540, 350 );
     //static Boolean a = false;

////////////////////////////////////////////////////////////////////////////
     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", 220, 320 );
     }
     // /////////////////////////////////////////////////////////////////////////
     public static int Sleep_3 ( int x ) {
         Task < Integer > close = new Task < Integer > ( ) {
              @Override
              protected Integer call ( ) throws Exception {
                   do {
                        Thread.sleep ( x );
                        System.out.printf ( " %d",  y );
                        y++;
                   } while ( y < 100 );
                   return null;
              }
         };       
         new Thread ( close ).start ( );
         return 0;
     }
     // /////////////////////////////////////////////////////////////////////////
     //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 ( load );
                            st = new StringBuilder ( );
                            ctx.clearRect ( 180, 130, 400, 30 );
                            y = 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 );
                   }
              } ) );
              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++;
                        if ( y == 10 ) {
                            videoclick_1.pause ( );    
                            videoTick.play ( );
                            k = 0;
                            st = new StringBuilder ( );
                            ctx.clearRect ( 180, 130, 300, 200 );
                        }                      
                        ctx.clearRect ( 200, 140, 400, 30 );
                        ctx.setFill ( Color.RED );
                        ctx.fillText (  " " + y, 240, 150 );
                        st.append ( "▀" );
                        ctx.setFill ( Color.BLUE );
                        ctx.fillText (  " " + st, 190, 140 );
                        Informe ( );
                   }
              } ) );
              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 HORIZONTAL" );
         //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 HORIZONTAL", 110, 50 );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Barra horizontal sendo carregada", 170, 80 );
         Sleep ( load );
         ro_ot.getChildren ( ).addAll ( canvas );
         stage.setScene ( sce_ne );
         stage.show ( );
     }
     // /////////////////////////////////////////////////////////////////////////
     public static void main ( String [ ] args ) {
         Application.launch ( args );
     }
}
 

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.