quarta-feira, 10 de julho de 2024

JavaFx - Gerador de cpf - Label num ScrollArea

 

JavaFX - Gerador de cpf

Apenas para fins didáticos,
e sem me responsabilizar por uso indevido,
apresento este gerador de cpf.
O gerador aqui proposto pode gerar
até 11 cpfs de uma só vez,
e foi criado em linguagem java.
Quantidades inferiores a 1,
ou maior que 11 não passa pela entrada,
como também qualquer caractere,
passando mesmo tão somente dígitos.
Fica em sua responsabilidade o uso,
não indicado pelo autor que sou eu mesmo.


Veja abaixo imagens do programa em execução:




package application;

 

import javafx.scene.Scene;

import javafx.stage.Stage;

import javafx.application.Application;

import javafx.geometry.Insets;

import javafx.scene.layout.GridPane;

import javafx.scene.layout.HBox;

import javafx.scene.layout.VBox;

import javafx.scene.layout.BorderPane;

import javafx.scene.canvas.Canvas;

import javafx.scene.canvas.GraphicsContext;

import javafx.scene.layout.Pane;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.scene.paint.Color;

import javafx.scene.control.Label;

import javafx.scene.control.Menu;

import javafx.scene.control.MenuBar;

import javafx.scene.control.MenuItem;

import javafx.scene.control.ScrollPane;

import javafx.scene.control.TextField;

import javafx.scene.text.Font;

import javafx.scene.text.FontPosture;

import javafx.scene.text.FontWeight;

import javafx.scene.text.Text;

import javafx.scene.input.KeyCode;

import javafx.scene.input.KeyEvent;

import java.util.Random;

import java.util.function.UnaryOperator;

import javafx.scene.control.TextFormatter;

import javafx.scene.control.TextFormatter.Change;

 

public class Main extends Application {

     static String [ ] receita = {

         "CPF é o Cadastro de Pessoa Física,\n",

         "um número composto por 11 dígitos\n",

         "que fica registrado na Receita Federal\n",

         "armazenando informações sobre cada pessoa.\n",

         "Este programa não tem ligação alguma\n",

         "com a receita federal,\n",

         "e os cpfs aqui gerados mesmo válidos\n",

         "não estão vinculados, isto significa\n",

         "que eles não existem.\n"};        

  

     static TextField textField_1 = new TextField ( );

     static HBox hbox_1 = new HBox ( textField_1 );

     static StringBuilder st = new StringBuilder ( );

    

     static int Vet_cpf [ ] = new int [ 12 ];

     static int Vet_cpf_2 [ ] = new int [ 12 ];

     static int Vet_cpf_3 [ ] = new int [ 12 ];

     static int val_cpf [ ] = { 10, 9, 8, 7, 6, 5, 4, 3, 2 };

     static int val_cpf_2 [ ] = { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 };

     static int n = 9;

     static int a = 0, e = 0, f = 0, g = 0, h = 0, i = 0, j = 0, l = 0;

     static int tot = 0;

   

     static String str_1;

   

     public static Label lbl = new Label ( );

     public static Label lbl_2 = new Label ( );

   

     static Text t_1 = new Text ( );

     static Text t_2 = new Text ( );

     static Text t_3 = new Text ( );

   

     static Canvas canvas = new Canvas ( );

     static GraphicsContext ctx = canvas.getGraphicsContext2D ( );

     static ScrollPane scroll = new ScrollPane ( );

    

    

     static VBox vbox = new VBox ( );

     static GridPane grid = new GridPane ( );

     static MenuBar menuBar = new MenuBar ( );

     static Pane ro_ot = new Pane ( );

     static BorderPane root = new BorderPane ( );

     static Scene scene = new Scene ( vbox, 500, 300 );

     // /////////////////////////////////////////////////////////////////////////

     public static int Menu_Bar ( ) {

         EventHandler < ActionEvent > action = changeTabPlacement ( );

         Menu menu = new Menu ( "Arq" );

         MenuItem cpf = new MenuItem ( "Cpf" );

         MenuItem sair = new MenuItem ( "Sair" );

         MenuItem sobre = new MenuItem ( "Sobre" );

         menu.getItems ( ).add ( cpf );

         menu.getItems ( ).add ( sobre );

         menu.getItems ( ).add ( sair );    

         cpf.setOnAction ( action );

         sobre.setOnAction ( action );

         sair.setOnAction ( action );

         menuBar.getMenus ( ).add ( menu );

         root.setTop ( menuBar );

         return 0;

     }

