segunda-feira, 12 de março de 2018

Javafx - de decimal para romanos II

Há poucos dias passados postei por aqui
um link de um programa que havíamos criado
para converter de decimal para romanos,
e o programa foi bem recebido pela comunidade.
Mas para iniciantes o código se tornou complicado,

a classe criada é difícil de se entender, então usando
nossa habilidade, editei o programa:
"Javafx - de decimal para cardinais", 
criado por mim mesmo e transformei num
conversor de decimal para romanos,
e ficou bem mais simples de entender.
Esta cadeia de ifs usados de propósito por mim
podem ser substituída de uma forma elegante
pelo operador ternário do java,
mas os iniciantes não o entendem tão rápido,
então, preferi fazê-lo assim, aproveitem
o código, para estudos, copiem cole e compile.


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 ( ) {
         if ( un == 1 ) {      
              Card.append ( "I" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( un == 2 ) {
              Card.append ( "II" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( un == 3 ) {
              Card.append ( "III" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( un == 4 ) {
              Card.append ( "IV" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( un == 5 ) {
              Card.append ( "V" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( un == 6 ) {
              Card.append ( "VI" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( un == 7 ) {
              Card.append ( "VII" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( un == 8 ) {
              Card.append ( "VIII" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( un == 9 ) {
              Card.append ( "IX" );
              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 romanos: ", 180, 230 );   
         Informe ( );
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 16 ) );
         ctx.setFill ( Color.BLACK );
         if ( mi == 9000 ) {
              if ( ce == 0 && de == 0 && un == 0 ) {
                   Card.append ( "MMMMMMMMM" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else {
                   Card.append ( "MMMMMMMMM" );
                   ctx.fillText ( " " + Card, 180, 250 );
              }
         }
         if ( mi == 8000 ) {
              if ( ce == 0 && de == 0 && un == 0 ) {
                   Card.append ( "MMMMMMMM" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else {
                   Card.append ( "MMMMMMMM" );
                   ctx.fillText ( " " + Card, 180, 250 );
              }
         }
         if ( mi == 7000 ) {
              if ( ce == 0 && de == 0 && un == 0 ) {
                   Card.append ( "MMMMMMM" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else {
                   Card.append ( "MMMMMMM" );
                   ctx.fillText ( " " + Card, 180, 250 );
              }
         }
         if ( mi == 6000 ) {
              if ( ce == 0 && de == 0 && un == 0 ) {
                   Card.append ( "MMMMMM" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else {
                   Card.append ( "MMMMMM" );
                   ctx.fillText ( " " + Card, 180, 250 );
              }
         }
         if ( mi == 5000 ) {
              if ( ce == 0 && de == 0 && un == 0 ) {
                   Card.append ( "MMMMM" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else {
                   Card.append ( "MMMMM" );
                   ctx.fillText ( " " + Card, 180, 250 );
              }
         }
         if ( mi == 4000 ) {
              if ( ce == 0 && de == 0 && un == 0 ) {
                   Card.append ( "MMMM" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else {
                   Card.append ( "MMMM" );
                   ctx.fillText ( " " + Card, 180, 250 );
              }
         }
         if ( mi == 3000 ) {
              if ( ce == 0 && de == 0 && un == 0 ) {
                   Card.append ( "MMM" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else {
                   Card.append ( "MMM" );
                   ctx.fillText ( " " + Card, 180, 250 );
              }
         }
         if ( mi == 2000 ) {
              if ( ce == 0 && de == 0 && un == 0 ) {
                   Card.append ( "MM" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else {
                   Card.append ( "MM" );
                   ctx.fillText ( " " + Card, 180, 250 );
              }
         }
         if ( mi == 1000 ) {
              if ( ce == 0 && de == 0 && un == 0 ) {
                   Card.append ( "M" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else {          
                   Card.append ( "M" );
                   ctx.fillText ( " " + Card, 180, 250 );
              }
         }
         if ( ce == 900 ) {
              if ( de == 0 && un == 0 ) {
                   Card.append ( "CM" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "CM" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( ce == 800 ) {
              if ( de == 0 && un == 0 ) {
                   Card.append ( "DCCC" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "DCCC" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( ce == 700 ) {
              if ( de == 0 && un == 0 ) {
                   Card.append ( "DCC" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "DCC" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( ce == 600 ) {
              if ( de == 0 && un == 0 ) {
                   Card.append ( "DC" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "DC" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( ce == 500 ) {
              if ( de == 0 && un == 0 ) {
                   Card.append ( "D" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "D" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( ce == 400 ) {
              if ( de == 0 && un == 0 ) {
                   Card.append ( "CD" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "CD" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( ce == 300 ) {
              if ( de == 0 && un == 0 ) {
                   Card.append ( "CCC" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "CCC" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( ce == 200 ) {
              if ( de == 0 && un == 0 ) {
                   Card.append ( "CC" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "CC" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( ce == 100 ) {
              if ( de == 0 && un == 0 ) {
                   Card.append ( "C" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "C" );
              ctx.fillText ( " " + Card, 180, 250 );
         }
         if ( de == 20 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "XX" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "XX" );
              ctx.fillText ( " " + Card, 180, 250 );
              FuncUnidade ( );
              main ( args );
         }
         if ( de == 30 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "XXX" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "XXX" );
              ctx.fillText ( " " + Card, 180, 250 );
              FuncUnidade ( );
              main ( args );
         }
         if ( de == 40 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "XL" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "XL" );
              ctx.fillText ( " " + Card, 180, 250 );
              FuncUnidade ( );
              main ( args );
         }
         if ( de == 50 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "L" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "L" );
              ctx.fillText ( " " + Card, 180, 250 );
              FuncUnidade ( );
              main ( args );
         }
         if ( de == 60 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "LX" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "LX" );
              ctx.fillText ( " " + Card, 180, 250 );
              FuncUnidade ( );
              main ( args );
         }
         if ( de == 70 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "LXX" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "LXX" );
              ctx.fillText ( " " + Card, 180, 250 );
              FuncUnidade ( );
              main ( args );
         }
         if ( de == 80 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "LXXX" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "LXXX" );
              ctx.fillText ( " " + Card, 180, 250 );
              FuncUnidade ( );
              main ( args );
         }
         if ( de == 90 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "XC" );
                   ctx.fillText ( " " + Card, 180, 250 );
              } else
                   Card.append ( "XC" );
              ctx.fillText ( " " + Card, 180, 250 );
              FuncUnidade ( );
              main ( args );
         }
         if ( un == 1 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "I" );
                   ctx.fillText ( " " + Card, 180, 250 );
                   main ( args );
              } else
                   Card.append ( "XI" );
              ctx.fillText ( " " + Card, 180, 250 );
              main ( args );
         }
         if ( un == 2 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "II" );
                   ctx.fillText ( " " + Card, 180, 250 );
                   main ( args );
              } else
                   Card.append ( "XII" );
              ctx.fillText ( " " + Card, 180, 250 );
              main ( args );
         }
         if ( un == 3 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "III" );
                   ctx.fillText ( " " + Card, 180, 250 );
                   main ( args );
              } else
                   Card.append ( "XIII" );
              ctx.fillText ( " " + Card, 180, 250 );
              main ( args );
         }
         if ( un == 4 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "IV" );
                   ctx.fillText ( " " + Card, 180, 250 );
                   main ( args );
              } else
                   Card.append ( "XIV" );
              ctx.fillText ( " " + Card, 180, 250 );
              main ( args );
         }
         if ( un == 5 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "V" );
                   ctx.fillText ( " " + Card, 180, 250 );
                   main ( args );
              } else
                   Card.append ( "XV" );
              ctx.fillText ( " " + Card, 180, 250 );
              main ( args );
         }
         if ( un == 6 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "VI" );
                   ctx.fillText ( " " + Card, 180, 250 );
                   main ( args );
              } else
                   Card.append ( "XVI" );
              ctx.fillText ( " " + Card, 180, 250 );
              main ( args );
         }
         if ( un == 7 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "VII" );
                   ctx.fillText ( " " + Card, 180, 250 );
                   main ( args );
              } else
                   Card.append ( "XVII" );
              ctx.fillText ( " " + Card, 180, 250 );
              main ( args );
         }
         if ( un == 8 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "VIII    " );
                   ctx.fillText ( " " + Card, 180, 250 );
                   main ( args );
              } else
                   Card.append ( "XVIII " );
              ctx.fillText ( " " + Card, 180, 250 );
              main ( args );
         }
         if ( un == 9 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "IX" );
                   ctx.fillText ( " " + Card, 180, 250 );
                   main ( args );
              } else
                   Card.append ( "XIX" );
              ctx.fillText ( " " + Card, 180, 250 );
              main ( args );
         }
         if ( de == 10 ) {
              if ( de == 0 || un == 0 ) {
                   Card.append ( "X" );
                   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 ROMANOS II" );
         //Criando moldura e dando efeitos
         root.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: pink;");      ctx.setFill ( Color.RED );
                   ctx.fillText ( "JAVAFX - DE DECIMAL PARA ROMANOS II", 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.