sábado, 17 de março de 2018

Javafx - embaralhando array de char automaticamente

Faltam 1,2,3,4... segundos para embaralhar,
esta é a mensagem de aviso ao usuário,
e no fim dos dez segundos o array é embaralhado
automaticamente, e permanece na tela durante
três segundos e é apagado também automaticamente
para receber um novo embaralhamento.
O clock, criado cuidadosamente pode ser aproveitado
em diversas aplicações por um estudante avançado de java,
já o código não oferece nenhuma dificuldade em se entender
e é indicado a estudantes de nível médio em java.


Veja abaixo uma imagem do programa em execução:



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.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import java.util.Random;
import javafx.event.ActionEvent;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.EventHandler;
import javafx.util.Duration;
import javafx.animation.*;

////////////////////////////////////////////////////////////////////////////////
public class PROJETO extends Application {
     private static Timeline     videoTick;
     static int k = -1;
     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 );
     // ////////////////////////////////////////////////////////////////////////
     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 );
     }
     // /////////////////////////////////////////////////////////////////////////  
     public static int emb_lhar ( char vet [ ] ) {
         int i, temp, y;
         Random random = new Random ( );
         for ( i = 0; i < TAM; i++ ) {
              y = random.nextInt ( TAM );
              temp = vet [ i ];
              vet [ i ] = vet [ y ];
              vet [ y ] = ( char ) temp;
         }
         return i;
     }
////////////////////////////////////////////////////////////////////////////
     //Método usado como clock criado por mim
     public static int Sleep ( int x, char vet [ ] ) {     
         ctx.clearRect ( 180, 130, 500, 20 );
         ctx.setFill ( Color.BLUE );
         ctx.fillText (  "Faltam ", 190, 140 );
         ctx.setFill ( Color.BLUE );
         ctx.fillText (  " segundos para embaralhar", 260, 140 );       
         while ( k < 10 ){
              Duration duration = Duration.millis ( x );
              videoTick = new Timeline ( new KeyFrame ( duration,
                        new EventHandler < ActionEvent > ( ) {
                   public void handle ( ActionEvent actionEvent ) {      
                        k++;
                        if ( k == 3 ) {
                            // Coluna, linha, comprimento, altura
                            ctx.clearRect ( 180, 160, 580, 380 );
                        }
                        ctx.clearRect ( 240, 130, 20, 20 );
                        ctx.setFill ( Color.RED );
                        ctx.fillText (  " " + k, 240, 140 );
                        if ( k == 10 ){
                            But_ton_3 ( vet );
                            Informe ( );
                        }           
                   }
              } ) );
              break;
         }
         videoTick.setCycleCount ( Animation.INDEFINITE );
         videoTick.playFromStart ( );
         return k;
     }
     // //////////////////////////////////////////////////////////////////////////
     public static int But_ton_3 ( char vet [ ] ) {
         int a, i;
         StringBuilder st = new StringBuilder ( );
         // Coluna, linha, comprimento, altura
         ctx.clearRect ( 70, 130, 580, 180 );
         a = Sleep ( 1000, vet );
         k = -1;
         if ( a == 10 ){
              emb_lhar ( vet );              
              for ( i = 0; i < 10; i++ ){
                   st.append ( vet [ i ] + "    " );
              }  
              ctx.setFill ( Color.BLACK );
              ctx.fillText (  "O array foi embaralhado ", 190, 170 );
              ctx.setFill ( Color.RED );
              ctx.fillText ( " " + st, 190, 200 );
         }
         return 0;
     }
     // ////////////////////////////////////////////////////////////////////////
     public void start ( Stage stage ) {
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
         stage.setTitle ( "JAVAFX - EMBARALHANDO ARRAY DE CHAR EM AUTOMÁTICO" );
         //Criando moldura e dando efeitos
        ro_ot.setStyle ( "-fx-padding: 5;" + "-fx-border-style: solid inside;"
                + "-fx-border-width: 15;" + "-fx-border-insets: 5;"
                + "-fx-border-radius: 5;" + "-fx-border-color: black;"
                + "-fx-background: lightcyan;");
         ctx.setFill ( Color.RED );
         ctx.fillText ( "JAVAFX - EMBARALHANDO ARRAY DE CHAR EM AUTOMÁTICO", 90, 50 );
         String st = " ";
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Imprimindo abaixo o array de char", 190, 80 );
         ctx.setFill ( Color.BLACK );
         char vet [ ] = { 'U', 'S', 'E', '-', 'J', 'A', 'V', 'A', 'F', 'X' };
         int i;
         for ( i = 0; i < TAM; i++ ) {
              st += vet [ i ] + "    ";
         }
         ctx.setFill ( Color.RED );
         ctx.fillText ( st, 190, 110 );
         But_ton_3 ( vet );
         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.