     // /////////////////////////////////////////////////////////////////////////

     private static EventHandler < ActionEvent > changeTabPlacement ( ) {

         String args = "";

        

         return new EventHandler < ActionEvent > ( ) {

              public void handle ( ActionEvent event ) {

                 

                MenuItem mItem = ( MenuItem ) event.getSource ( );

                  

                String side = mItem.getText ( );

                  

                   if ( "Cpf".equalsIgnoreCase ( side ) ) {

                      

                      st = new StringBuilder ( );

                      

                      tot = 0;

                      

                      textField_1.clear ( );

                      

                       //text.clear ( );

                        textField_1.setDisable ( false );

                        textField_1.setVisible ( true );            

                       

                        scroll.setDisable ( false );

                        scroll.setVisible ( true );

                      

                        lbl_2.setVisible ( true );

                      

                        t_1.setDisable ( true );

                        t_1.setVisible ( false );

                     

                        Pesquisa ( args );

                       

                        Scrool_Area ( );

                   }

                   else if ( "Sobre".equalsIgnoreCase ( side ) ) {

                        int i = 0;

                      

                        st = new StringBuilder ( );

                       

                        for ( i = 0; i < receita.length; i++ ) {

                            st.append ( receita [ i ] );

                        }

                      

                        t_1.setX ( 120 );

                        t_1.setY ( 80 );

                        t_1.setFill ( Color.BLACK );

                        t_1.setText ( " " + st ); 

                        t_1.setDisable ( false );

                        t_1.setVisible ( true );

                      

                        lbl_2.setVisible ( false );

                      

                        textField_1.setDisable ( true );

                        textField_1.setVisible ( false );

                       

                        scroll.setDisable ( true );

                        scroll.setVisible ( false );

                       

                        root.getChildren ( ).addAll ( t_1 );

                   }

                   if ( "Sair".equalsIgnoreCase ( side ) ) {

                        System.exit ( 0 );

                   }

              }

             

         };

     }

     // /////////////////////////////////////////////////////////////////////////

     public static void bloq_textfield ( TextField textField, int limit ) {

         UnaryOperator < Change > filtro = change -> {

              if ( change.isContentChange ( ) ) {

                   int newLength = change.getControlNewText ( ).length ( );

                   if ( newLength > limit ) {

                        String textst = change.getControlNewText ( )

                                 .substring ( 0, limit );

                        change.setText ( textst );

                        int oldLength = change.getControlText ( ).length ( );

                        change.setRange ( 0, oldLength );

                   }

              }

              return change;

         };

         textField.setTextFormatter ( new TextFormatter < Object > ( filtro ) );               

     }

     // /////////////////////////////////////////////////////////////////////////

     public static void Gera_Cpf ( ) {

         Random random = new Random ( );

        

         t_3.setFont ( Font.font ( "Helvetica", FontWeight.BOLD,

                 FontPosture.ITALIC, 12 ) );

      

         scroll.setContent ( t_3 );

     

         for ( a = 0; a < tot; a++ ) {

              for ( i = 0; i < n; i++ ) {

                   // Geramos 10 números e inserimos no array Vet_cpf [ ]

                   Vet_cpf [ i ] = random.nextInt ( 10 );

              }

              for ( i = 0; i < 9; i++ ) {

                   Vet_cpf_2 [ i ] = Vet_cpf [ i ] * val_cpf [ i ];

                   e = e += Vet_cpf_2 [ i ];

              }

              f = e % 11;

              g = 11 - f;

              if ( g > 9 )

                   g = 0;

              // /////////////////////////////////////////////////////////////////

              Vet_cpf [ 9 ] = g;

              for ( i = 0; i < 10; i++ ) {

                   Vet_cpf_3 [ i ] = Vet_cpf [ i ] * val_cpf_2 [ i ];

                   h = h += Vet_cpf_3 [ i ];

              }

              j = h % 11;

              l = 11 - j;

              if ( l > 9 )

                   l = 0;

             

              Vet_cpf [ 10 ] = l;

            

              // Abaixo o cpf gerado já com seus dois últimos dígitos

              ctx.setFill ( Color.BLACK );

              for ( i = 0; i < 11; i++ ) {

                   if ( i == 3 )

                        st.append ( "." );

                   if ( i == 6 )

                        st.append ( "." );

                   if ( i == 9 )

                        st.append ( " - " );

                   st.append ( Vet_cpf [ i ] );

              }

             

              if ( i % 11 == 0 && tot > 0 && tot <= 11 )

                   st.append ( "\n" );

              t_3.setText ( " " + st );

              e = 0;

              f = 0;

              g = 0;

              h = 0;

              i = 0;

              j = 0;

              l = 0;

         }

     }

