segunda-feira, 30 de julho de 2018

JavaFx - desenhando degraus em 3D

Desde o post anterior, estamos desenhando
objetos em 3D no cenário do javaFx,
e como  já falamos sobre a classe shape3D,
agora estamos mostrando um exemplo de
como criarmos caixas em 3D, observe,
que para se obter um cubo basta editarmos
as dimensões de cada Box deixando-as em
dimensões iguais.

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


Veja abaixo o código do programa:


import java.io.IOException;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;

public class PROJETO extends Application {
     Canvas canvas = new Canvas ( 600, 350 );
     GraphicsContext ctx = canvas.getGraphicsContext2D ( );
     Pane root = new Pane ( );
     Scene scene = new Scene ( root );
     //Criando as caixas ( degraus ).
     Box rect_1 = new Box ( 200, 30, 100 );
     Box rect_2 = new Box ( 200, 30, 100 );
     Box rect_3 = new Box ( 200, 30, 100 );
     Box rect_4 = new Box ( 200, 30, 100 );
     // Criando PhongMaterial
     PhongMaterial material_1 = new PhongMaterial ( );
     PhongMaterial material_2 = new PhongMaterial ( );
     PhongMaterial material_3 = new PhongMaterial ( );
     PhongMaterial material_4 = new PhongMaterial ( );
     // /////////////////////////////////////////////////////////////////////////
     public void Informe ( ) {
         ctx.setFont ( Font.font ( "Arial", FontWeight.NORMAL, 13 ) );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "Por: ", 300, 280 );
         ctx.setFill ( Color.BLUE );
         ctx.fillText ( "Samuel Lima", 340, 280 );
         ctx.setFill ( Color.BLACK );
         ctx.fillText ( "sa_sp10@hotmail.com", 300, 295 );
         ctx.setFill ( Color.RED );
         ctx.fillText ( " MUITO OBRIGADO", 345, 320 );
     }
     // /////////////////////////////////////////////////////////////////////////
     public void start ( Stage stage ) throws IOException {
         stage.setTitle ( "JAVAFX - DESENHANDO DEGRAUS EM 3D" );
         // Usando fonte em italic
         ctx.setFont ( Font.font ( "Helvetica", FontWeight.BOLD,
                   FontPosture.ITALIC, 15 ) );
         ctx.setFill ( Color.RED );
         ctx.fillText ( "JAVAFX - DESENHANDO DEGRAUS EM 3D", 150, 35 );
         // Criando uma moldura retangular em canvas
         ctx.setStroke ( Color.BLUE );
         ctx.setLineWidth ( 10.0 );
         ctx.strokeRoundRect ( 10, 10, 580, 330, 10, 10 );
         ctx.setFont ( Font.font ( "Helvetica", FontWeight.BOLD,
                   FontPosture.ITALIC, 12 ) );
     ////////////////////////////////////////////////////////////////////////
         material_1.setDiffuseColor ( Color.BLUE );
         material_1.setSpecularColor ( Color.BLACK );
         rect_1.setMaterial ( material_1 );
         rect_1.setLayoutX ( 150 );
         rect_1.setLayoutY ( 250 );
         rect_1.getTransforms ( ).add ( new Rotate ( 20, Rotate.X_AXIS ) );
         rect_1.getTransforms ( ).add ( new Rotate ( 10, Rotate.Z_AXIS ) );
         rect_1.getTransforms ( ).add ( new Rotate ( 30, Rotate.Y_AXIS ) );
     ////////////////////////////////////////////////////////////////////////
         material_2.setDiffuseColor ( Color.BLUE );
         material_2.setSpecularColor ( Color.BLACK );
         rect_2.setMaterial ( material_2 );
         rect_2.setLayoutX ( 202 );
         rect_2.setLayoutY ( 200 );
         rect_2.getTransforms ( ).add ( new Rotate ( 20, Rotate.X_AXIS ) );
         rect_2.getTransforms ( ).add ( new Rotate ( 10, Rotate.Z_AXIS ) );
         rect_2.getTransforms ( ).add ( new Rotate ( 30, Rotate.Y_AXIS ) );
     ////////////////////////////////////////////////////////////////////////
         material_3.setDiffuseColor ( Color.BLUE );
         material_3.setSpecularColor ( Color.BLACK );
         rect_3.setMaterial ( material_3 );
         rect_3.setLayoutX ( 254 );
         rect_3.setLayoutY ( 150 );
         rect_3.getTransforms ( ).add ( new Rotate ( 20, Rotate.X_AXIS ) );
         rect_3.getTransforms ( ).add ( new Rotate ( 10, Rotate.Z_AXIS ) );
         rect_3.getTransforms ( ).add ( new Rotate ( 30, Rotate.Y_AXIS ) );
     ////////////////////////////////////////////////////////////////////////
         material_4.setDiffuseColor ( Color.BLUE );
         material_4.setSpecularColor ( Color.BLACK );
         rect_4.setMaterial ( material_3 );
         rect_4.setLayoutX ( 306 );
         rect_4.setLayoutY ( 100 );
         rect_4.getTransforms ( ).add ( new Rotate ( 20, Rotate.X_AXIS ) );
         rect_4.getTransforms ( ).add ( new Rotate ( 10, Rotate.Z_AXIS ) );
         rect_4.getTransforms ( ).add ( new Rotate ( 30, Rotate.Y_AXIS ) );
         root.getChildren ( ).addAll ( rect_1, rect_2, rect_3, rect_4, canvas );
         Informe ( );
         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.