segunda-feira, 22 de janeiro de 2018

Javafx - imprimindo um vector de inteiros

Aqui está um bom exemplo de como imprimir uma lista
de vector em javafx.
Note que esta inicialização poderia ser diferente,
poderia ser a partir de um laço for, más optei por fazer assim.

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

Veja abaixo o código do programa:




import java.util.Arrays;
import java.util.Vector;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Node;
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.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.geometry.Insets;

public class PROJETO extends Application {
     static int i;
     static String str = " ";
     static Canvas canvas = new Canvas ( 620, 350 );
     static GraphicsContext ctx = canvas.getGraphicsContext2D ( );
     static BorderPane borderPane = new BorderPane ( );
     static Pane ro_ot = new Pane ( );
     static Scene sce_ne = new Scene ( ro_ot, 620, 350 );
     static Text t_0 = new Text ( );
     static Vector < String > vect_List = new Vector <> ( Arrays.asList (
              "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
              "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22",
              "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33",
              "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44",
              "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55",
              "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66",
              "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77",
              "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88",
              "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100" ) );
     // /////////////////////////////////////////////////////////////////////////
     public static Node Arr_List ( ) {
         Group y = new Group ( );
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
         t_0.setFont ( Font.font ( "Arial", FontWeight.BOLD, 15 ) );
         int i = 0;
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Imprimindo abaixo a lista de inteiros", 190, 80 );
         ctx.setFont ( Font.font ( "Arial", FontWeight.BOLD, 14 ) );
         ctx.setFill ( Color.BLACK );
         for ( i = 0; i < vect_List.size ( ); i++ ) {
              if ( i % 10 == 0 ){
                   str += "\n";
              }
              if ( i >= 0 && i < 9 )
                   str += ( "0" );
              str += vect_List.get ( i ) + "\t";
         }
         t_0.setFill ( Color.BLACK );
         t_0.setX ( 160 );// coluna
         t_0.setY ( 110 );// linha
         t_0.setText ( str );
         return y;
     }
     // /////////////////////////////////////////////////////////////////////////
     public void start ( Stage stage ) {
         stage.setTitle ( "IMPRIMINDO VECTOR EM JAVAFX" );
         // Dando uns efeitos na moldura
         ro_ot.setStyle ( "-fx-padding: 5;" + "-fx-border-style: solid inside;"
                   + "-fx-border-width: 18;" + "-fx-border-insets: 5;"
                   + "-fx-border-radius: 5;" + "-fx-border-color: blue;" );
         // Configurando o local exato do menu
         borderPane.setPadding ( new Insets ( 23, 500, 0, 23 ) );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "IMPRIMINDO VECTOR EM JAVAFX", 210, 50 );
         Arr_List ( );
         ro_ot.getChildren ( ).addAll ( canvas, borderPane, t_0 );
         stage.setScene ( sce_ne );
         stage.show ( );
     }
     // /////////////////////////////////////////////////////////////////////////
     public static void main ( String [ ] args ) {
         Application.launch ( args );
     }
}

//EXEMPLO EM INTEGER QUE PODE SER INICIADO POR UM LAÇO FOR


import java.util.Arrays;
import java.util.Vector;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Node;
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.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.geometry.Insets;

public class PROJETO extends Application {
     static int i;
     static String str = " ";
     static Canvas canvas = new Canvas ( 620, 350 );
     static GraphicsContext ctx = canvas.getGraphicsContext2D ( );
     static BorderPane borderPane = new BorderPane ( );
     static Pane ro_ot = new Pane ( );
     static Scene sce_ne = new Scene ( ro_ot, 620, 350 );
     static Text t_0 = new Text ( );
     static Vector < Integer > vect_List = new Vector < > ( Arrays.asList (
              0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
              11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
              21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
              31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
              41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
              51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
              61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
              71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
              81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
              91, 92, 93, 94, 95, 96, 97, 98, 99 ) );
     // /////////////////////////////////////////////////////////////////////////
     public static Node Arr_List ( ) {
         Group y = new Group ( );
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 15 ) );
         t_0.setFont ( Font.font ( "Arial", FontWeight.BOLD, 15 ) );
         int i = 0;
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Imprimindo abaixo a lista de inteiros", 190, 80 );
         ctx.setFont ( Font.font ( "Arial", FontWeight.BOLD, 14 ) );
         ctx.setFill ( Color.BLACK );
         //removendo o número 22
         vect_List.remove ( 22 );
         //Adicionando o número 99 no lugar do 22
         vect_List.add ( 22, 99 );
         //Trocando o número 22 por 99
         //Collections.replaceAll ( vect_List, 22, 99 );
         for ( i = 0; i < vect_List.size ( ); i++ ) {
              if ( vect_List.get ( i ) % 10 == 0 ){
                   str += "\n";
              }
              if ( i >= 0 && i <= 9 )
                   str += ( "0" );
              str += vect_List.get ( i ) + "\t";
         }
         t_0.setFill ( Color.BLACK );
         t_0.setX ( 160 );// coluna
         t_0.setY ( 110 );// linha
         t_0.setText ( str );
         return y;
     }
     // /////////////////////////////////////////////////////////////////////////
     public void start ( Stage stage ) {
         stage.setTitle ( "IMPRIMINDO VECTOR EM JAVAFX" );
         // Dando uns efeitos na moldura
         ro_ot.setStyle ( "-fx-padding: 5;" + "-fx-border-style: solid inside;"
                   + "-fx-border-width: 18;" + "-fx-border-insets: 5;"
                   + "-fx-border-radius: 5;" + "-fx-border-color: blue;" );
         // Configurando o local exato do menu
         borderPane.setPadding ( new Insets ( 23, 500, 0, 23 ) );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "IMPRIMINDO VECTOR EM JAVAFX", 210, 50 );
         Arr_List ( );
         ro_ot.getChildren ( ).addAll ( canvas, borderPane, t_0 );
         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.