     // /////////////////////////////////////////////////////////////////////////

     public static int Pesquisa ( String args ) {

         // Posiciona o local exato do textfield

         hbox_1.setPadding ( new Insets ( 43, 0, 10, 300 ) );

       

         // Abaixo ajustamos o total de colunas do TextField

         textField_1.setPrefColumnCount ( 2 );

         textField_1.setPromptText ( "cpf" );

 

         textField_1.setOnKeyPressed ( new EventHandler < KeyEvent > ( ) {

              @Override

              public void handle ( KeyEvent event ) {

                    bloq_textfield ( textField_1, 2 );

                   if ( event.getCode ( ) == KeyCode.ENTER ) {

                        // convertendo string para inteiro

                        tot = Integer.parseInt ( textField_1.getText ( ) );

                        if ( Integer.parseInt ( textField_1.getText ( ) ) >= 0

                                 && Integer.parseInt ( textField_1.getText ( ) ) < 12 ) {

                            Gera_Cpf ( );

                        }

                   }

              }

         } );

        

         ro_ot.getChildren ( ).addAll ( hbox_1 );

         vbox.getChildren ( ).addAll ( scroll);

        

         return 0;

     }

     // /////////////////////////////////////////////////////////////////////////

     public static int Scrool_Area ( ) {

         // Largura máximo do Scrool_Area

     scroll.setPrefWidth ( 180 );

         // Comprimento máxima do Scrool_Area

     scroll.setPrefHeight ( 120 );

       

     // Adiciona o Scrool_Area na horizontal

         grid.setHgap ( 2 );

         //Posiciona a Margem do Scrool_Area

         grid.add ( scroll, 70, 2, 4, 4 );

        

         scroll.setStyle ( "-fx-text-fill: blue" );

        

         // Posiciona o label na horizontal

         lbl_2.setLayoutX ( 140 );

         // Posiciona o label na vertical

         lbl_2.setLayoutY ( 50 );

         // Aplica cor no rótulo

         lbl_2.setTextFill ( Color.BLACK );

         lbl_2.setText ( "Quantos cpfs deseja gerar?" );

        

         root.getChildren ( ).addAll ( t_3 );

        

         return 0;

     }

     // /////////////////////////////////////////////////////////////////////////

     @Override

     public void start ( Stage stage ) throws Exception {

         stage.setTitle ( "JAVAFX - GERADOR DE CPFS" );

        

         Menu_Bar ( );

         scroll.setDisable ( true );

         scroll.setVisible ( false );

        

         // Usando fonte em italic

         lbl.setFont ( Font.font ( "Helvetica", FontWeight.BOLD,

                   FontPosture.ITALIC, 16 ) );

         // Posiciona o label na horizontal

         lbl.setLayoutX ( 130 );

         // Posiciona o label na vertical

         lbl.setLayoutY ( 15 );

         // Aplica cor no rótulo

         lbl.setTextFill ( Color.RED );

         lbl.setText ( "JAVAFX - GERADOR DE CPFS" );

        

         // Criando moldura

         vbox.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;" );  

                 

         ro_ot.getChildren ( ).addAll ( lbl, lbl_2 );

         vbox.getChildren ( ).addAll ( root, ro_ot,  grid );

         stage.setScene ( scene );

         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.