segunda-feira, 9 de abril de 2018

JavaFX - criando menu personalizado

"Bem-vindo ao maravilhoso mundo de java".
O java através de sua poderosa API javaFX,
permite a seus programadores personalizarem
seu próprios menus, e isto é muito especial.
Quanto mais se conhece destes recursos
e se sabe aplicar mais profissional ficará seu programa.
Neste exemplo estamos adicionando um Button,
um RadioButton, um Label e um CheckBox
para selecionar opções em um menu personalizado.
Mas ainda existem outras subclasses MenuItem
disponíveis para usar como opções de menu
que não estão sendo utilizados neste exemplo.


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


  
Veja abaixo o vídeo do programa:


Veja abaixo o código do programa: 


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.CustomMenuItem;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.RadioButton;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

public class PROJETO extends Application {
     String st = "JAVA";
     Canvas canvas = new Canvas ( 690, 350 );
     GraphicsContext ctx = canvas.getGraphicsContext2D ( );
     BorderPane root = new BorderPane ( );
     Scene scene = new Scene ( root, 660, 350 );
     MenuBar menuBar = new MenuBar ( );
     Menu mainMenu = new Menu ( "Personalizado" );
     CustomMenuItem person_1 = new CustomMenuItem ( new Button ( "Sair" ) );
     CustomMenuItem person_2 = new CustomMenuItem ( new Label ( st ) );
     CustomMenuItem person_3 = new CustomMenuItem ( new RadioButton ( ) );
     CustomMenuItem person_4 = new CustomMenuItem ( new CheckBox ( ) );
     Timeline videoTick;
     Duration duration = Duration.millis ( 2000 );
     ImageView imageView = new ImageView ( );
     DropShadow ds = new DropShadow ( );
     Text t = new Text ( );
     // /////////////////////////////////////////////////////////////////////////
     public void Java ( ) {
         ds.setOffsetY ( 3.0 );
         ds.setColor ( Color.color ( 0.4, 0.4, 0.4 ) );
         t.setEffect ( ds );
         t.setCache ( true );
         t.setX ( 100.0 );
         t.setY ( 320.0 );
         t.setFill ( Color.BLUE );
         t.setFont ( Font.font ( "Century", FontWeight.BOLD, 36 ) );
         t.setText ( "The wonderful world of java" );
     }
     // /////////////////////////////////////////////////////////////////////////
     public void Informe ( ) {       
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 13 ) );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "Por: ", 250, 245 );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Samuel Lima", 280, 245 );
         ctx.setFill ( Color.BLACK );
         ctx.fillText ( "sa_sp10@hotmail.com", 250, 260 );
         ctx.setFill ( Color.RED );
         ctx.fillText ( " MUITO OBRIGADO", 280, 290 );
         videoTick = new Timeline ( new KeyFrame ( duration,
                   new EventHandler < ActionEvent > ( ) {
                        public void handle ( ActionEvent actionEvent ) {
                            // Limpando a tela
                            // Coluna, linha, comprimento, altura
                            ctx.clearRect ( 240, 230, 330, 150 );
                            Principal ( );
                        }
                   } ) );
         videoTick.playFromStart ( );
     }
     // /////////////////////////////////////////////////////////////////////////
     public void Texto ( ) {
         ctx.clearRect ( 210, 70, 330, 50 );
         ctx.setFont ( Font.font ( "Arial", FontWeight.BOLD, 15 ) );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "JAVAFX - CRIANDO MENU PERSONALIZADO", 180, 80 );
         ctx.setFill ( Color.BLACK );
         int i;
         StringBuilder st = new StringBuilder ( );
         ArrayList < Character > arr = new ArrayList <> ( Arrays.asList ( 'B',
                   'E', 'M', '-', 'V', 'I', 'N', 'D', 'O', 'A', 'O', 'M', 'A',
                   'R', 'A', 'V', 'I', 'L', 'H', 'O', 'S', 'O', 'M', 'U', 'N',
                   'D', 'O', 'D', 'E', 'J', 'A', 'V', 'A' ) );
         for ( i = 0; i < arr.size ( ); i++ ) {
              if ( i % 11 == 0 ) {
                   st.append ( "\n" );
              }
              st.append ( arr.get ( i ) + "       " );
         }
         ctx.fillText ( " " + st, 130, 110 );
         videoTick = new Timeline ( new KeyFrame ( duration,
                   new EventHandler < ActionEvent > ( ) {
                        public void handle ( ActionEvent actionEvent ) {
                            // Limpando a tela
                            // Coluna, linha, comprimento, altura
                            ctx.clearRect ( 120, 100, 420, 150 );
                            Principal ( );
                        }
                   } ) );
         videoTick.playFromStart ( );
     }
     // /////////////////////////////////////////////////////////////////////////
     public void Menu ( Stage stage ) {
         mainMenu.getItems ( ).addAll ( person_1, person_2, person_3, person_4 );
         menuBar.getMenus ( ).add ( mainMenu );
         person_1.setOnAction ( actionEvent -> System.exit ( 0 ) );
         person_2.setOnAction ( actionEvent -> Texto ( ) );
         person_3.setOnAction ( actionEvent -> Informe ( ) );
         person_4.setOnAction ( new EventHandler < ActionEvent > ( ) {
              @Override
              public void handle ( ActionEvent e ) {
                   try {
                        Image_View ( );
                   } catch ( FileNotFoundException e1 ) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace ( );
                   }
              }
         } );
     }
     // /////////////////////////////////////////////////////////////////////////
     public void Principal ( ) {
         ctx.setFont ( Font.font ( "Arial", FontWeight.BOLD, 15 ) );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "JAVAFX - CRIANDO MENU PERSONALIZADO", 180, 80 );
         Java ( );
     }
     // /////////////////////////////////////////////////////////////////////////
     public int Image_View ( ) throws FileNotFoundException {
         Image image = new Image (
                   new FileInputStream (
                            "H:\\eclipse - luna java\\PROJETOS\\PROJETO\\src\\Foto\\java.png" ) );
         imageView = new ImageView ( image );
         // Coluna
         imageView.setX ( 240 );
         // Linha
         imageView.setY ( 90 );
         // Largura
         imageView.setFitHeight ( 640 );
         // Altura
         imageView.setFitWidth ( 150 );
         imageView.setPreserveRatio ( true );
         videoTick = new Timeline ( new KeyFrame ( duration,
                   new EventHandler < ActionEvent > ( ) {
                        public void handle ( ActionEvent actionEvent ) {
                            imageView.setVisible ( false );
                            Principal ( );
                        }
                   } ) );
         videoTick.playFromStart ( );
         root.getChildren ( ).addAll ( imageView );
         return 0;
     }
     // /////////////////////////////////////////////////////////////////////////
     public void start ( Stage stage ) {
         // Criando moldura e aplicando 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: pink;" );
         stage.setTitle ( "JAVAFX - CRIANDO MENU PERSONALIZADO" );
         Principal ( );
         Menu ( stage );
         root.getChildren ( ).addAll ( canvas, t );
         root.setTop ( menuBar );
         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.