Advertisement
Guest User

bala_velha_com_jfx

a guest
Aug 28th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.41 KB | None | 0 0
  1. /*
  2. jogo da velha
  3. bem10jfx.blogspot.com
  4. knbjfx.blogspot.com
  5. knb 's thugs luves
  6. */
  7. package balavelha;
  8.  
  9. import java.util.Random;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12. import javafx.application.Application;
  13. import javafx.event.Event;
  14. import javafx.event.EventHandler;
  15. import javafx.scene.Group;
  16. import javafx.scene.Scene;
  17. import javafx.scene.effect.Blend;
  18. import javafx.scene.effect.BlendMode;
  19. import javafx.scene.effect.Effect;
  20. import javafx.scene.paint.Color;
  21. import javafx.scene.paint.CycleMethod;
  22. import javafx.scene.paint.LinearGradient;
  23. import javafx.scene.paint.Stop;
  24. import javafx.scene.shape.Line;
  25. import javafx.scene.shape.Rectangle;
  26. import javafx.scene.shape.StrokeLineCap;
  27. import javafx.scene.text.Font;
  28. import javafx.scene.text.Text;
  29. import javafx.stage.Stage;
  30.  
  31. /**
  32. *
  33. * @author knb
  34. */
  35. public class BalaVelha extends Application {
  36.  
  37. /*
  38. int com numero impar = O; int com numero par=X;
  39. valor de retorno atravez de um rand ou segundos da hora
  40.  
  41. */
  42. Stage stage;
  43. public Text txvl_Rand;
  44. public Rectangle rect[] = new Rectangle[10];
  45. public Group gp[] = new Group[10];
  46. int i;
  47. int playOne = 1;
  48. public Text txX[] = new Text[10];
  49. public Scene scene;
  50. public Group go;
  51. Line line1 = new Line();
  52. Line line2 = new Line();
  53. Line line3 = new Line();
  54. Line line4 = new Line();
  55. Text textFKs = new Text();
  56.  
  57. public void thugs() {
  58. Random rand = new Random(System.currentTimeMillis());
  59. int strRand = rand.nextInt((int) 21);
  60. txvl_Rand = new Text();
  61. txvl_Rand.setLayoutX(5);
  62. txvl_Rand.setLayoutY(10);
  63. //confere gameplayer
  64. int limitplayerB = 11;
  65.  
  66. if (strRand >= limitplayerB) {
  67.     System.out.println("maior quer 11 ou igual a 11 play 1");
  68.     txvl_Rand.setText("Valor_Random" + strRand + "\nPlay1=X");
  69.  
  70.     //x =1;
  71.     playOne = 1;
  72. } else {
  73.     System.out.println("menor que 11 ou = a 10");
  74.     txvl_Rand.setText("Valor_Random" + strRand + "\nPlay1=O");
  75.     //O=0;
  76.     playOne = 0;
  77. }
  78. }
  79.  
  80. public void stageWind() {
  81.  
  82. Group gpWind = new Group();
  83. Scene scenexx = new Scene(gpWind, 500, 500, Color.RED);
  84.  
  85. gpWind.getChildren().add(textFKs);
  86.  
  87. stage.setScene(scenexx);
  88.  
  89. }
  90.  
  91. @Override
  92. public void start(Stage stagex) {
  93. stage = new Stage();
  94. thugs();
  95. go = new Group();
  96. Stop[] stops = {new Stop(0, Color.WHITE), new Stop(01, Color.SILVER)};
  97. LinearGradient lgOne = new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stops);
  98.  
  99. scene = new Scene(go, 500, 300, lgOne);
  100. stage.setScene(scene);
  101.  
  102. go.getChildren().add(txvl_Rand);
  103.  
  104. line1.startXProperty().bind(scene.widthProperty().divide(3));
  105. line1.startYProperty().bind(scene.heightProperty()
  106.         .subtract(scene.heightProperty().add(60)));
  107.  
  108. line1.endXProperty().bind(scene.widthProperty().divide(3));
  109. line1.endYProperty().bind(scene.heightProperty());
  110.  
  111. line2.startXProperty().bind(scene.widthProperty().divide(1.5));
  112. line2.startYProperty().bind(scene.heightProperty()
  113.         .subtract(scene.heightProperty().add(60)));
  114.  
  115. line2.endXProperty().bind(scene.widthProperty().divide(1.5));
  116. line2.endYProperty().bind(scene.heightProperty());
  117.  
  118. line3.startXProperty().bind(scene.widthProperty().subtract(scene.widthProperty()));
  119. line3.startYProperty().bind(scene.heightProperty().divide(3));
  120.  
  121. line3.endXProperty().bind(scene.widthProperty());
  122. line3.endYProperty().bind(scene.heightProperty().divide(3));
  123.  
  124. line4.startXProperty().bind(scene.widthProperty().subtract(scene.widthProperty()));
  125. line4.startYProperty().bind(scene.heightProperty().divide(1.5));
  126.  
  127. line4.endXProperty().bind(scene.widthProperty());
  128. line4.endYProperty().bind(scene.heightProperty().divide(1.5));
  129.  
  130. go.getChildren().addAll(line1, line2, line3, line4);
  131. line1.setStroke(Color.ALICEBLUE);
  132. line1.setStrokeLineCap(StrokeLineCap.BUTT);
  133. line1.setStrokeMiterLimit(60);
  134. line1.setStrokeWidth(10);
  135.  
  136. line2.setStroke(Color.ALICEBLUE);
  137. line2.setStrokeLineCap(StrokeLineCap.BUTT);
  138. line2.setStrokeMiterLimit(60);
  139. line2.setStrokeWidth(10);
  140.  
  141. line3.setStroke(Color.ALICEBLUE);
  142. line3.setStrokeLineCap(StrokeLineCap.BUTT);
  143. line3.setStrokeMiterLimit(60);
  144. line3.setStrokeWidth(10);
  145.  
  146. line4.setStroke(Color.ALICEBLUE);
  147. line4.setStrokeLineCap(StrokeLineCap.BUTT);
  148. line4.setStrokeMiterLimit(60);
  149. line4.setStrokeWidth(10);
  150. Stop[] stopGo = {new Stop(0, Color.BLUE), new Stop(01, Color.WHITE)};
  151. LinearGradient lg = new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stopGo);
  152.  
  153. line1.setStroke(lg);
  154. line2.setStroke(lg);
  155. line3.setStroke(lg);
  156. line4.setStroke(lg);
  157.  
  158. for (i = 1; i < 10; i++) {
  159.  
  160.     System.out.println("ln" + i);
  161.     gp[i] = new Group();
  162.     rect[i] = new Rectangle();
  163.     rect[i].setFill(Color.BLUE);
  164.     rect[i].setArcHeight(20);
  165.     rect[i].setArcWidth(20);
  166.     rect[i].setWidth(70);
  167.     rect[i].setHeight(70);
  168.     go.getChildren().addAll(gp[i]);
  169.     gp[i].getChildren().addAll(rect[i]);
  170.     final int er = i;
  171.     rect[i].setOnMouseClicked(new EventHandler<Event>() {
  172.         @Override
  173.         @SuppressWarnings("empty-statement")
  174.         public void handle(Event event) {
  175.  
  176.             System.out.println(er);
  177.  
  178.             rect[er].setEffect(new Blend(BlendMode.DARKEN));
  179.             rect[er].setStrokeDashOffset(10);
  180.             rect[er].setStrokeLineCap(StrokeLineCap.ROUND);
  181.             rect[er].setStroke(Color.AZURE);
  182.  
  183.             if (playOne == 1) {
  184.                 txX[er] = new Text("X");
  185.                 txX[er].setFont(new Font(71));
  186.                 txX[er].setWrappingWidth(200);
  187.                 txX[er].setStrokeDashOffset(100);
  188.                 txX[er].setStrikethrough(true);
  189.                 txX[er].setStrokeMiterLimit(100);
  190.                 txX[er].setY(056);
  191.                 gp[er].getChildren().add(txX[er]);
  192.                 try {
  193. //X
  194.  
  195.     if ("X".equals(txX[1].getText()) &
  196.             "X".equals(txX[2].getText()) &
  197.             "X".equals(txX[3].getText())) {
  198.         System.out.println("fim de jogo echo 1 finalizado");
  199.         textFKs.setText("Fim de Jogo \nUltimo Ganhador" + "\nGanhador: Xis ");
  200.         textFKs.setFont(new Font(100));
  201.         try {
  202.             stageWind();
  203.         } catch (Exception e) {
  204.             e.getMessage();
  205.         }
  206.     }
  207.     if ("X".equals(txX[1].getText()) &
  208.             "X".equals(txX[5].getText()) &
  209.             "X".equals(txX[9].getText())) {
  210.         try {
  211.             System.out.println("fim de jogo echo 2 finalizado");
  212.             textFKs.setText("Fim de Jogo \nUltimo Ganhador" + "\nGanhador: Xis");
  213.             textFKs.setFont(new Font(100));
  214.  
  215.             stageWind();
  216.         } catch (Exception e) {
  217.             e.getMessage();
  218.         }
  219.     }
  220.     if ("X".equals(txX[4].getText()) &
  221.             "X".equals(txX[5].getText()) &
  222.             "X".equals(txX[6].getText())) {
  223.  
  224.         try {
  225.             System.out.println("fim de jogo echo 3 finalizadox");
  226.             textFKs.setText("Fim de Jogo \nUltimo Ganhador" + "\nGanhador: Xis");
  227.             textFKs.setFont(new Font(100));
  228.  
  229.             stageWind();
  230.         } catch (Exception e) {
  231.             e.getMessage();
  232.         }
  233.  
  234.     }
  235.     if ("X".equals(txX[7].getText()) &
  236.             "X".equals(txX[8].getText()) &
  237.             "X".equals(txX[9].getText())) {
  238.         try {
  239.             System.out.println("fim de jogo echo 4 finalizado");
  240.             textFKs.setText("Fim de Jogo \nUltimo Ganhador" + "\nGanhador: Xis ");
  241.             textFKs.setFont(new Font(100));
  242.             stageWind();
  243.         } catch (Exception e) {
  244.             e.getMessage();
  245.         }
  246.     } else if ("X".equals(txX[7].getText()) &
  247.             "X".equals(txX[5].getText()) &
  248.             "X".equals(txX[3].getText())) {
  249.         try {
  250.             System.out.println("fim de jogo echo 5 finalizado");
  251.             textFKs.setText("Fim de Jogo \nUltimo Ganhador" + "\nGanhador: X ");
  252.             textFKs.setFont(new Font(100));
  253.             stageWind();
  254.         } catch (Exception e) {
  255.             e.getMessage();
  256.         }
  257.     } else if ("X".equals(txX[3].getText()) &
  258.             "X".equals(txX[6].getText()) &
  259.             "X".equals(txX[9].getText())) {
  260.         try {
  261.             System.out.println("fim de jogo echo 6 finalizado");
  262.             textFKs.setText("Fim de Jogo \nUltimo Ganhador" + "\nGanhador: X ");
  263.             textFKs.setFont(new Font(100));
  264.             stageWind();
  265.         } catch (Exception e) {
  266.             e.getMessage();
  267.         }
  268.     }
  269.     if ("X".equals(txX[1].getText()) &
  270.             "X".equals(txX[4].getText()) &
  271.             "X".equals(txX[7].getText())) {
  272.         try {
  273.             System.out.println("fim de jogo echo 7 finalizado");
  274.             textFKs.setText("Fim de Jogo \nUltimo Ganhador" + "\nGanhador: X ");
  275.             textFKs.setFont(new Font(100));
  276.             stageWind();
  277.         } catch (Exception e) {
  278.             e.getMessage();
  279.         }
  280.     }
  281.     if ("X".equals(txX[8].getText()) &
  282.             "X".equals(txX[5].getText()) &
  283.             "X".equals(txX[2].getText())) {
  284.         try {
  285.             System.out.println("fim de jogo echo 8 finalizado");
  286.             textFKs.setText("Fim de Jogo \nUltimo Ganhador" + "\nGanhador: X ");
  287.             textFKs.setFont(new Font(100));
  288.             stageWind();
  289.         } catch (Exception e) {
  290.             e.getMessage();
  291.         }
  292.     } else {
  293.     }
  294. } catch (Exception e) {
  295.     //severe
  296.     Logger.getLogger(balavelha.BalaVelha.class.getName()).log(Level.SEVERE, null, e);
  297.  
  298. }
  299. playOne = 0;
  300. } else {
  301. txX[er] = new Text("O");
  302. txX[er].setFont(new Font(71));
  303. txX[er].setWrappingWidth(200);
  304. txX[er].setStrokeDashOffset(100);
  305. txX[er].setStrikethrough(true);
  306. txX[er].setStrokeMiterLimit(100);
  307. txX[er].setY(056);
  308.  
  309. gp[er].getChildren().add(txX[er]);
  310.  
  311. try {
  312.     if ("O".equals(txX[1].getText().toString()) &
  313.             "O".equals(txX[2].getText()) &
  314.             "O".equals(txX[3].getText())) {
  315.         System.out.println("fim de jogo echo 1 finalizado");
  316.         textFKs.setText("Fim de Jogo \nUltimo Ganhador" + "\nGanhador: Y ");
  317.         textFKs.setFont(new Font(100));
  318.         try {
  319.             stageWind();
  320.         } catch (Exception e) {
  321.             e.getMessage();
  322.         }
  323.     } else if ("O".equals(txX[1].getText()) &
  324.             "O".equals(txX[5].getText()) & "O".equals(txX[9].getText())) {
  325.         System.out.println("fim de jogo echo 2 finalizado");
  326.         textFKs.setText("Fim de Jogo \nUltimo Ganhador" + "\nGanhador: X ");
  327.         textFKs.setFont(new Font(100));
  328.         try {
  329.             stageWind();
  330.         } catch (Exception e) {
  331.             e.getMessage();
  332.         }
  333.     } else if ("O".equals(txX[4].getText()) &
  334.             "O".equals(txX[5].getText()) & "O".equals(txX[6].getText())) {
  335.         System.out.println("fim de jogo echo 3 finalizado");
  336.         textFKs.setText("Fim de Jogo \nUltimo Ganhador" + "\nGanhador: X ");
  337.         textFKs.setFont(new Font(100));
  338.         try {
  339.             stageWind();
  340.         } catch (Exception e) {
  341.             e.getMessage();
  342.         }
  343.     } else if ("O".equals(txX[7].getText()) &
  344.             "O".equals(txX[8].getText()) & "O".equals(txX[9].getText())) {
  345.         System.out.println("fim de jogo echo 4 finalizado");
  346.         textFKs.setText("Fim de Jogo \nUltimo Ganhador" + "\nGanhador: X ");
  347.         textFKs.setFont(new Font(100));
  348.         try {
  349.             stageWind();
  350.         } catch (Exception e) {
  351.             e.getMessage();
  352.         }
  353.     } else if ("O".equals(txX[7].getText()) &
  354.             "O".equals(txX[5].getText()) & "O".equals(txX[3].getText())) {
  355.         System.out.println("fim de jogo echo 5 finalizado");
  356.         textFKs.setText("Fim de Jogo \nUltimo Ganhador" + "\nGanhador: O ");
  357.         textFKs.setFont(new Font(100));
  358.         try {
  359.             stageWind();
  360.         } catch (Exception e) {
  361.             e.getMessage();
  362.         }
  363.     } else if ("O".equals(txX[3].getText()) &
  364.             "O".equals(txX[6].getText()) & "O".equals(txX[9].getText())) {
  365.         System.out.println("fim de jogo echo 6 finalizado");
  366.         textFKs.setText("Fim de Jogo \nUltimo Ganhador" + "\nGanhador: O ");
  367.         textFKs.setFont(new Font(100));
  368.         try {
  369.             stageWind();
  370.         } catch (Exception e) {
  371.             e.getMessage();
  372.         }
  373.     } else if ("O".equals(txX[1].getText()) &
  374.             "O".equals(txX[4].getText()) & "O".equals(txX[7].getText())) {
  375.         System.out.println("fim de jogo echo 7 finalizado");
  376.         textFKs.setText("Fim de Jogo \nUltimo Ganhador" + "\nGanhador: O ");
  377.         textFKs.setFont(new Font(100));
  378.         try {
  379.             stageWind();
  380.         } catch (Exception e) {
  381.             e.getMessage();
  382.         }
  383.     } else if ("O".equals(txX[8].getText()) &
  384.             "O".equals(txX[5].getText()) & "O".equals(txX[2].getText())) {
  385.         System.out.println("fim de jogo echo 8 finalizado");
  386.         textFKs.setText("Fim de Jogo \nUltimo Ganhador" + "\nGanhador: O ");
  387.         textFKs.setFont(new Font(100));
  388.         try {
  389.             stageWind();
  390.         } catch (Exception e) {
  391.             e.getMessage();
  392.         }
  393.     }
  394.  
  395. } catch (Exception e) {
  396.     Logger.getLogger(balavelha.BalaVelha.class.getName()).log(Level.SEVERE, null, e);
  397. }
  398. playOne = 1;
  399. }
  400. }
  401. //velha
  402. });
  403. }
  404. gp[1].layoutXProperty().bind(scene.widthProperty().divide(6.7));
  405. gp[1].layoutYProperty().bind(
  406.         stage.heightProperty().subtract(
  407.                 scene.heightProperty().add(6.9)));
  408.  
  409. gp[2].layoutXProperty().bind(scene.widthProperty().divide(2.2));
  410. gp[2].layoutYProperty().bind(
  411.         stage.heightProperty().subtract(
  412.                 scene.heightProperty().add(6.9)));
  413.  
  414. gp[3].layoutXProperty().bind(scene.widthProperty().divide(1.3));
  415. gp[3].layoutYProperty().bind(
  416.         stage.heightProperty().subtract(
  417.                 scene.heightProperty().add(6.9)));
  418. //linha 2
  419.  
  420. gp[4].layoutXProperty().bind(scene.widthProperty().divide(6.7));
  421. gp[4].layoutYProperty().bind(scene.heightProperty().divide(2.5));
  422.  
  423. gp[5].layoutXProperty().bind(scene.widthProperty().divide(2.2));
  424. gp[5].layoutYProperty().bind(scene.heightProperty().divide(2.5));
  425.  
  426. gp[6].layoutXProperty().bind(scene.widthProperty().divide(1.3));
  427. gp[6].layoutYProperty().bind(scene.heightProperty().divide(2.5));
  428.  
  429. //linha 3
  430. gp[7].layoutXProperty().bind(scene.widthProperty().divide(6.7));
  431. gp[7].layoutYProperty().bind(scene.heightProperty().divide(1.4));
  432.  
  433. gp[8].layoutXProperty().bind(scene.widthProperty().divide(2.2));
  434. gp[8].layoutYProperty().bind(scene.heightProperty().divide(1.4));
  435.  
  436. gp[9].layoutXProperty().bind(scene.widthProperty().divide(1.3));
  437. gp[9].layoutYProperty().bind(scene.heightProperty().divide(1.4));
  438.  
  439. stage.show();
  440. }
  441.  
  442. public static void main(String[] args) {
  443. launch(args);
  444. }
  445.  
  446. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement