terça-feira, 15 de novembro de 2016

Javafx - Adicionando imagens e textos coloridos

JavaFX é uma poderosa biblioteca para 
criações de aplicativos java.
Com os recursos avançados que tem,
é possível criar aplicações para desktop,
browser e dispositivos móveis.
É possível adicionar imagens, áudio e vídeo,
sendo ideal também para criação de jogos.
Neste exemplo mostro como adicionar imagens do computador,
além de uma bela interface com moldura e menu,
o programa mostra ainda fontes coloridas e bem posicionadas.

Veja abaixo imagens do programa em execução:




Abaixo as imagens usada para criar o programa:








Veja abaixo o código do programa:


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
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.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;

public class PROJETO extends Application {
     @Override
     public void start ( Stage stage ) throws FileNotFoundException {
          ///////////////////////////////////////////////////////////////////////
          Image image = new Image ( new FileInputStream (
                     "C:\\Users\\SAMUEL 64SP1\\Desktop\\Times\\Palmeiras.PNG" ) );
          ImageView imageView = new ImageView ( image );
          BorderPane root = new BorderPane ( );
          imageView.setX ( 50 );
          imageView.setY ( 120 );
          imageView.setFitHeight ( 55 );
          imageView.setFitWidth ( 50 );
          imageView.setPreserveRatio ( true );
          root.getChildren ( ).add ( imageView );
          ////////////////////////////////////////////////////////////////////////
          Image image1 = new Image ( new FileInputStream (
                     "C:\\Users\\SAMUEL 64SP1\\Desktop\\Times\\Santos.PNG" ) );
          ImageView imageView1 = new ImageView ( image1 );
          imageView1.setX ( 50 );
          imageView1.setY ( 180 );
          imageView1.setFitHeight ( 55 );
          imageView1.setFitWidth ( 50 );
          imageView1.setPreserveRatio ( true );
          root.getChildren ( ).add ( imageView1 );
          ////////////////////////////////////////////////////////////////////////
          Image image2 = new Image ( new FileInputStream (
                     "C:\\Users\\SAMUEL 64SP1\\Desktop\\Times\\Flamengo.PNG" ) );
          ImageView imageView2 = new ImageView ( image2 );
          imageView2.setX ( 50 );
          imageView2.setY ( 240 );
          imageView2.setFitHeight ( 55 );
          imageView2.setFitWidth ( 50 );
          imageView2.setPreserveRatio ( true );
          root.getChildren ( ).add ( imageView2 );
          ////////////////////////////////////////////////////////////////////////
          Image image3 = new Image ( new FileInputStream (
                     "C:\\Users\\SAMUEL 64SP1\\Desktop\\Times\\Atlético-MG.PNG" ) );
          ImageView imageView3 = new ImageView ( image3 );
          imageView3.setX ( 50 );
          imageView3.setY ( 300 );
          imageView3.setFitHeight ( 55 );
          imageView3.setFitWidth ( 50 );
          imageView3.setPreserveRatio ( true );
          root.getChildren ( ).add ( imageView3 );
          ////////////////////////////////////////////////////////////////////////
          Image image4 = new Image ( new FileInputStream (
                     "C:\\Users\\SAMUEL 64SP1\\Desktop\\Times\\Botafogo.PNG" ) );
          ImageView imageView4 = new ImageView ( image4 );
          imageView4.setX ( 50 );
          imageView4.setY ( 360 );
          imageView4.setFitHeight ( 55 );
          imageView4.setFitWidth ( 50 );
          imageView4.setPreserveRatio ( true );
          root.getChildren ( ).add ( imageView4 );
          ////////////////////////////////////////////////////////////////////////
          Image image5 = new Image ( new FileInputStream (
                     "C:\\Users\\SAMUEL 64SP1\\Desktop\\Times\\Atlético-PR.PNG" ) );
          ImageView imageView5 = new ImageView ( image5 );
          imageView5.setX ( 50 );
          imageView5.setY ( 420 );
          imageView5.setFitHeight ( 55 );
          imageView5.setFitWidth ( 50 );
          imageView5.setPreserveRatio ( true );
          root.getChildren ( ).add ( imageView5 );
          ////////////////////////////////////////////////////////////////////////
          // BorderPane root = new BorderPane ( );
          Scene scene = new Scene ( root, 660, 505 );
          Canvas canvas = new Canvas ( 690, 540 );
          GraphicsContext ctx = canvas.getGraphicsContext2D ( );
          ctx.setFont ( Font.font ( "Arial", FontWeight.BOLD, 15 ) );
          ctx.setLineWidth ( 22.0 );
          ctx.setStroke ( Color.RED );
          ctx.strokeRect ( 10, 35, 640, 460 );
          stage.setTitle ( "Tabela do Brasileirão 2016" );
          ctx.setFill ( Color.RED );
          ctx.fillText ( "OS SEIS MELHORES DO BRASILEIRÃO 2016", 180, 80 );
        ////////////////////////////////////////////////////////////////////////
          ctx.setFill ( Color.BLUE );
          ctx.fillText ( "Times", 50, 110 );
        ///////////////////////////////////TABELA///////////////////////////////
          ctx.setFill ( Color.BLUE );
          ctx.fillText ( " P        J        V        E        D", 150, 110 );
          ctx.fillText ( " GP       GC       SG       %", 360, 110 );
          ctx.setFill ( Color.BLACK );
          //////////////////////////////////PALMEIRAS////////////////////////////
          ctx.fillText ( "70      34       21       7        6        57 "
                     + "       30         27       68", 150, 145  );
         //////////////////////////////////SANTOS////////////////////////////////
          ctx.setFill ( Color.BLACK );
          ctx.fillText ( "64      34       20       4       10       53"
                     + "        29        24        62", 150, 205 );
          /////////////////////////////////FLAMENGO//////////////////////////////
          ctx.setFill ( Color.BLACK );
          ctx.fillText ( "63      34       18       9        7        47"
                     + "        33        14        61", 150, 265 );
         /////////////////////////////////ATLÉTICO-MG///////////////////////////
          ctx.setFill ( Color.BLACK );
          ctx.fillText ( "60      34       17       9        8        56"
                     + "         44        12       58", 150, 325 );
        //////////////////////////////////BOTAFOGO//////////////////////////////
          ctx.setFill ( Color.BLACK );
          ctx.fillText ( "55      34       16       7       11       41"
                     + "        35        6         53", 150, 385 );
             ///////////////////////////////ATLÉTICO-PR///////////////////////////
          ctx.setFill ( Color.BLACK );
          ctx.fillText ( "52      35       16       4       15       36"
                     + "        32        4         49", 150, 445 );
        ////////////////////////////////////////////////////////////////////////
          imageView.setPreserveRatio ( true );
          root.getChildren ( ).add ( canvas );
          stage.setScene ( scene );
          stage.show ( );
          ////////////////////////////////////////////////////////////////////////
          MenuBar m_nu_1 = new MenuBar ( );
          Menu m_nu = new Menu ( "Arq" );
          MenuItem exd = new MenuItem ( "Sair" );
          MenuItem te_d = new MenuItem ( "Sobre" );
          m_nu.getItems ( ).addAll ( te_d, exd );
          ////////////////////////////////////////////////////////////////////////
          m_nu_1.getMenus ( ).add ( m_nu );
          root.setTop ( m_nu_1 );
          ////////////////////////////////////////////////////////////////////////
          exd.setOnAction ( new EventHandler < ActionEvent > ( ) {
                @Override
                public void handle ( ActionEvent e ) {
                     stage.close ( );
                }
          } );
          te_d.setOnAction ( new EventHandler < ActionEvent > ( ) {
                @Override
                public void handle ( ActionEvent e ) {
                     Alert alert = new Alert ( AlertType.INFORMATION );
                     alert.setTitle ( "Atenção" );
                     alert.setHeaderText ( null );
                     alert.setContentText ( "Programa criado por Samuel Lima" );
                     alert.showAndWait ( );
                }
          } );
     }
     ////////////////////////////////////////////////////////////////////////////
     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.