sábado, 10 de março de 2018

Javafx - de decimal para cardinais

Este programa foi criado originalmente
em linguagem C por mim mesmo, a cinco
anos atrás, e lembro que na época
foi um grande sucesso de visualizações no blog.
Agora, resolvi convertê-lo em java usando como

 interface os recursos da biblioteca javafx.
O programa está limitado entre 0 e 10000,
mas nada impede os mais experientes expandir
estes valores adequando-o as suas necessidades.


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.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.event.EventHandler;
import javafx.scene.control.TextField;
import javafx.geometry.Insets;
import javafx.scene.layout.HBox;
import javafx.scene.input.KeyEvent;
import java.util.function.UnaryOperator;
import javafx.scene.control.TextFormatter;
import javafx.scene.control.TextFormatter.Change;      

public class PROJETO extends Application {
     static StringBuilder Card = new StringBuilder ( );
     static Pane root = new Pane ( );
     static Canvas canvas = new Canvas ( 620, 350 );
     static GraphicsContext ctx = canvas.getGraphicsContext2D ( );
     static Scene scene = new Scene ( root, 620, 350, Color.WHITE );
     static TextField textField_1 = new TextField ( );
     static HBox hbox_1 = new HBox ( textField_1 );
     public static int n = 0, un, de, ce, mi;
////////////////////////////////////////////////////////////////////////////    
     public static void Informe ( ) {
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 13 ) );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "Por: ", 250, 305 );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Samuel Lima", 280, 305 );
         ctx.setFill ( Color.BLACK );
         ctx.fillText ( "sa_sp10@hotmail.com", 250, 320 );
     }
     // /////////////////////////////////////////////////////////////////////////
     public static void FuncUnidade ( ) {
         ctx.setFill ( Color.RED );
         if ( un == 1 ) {      
              Card.append ( "Um" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( un == 2 ) {
              Card.append ( "Dois" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( un == 3 ) {
              Card.append ( "Três" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( un == 4 ) {
              Card.append ( "Quatro" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( un == 5 ) {
              Card.append ( "Cinco" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( un == 6 ) {
              Card.append ( "Seis" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( un == 7 ) {
              Card.append ( "Sete" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( un == 8 ) {
              Card.append ( "Oito" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( un == 9 ) {
              Card.append ( "Nove" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
     }
///////////////////////////////////////////////////////////////////////////   
     public static void Cardinais ( String args ) {
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
         // Coluna, linha, comprimento, altura
         ctx.clearRect ( 170, 70, 400, 250 );
         Card = new StringBuilder ( );
         mi = ( n / 1000 );
         //separa centenas
         ce = ( ( n - ( mi * 1000 ) ) / 100 );
         //separa dezenas
         de = ( ( n - ( ( mi * 1000 ) + ( ce * 100 ) ) ) / 10 );
         //separa unidades
         un = ( n - ( ( mi * 1000 ) + ( ce * 100 ) + ( de * 10 ) ) );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Voce digitou: ", 180, 80 );
         ctx.setFill ( Color.RED );
         ctx.fillText ( mi + " " + ce + " " +  de + " " +  un + " ", 270, 80 );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Separando por grupo temos: ", 180, 110 );
         mi = mi * 1000;
         ce = ce * 100;
         de = de * 10;
         un = un * 1;
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Milhar", 180, 140 );
         ctx.setFill ( Color.RED );
         ctx.fillText ( mi / 1000 + " ", 260, 140 );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Centenas", 180, 160 );
         ctx.setFill ( Color.RED );
         ctx.fillText ( ce / 100 + " ", 260, 160 );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Dezenas", 180, 180 );
         ctx.setFill ( Color.RED );
         ctx.fillText ( de / 10 + " ", 260, 180 );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Unidades", 180, 200 );
         ctx.setFill ( Color.RED );
         ctx.fillText ( un / 1 + " ", 260, 200 );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Em Cardinal: ", 180, 230 );  
         Informe ( );
         ctx.setFill ( Color.RED );
         if ( mi == 9000 ) {
              if ( ce == 0 && de == 0 && un == 0 ) {
                   Card.append ( "Nove Mil" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else {
                   Card.append ( "Nove Mil e " );
                   ctx.fillText ( " " + Card, 180, 250 );
              }
         }
         if ( mi == 8000 ) {
              if ( ce == 0 && de == 0 && un == 0 ) {
                   Card.append ( "Oito Mil" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else {
                   Card.append ( "Oito Mil e " );
                   ctx.fillText ( " " + Card, 180, 250 );
              }
         }
         if ( mi == 7000 ) {
              if ( ce == 0 && de == 0 && un == 0 ) {
                   Card.append ( "Sete Mil" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else {
                   Card.append ( "Sete Mil e " );
                   ctx.fillText ( " " + Card, 180, 250 );
              }
         }
         if ( mi == 6000 ) {
              if ( ce == 0 && de == 0 && un == 0 ) {
                   Card.append ( "Seis Mil" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else {
                   Card.append ( "Seis Mil e " );
                   ctx.fillText ( " " + Card, 180, 250 );
              }
         }
         if ( mi == 5000 ) {
              if ( ce == 0 && de == 0 && un == 0 ) {
                   Card.append ( "Cinco Mil" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else {
                   Card.append ( "Cinco Mil e " );
                   ctx.fillText ( " " + Card, 180, 250 );
              }
         }
         if ( mi == 4000 ) {
              if ( ce == 0 && de == 0 && un == 0 ) {
                   Card.append ( "Quatro Mil" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else {
                   Card.append ( "Quatro Mil e " );
                   ctx.fillText ( " " + Card, 180, 250 );
              }
         }
         if ( mi == 3000 ) {
              if ( ce == 0 && de == 0 && un == 0 ) {
                   Card.append ( "Três Mil" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else {
                   Card.append ( "Três Mil e " );
                   ctx.fillText ( " " + Card, 180, 250 );
              }
         }
         if ( mi == 2000 ) {
              if ( ce == 0 && de == 0 && un == 0 ) {
                   Card.append ( "Dois Mil" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else {
                   Card.append ( "Dois Mil e " );
                   ctx.fillText ( " " + Card, 180, 250 );
              }
         }
         if ( mi == 1000 ) {
              if ( ce == 0 && de == 0 && un == 0 ) {
                   Card.append ( "Mil" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else {
                   Card.append ( "Mil e " );
                   ctx.fillText ( " " + Card, 180, 250 );
              }
         }
         if ( ce == 900 ) {
              if ( de == 0 && un == 0 ) {
                   Card.append ( "Novecentos" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "Novecentos e " );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( ce == 800 ) {
              if ( de == 0 && un == 0 ) {
                   Card.append ( "Oitocentos" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "Oitocentos e " );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( ce == 700 ) {
              if ( de == 0 && un == 0 ) {
                   Card.append ( "Setecentos" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "Setecentos e " );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( ce == 600 ) {
              if ( de == 0 && un == 0 ) {
                   Card.append ( "Seiscentos" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "Seiscentos e " );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( ce == 500 ) {
              if ( de == 0 && un == 0 ) {
                   Card.append ( "Quinhentos" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "Quinhentos e " );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( ce == 400 ) {
              if ( de == 0 && un == 0 ) {
                   Card.append ( "Quatrocentos" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "Quatrocentos e " );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( ce == 300 ) {
              if ( de == 0 && un == 0 ) {
                   Card.append ( "Trezentos" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "Trezentos e " );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( ce == 200 ) {
              if ( de == 0 && un == 0 ) {
                   Card.append ( "Duzentos" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "Duzentos e " );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( ce == 100 ) {
              if ( de == 0 && un == 0 ) {
                   Card.append ( "Cem" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "Cento e " );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( de == 20 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "Vinte" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "Vinte e " );
              ctx.fillText ( " " + Card, 180, 250 );
              FuncUnidade ( );
              main ( args );
         }
         if ( de == 30 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "Trinta" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "Trinta e " );
              ctx.fillText ( " " + Card, 180, 250 );
              FuncUnidade ( );
              main ( args );
         }
         if ( de == 40 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "Quarenta" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "Quarenta e " );
              ctx.fillText ( " " + Card, 180, 250 );
              FuncUnidade ( );
              main ( args );
         }
         if ( de == 50 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "Cincoenta" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "Cincoenta e " );
              ctx.fillText ( " " + Card, 180, 250 );
              FuncUnidade ( );
              main ( args );
         }
         if ( de == 60 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "Sessenta" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "Sessenta e " );
              ctx.fillText ( " " + Card, 180, 250 );
              FuncUnidade ( );
              main ( args );
         }
         if ( de == 70 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "Setenta" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "Setenta e " );
              ctx.fillText ( " " + Card, 180, 250 );
              FuncUnidade ( );
              main ( args );
         }
         if ( de == 80 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "Oitenta" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "Oitenta e " );
              ctx.fillText ( " " + Card, 180, 250 );
              FuncUnidade ( );
              main ( args );
         }
         if ( de == 90 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "Noventa" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "Noventa e " );
              ctx.fillText ( " " + Card, 180, 250 );
              FuncUnidade ( );
              main ( args );
         }
         if ( un == 1 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "Um" );
                   ctx.fillText ( " " + Card, 180, 250 );
                   main ( args );
              } else
                   Card.append ( "Onze" );
              ctx.fillText ( " " + Card, 180, 250 );
              main ( args );
         }
         if ( un == 2 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "Dois" );
                   ctx.fillText ( " " + Card, 180, 250 );
                   main ( args );
              } else
                   Card.append ( "Doze" );
              ctx.fillText ( " " + Card, 180, 250 );
              main ( args );
         }
         if ( un == 3 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "Três" );
                   ctx.fillText ( " " + Card, 180, 250 );
                   main ( args );
              } else
                   Card.append ( "Treze" );
              ctx.fillText ( " " + Card, 180, 250 );
              main ( args );
         }
         if ( un == 4 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "Quatro" );
                   ctx.fillText ( " " + Card, 180, 250 );
                   main ( args );
              } else
                   Card.append ( "Catorze" );
              ctx.fillText ( " " + Card, 180, 250 );
              main ( args );
         }
         if ( un == 5 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "Cinco" );
                   ctx.fillText ( " " + Card, 180, 250 );
                   main ( args );
              } else
                   Card.append ( "Quinze" );
              ctx.fillText ( " " + Card, 180, 250 );
              main ( args );
         }
         if ( un == 6 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "Seis" );
                   ctx.fillText ( " " + Card, 180, 250 );
                   main ( args );
              } else
                   Card.append ( "Dezesseis" );
              ctx.fillText ( " " + Card, 180, 250 );
              main ( args );
         }
         if ( un == 7 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "Sete" );
                   ctx.fillText ( " " + Card, 180, 250 );
                   main ( args );
              } else
                   Card.append ( "Dezessete" );
              ctx.fillText ( " " + Card, 180, 250 );
              main ( args );
         }
         if ( un == 8 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "Oito" );
                   ctx.fillText ( " " + Card, 180, 250 );
                   main ( args );
              } else
                   Card.append ( "Dezoito" );
              ctx.fillText ( " " + Card, 180, 250 );
              main ( args );
         }
         if ( un == 9 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "Nove" );
                   ctx.fillText ( " " + Card, 180, 250 );
                   main ( args );
              } else
                   Card.append ( "Dezenove" );
              ctx.fillText ( " " + Card, 180, 250 );
              main ( args );
         }
         if ( de == 10 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "Dez" );
                   ctx.fillText ( " " + Card, 180, 250 );
                   main ( args );
              }
         }
         if ( mi  == 0 && ce == 0 && de == 0 && un == 0 ) {
              Card.append ( "Zero" );
              ctx.fillText ( " " + Card, 180, 250 );
              main ( args );
         }
     }
     // /////////////////////////////////////////////////////////////////////////
     public static void limitTextField ( TextField textField, int limit ) {
         UnaryOperator < Change > textLimitFilter = change -> {
              if ( change.isContentChange ( ) ) {
                   int newLength = change.getControlNewText ( ).length ( );
                   if ( newLength > limit ) {
                        String trimmedText = change.getControlNewText ( )
                                 .substring ( 0, limit );
                        change.setText ( trimmedText );
                        int oldLength = change.getControlText ( ).length ( );
                        change.setRange ( 0, oldLength );
                   }
              }
              return change;
         };
         textField.setTextFormatter ( new TextFormatter < Object > (
                   textLimitFilter ) );
     }
     // /////////////////////////////////////////////////////////////////////////
     public static int Pesquisa ( String args ) {
         hbox_1.setPadding ( new Insets ( 290, 0, 10, 170 ) );
         // Abaixo ajustamos o total de colunas do TextField
         textField_1.setPrefColumnCount ( 4 );
         textField_1.setPromptText ( "pesq" );
         ctx.setFont ( Font.font ( "Tahoma", FontWeight.NORMAL, 12 ) );
         ctx.setFill ( Color.BLACK );
         ctx.fillText ( "Número", 170, 280 );
         textField_1.setOnKeyPressed ( new EventHandler < KeyEvent > ( ) {
              @Override
              public void handle ( KeyEvent event ) {
                   limitTextField ( textField_1, 4 );
                   if ( event.getCode ( ) == KeyCode.ENTER ) {
                        ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 13 ) );
                        ctx.setFill ( Color.BLUE );
                        // convertendo string para inteiro
                        n = Integer.parseInt ( textField_1.getText ( ) );
                        if ( Integer.parseInt ( textField_1.getText ( ) ) >= 0
                                 && Integer.parseInt ( textField_1.getText ( ) ) < 10000 ) {
                            Cardinais ( args );
                        }
                   }
              }
         } );
         root.getChildren ( ).add ( hbox_1 );
         return 0;
     }
     // ////////////////////////////////////////////////////////////////////////
     public void start ( Stage stage ) {
         String args = "";
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
         stage.setTitle ( "JAVAFX - DE DECIMAL PARA CARDINAIS" );
         //Criando moldura e dando efeitos
         root.setStyle ( "-fx-padding: 5;" + "-fx-border-style: solid inside;"
                   + "-fx-border-width: 12;" + "-fx-border-insets: 5;"
                   + "-fx-border-radius: 5;" + "-fx-border-color: blue;" );

         ctx.setFill ( Color.RED );
         ctx.fillText ( "JAVAFX - DE DECIMAL PARA CARDINAIS", 180, 50 );
         Pesquisa ( args  );
         stage.setScene ( scene );
         root.getChildren ( ).addAll ( canvas );
         stage.show ( );
     }
     // /////////////////////////////////////////////////////////////////////////
     public static void main ( String args ) {
         launch ( args );
     }
}

Nenhum comentário:

Postar um comentário

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