domingo, 26 de março de 2017

Javafx - Imprimindo array multidimensional de string

Veja como é fácil imprimir um array multidimensional de string em Javafx.

Imagem abaixo:




import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.Pane;
import javafx.animation.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.util.Duration;
public class PROJETO extends Application {
     private static Timeline     videoTick;
     static int k = 0;
     static String str = " ";
     static Stage  stage;
     Scene              scene;
     static Pane        root     = new Pane ( );
     static Canvas canvas = new Canvas ( 660, 460 );
     static GraphicsContext ctx = canvas.getGraphicsContext2D ( );
     public static int Tempo_1 ( int t ) {
         if ( t == 1 ){
              ctx.setFill ( Color.BLUE );
              ctx.fillText ( "Criado por:", 225, 380 );
         }
         if ( t == 2 ){
              ctx.setFill ( Color.MAGENTA );
              ctx.fillText ( "Samuel Lima", 315, 380 );
         }
         if ( t == 3 ){
              ctx.setFill ( Color.RED );
              ctx.fillText ( "MUITO OBRIGADO", 270, 420 );
         }
         if ( t == 4 )
         System.exit ( 0 );
         return 0;
     }
     public static int Sleep ( int x ) {
         do{
              Duration duration = Duration.millis ( x );
              videoTick = new Timeline ( new KeyFrame ( duration,
                        new EventHandler < ActionEvent > ( ) {
                   public void handle ( ActionEvent actionEvent ) {
                        System.out.println ( k );
                        k++;
                        Tempo_1 ( k );    
                   }
              } ) );
              break;
         }while ( true );
         videoTick.setCycleCount ( Animation.INDEFINITE );
         videoTick.playFromStart ( );
         return 0;
     }
     @Override
     public void start ( Stage stage ) throws InterruptedException {
         ctx.setStroke ( Color.RED );//MUDA A COR DA MOLDURA
         ctx.setLineWidth ( 22.0 );
         ctx.strokeRoundRect ( 10, 10, 640, 440, 10, 10 );
         root.getChildren ( ).add ( canvas );
         Scene scene = new Scene ( root );
         stage.setTitle ( "ARRAY MULTIDIMENSIONAL DE STRING" );
         ctx.setFont ( Font.font ( "Arial", FontWeight.BOLD, 15 ) );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "ARRAY MULTIDIMENSIONAL DE STRING", 180, 60 );
         ObservableList < Node > content = ( ( Pane ) scene.getRoot ( ) )
                   .getChildren ( );
         stage.setScene ( scene );
         stage.show ( );
         content.add ( Line ( ) );
     }
     public static Node Line ( ) {
         int i, j;
         Group k = new Group ( );
         String [ ] [ ] texto = {
                {"No Meio do Caminho                        "},
                 {"Carlos Drummond de Andrade                "},
                 {"No meio do caminho tinha uma pedra        "},
                 {"Tinha uma pedra no meio do caminho        "},
                 {"Tinha uma pedra                           "},
                 {"No meio do caminho tinha uma pedra.       "},
                 {"Nunca me esquecerei desse acontecimento   "},
                 {"Na vida de minhas retinas tão fatigadas.  "},
                 {"Nunca me esquecerei que no meio do caminho"},
                 {"Tinha uma pedra                           "},
                 {"Tinha uma pedra no meio do caminho        "},
                 {"No meio do caminho tinha uma pedra.       "}};
         for ( i = 0; i < texto.length; i++ ) {
            if ( i == 1 )
                 str += ( "\n" );
            if ( i == 2 )
                 str += ( "\n" );
            if ( i == 3 )
                 str += ( "\n" );
            if ( i == 4 )
                 str += ( "\n" );
            if ( i == 5 )
                 str += ( "\n" );
            if ( i == 6 )
                 str += ( "\n" );
            if ( i == 7 )
                 str += ( "\n" );
            if ( i == 8 )
                 str += ( "\n" );
            if ( i == 9 )
                 str += ( "\n" );
            if ( i == 10 )
                 str += ( "\n" );
            if ( i == 11 )
                str += ( "\n" );
            str = str + texto [ i ] [ 0 ];
            for ( j = 1; j < texto [ i ].length; j++ ) {
                 str = str + texto [ i ] [ j ] + " ";
            }
      }
         ctx.setFill ( Color.BLACK );
        ctx.fillText ( str, 150, 100 );
        Sleep ( 1000 );//um segundo
         return k;
     }
     public static void main ( String [ ] args ) {
         Application.launch ( args );
     }
}