Click here to Skip to main content
15,942,801 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
Please tell me how to do that the image size was varied along with the size of the window. 


Java
package tictactoe;
         
        import javafx.application.Application;
        import javafx.stage.Stage;
        import javafx.scene.Scene;
        import javafx.fxml.FXMLLoader;
        import javafx.scene.Parent;
         
        /**
         *
         * @author user
         */
        public class TicTacToe extends Application {
            
            @Override
            public void start(Stage primaryStage /*stage*/) throws Exception {
                Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
                
                Scene scene = new Scene(root,300,320);
                scene.getStylesheets().add(getClass().getResource("MyCss.css").toExternalForm());
                primaryStage.setTitle("TicTacToe");
                primaryStage.setScene( scene);
                primaryStage.show();
              
            }
         
            /**
             * @param args the command line arguments
             */
            public static void main(String[] args) {
                launch(args);
            }
            
        }
         
        package tictactoe;
         
         
        import javafx.event.ActionEvent;
        import javafx.fxml.FXML;
        import javafx.collections.ObservableList;
        import javafx.scene.Node;
        import javafx.scene.control.Button;
        import javafx.scene.control.MenuItem;
        import javafx.scene.layout.GridPane;
         
        /**
         *
         * @author user
         */
        public class FXMLDocumentController {
          @FXML Button b1; 
           @FXML Button b2;
           @FXML Button b3;
           @FXML Button b4;
           @FXML Button b5;
           @FXML Button b6;
           @FXML Button b7;
           @FXML Button b8;
           @FXML Button b9;
           
           @FXML GridPane gameBoard;
            
           private boolean isFirstPlayer = true;
           
           public void buttonClickHandler(ActionEvent evt){
                 
                Button clickedButton = (Button) evt.getTarget();
                String buttonLabel = clickedButton.getText();
                
                if ("".equals(buttonLabel) && isFirstPlayer){
                    clickedButton.setText("X");
                    isFirstPlayer = false;
                } else if("".equals(buttonLabel) && !isFirstPlayer){
                    clickedButton.setText("O");
                    isFirstPlayer = true;
                }
                
                find3InARow();  // is there a winner?
           }    
           
           private boolean find3InARow(){
               //Row 1
               if (!"".equals(b1.getText()) && b1.getText().equals(b2.getText()) 
                   && b2.getText().equals(b3.getText())){
                   highlightWinningCombo(b1,b2,b3);
                   return true;
               }
               //Row 2
               if (!"".equals(b4.getText()) && b4.getText().equals(b5.getText()) 
                   && b5.getText().equals(b6.getText())){
                   highlightWinningCombo(b4,b5,b6);
                   return true;
               }
               //Row 3
               if (!"".equals(b7.getText()) && b7.getText().equals(b8.getText()) 
                   && b8.getText().equals(b9.getText())){
                   highlightWinningCombo(b7,b8,b9);
                   return true;
               }
               //Column 1
               if (!"".equals(b1.getText()) && b1.getText().equals(b4.getText()) 
                   && b4.getText().equals(b7.getText())){
                   highlightWinningCombo(b1,b4,b7);
                   return true;
               }
               //Column 2
               if (!"".equals(b2.getText()) && b2.getText().equals(b5.getText()) 
                   && b5.getText().equals(b8.getText())){
                   highlightWinningCombo(b2,b5,b8);
                   return true;
               }
               //Column 3
               if (!"".equals(b3.getText()) && b3.getText().equals(b6.getText()) 
                   && b6.getText().equals(b9.getText())){
                   highlightWinningCombo(b3,b6,b9);
                   return true;
               }
               //Diagonal 1
               if (!"".equals(b1.getText()) && b1.getText().equals(b5.getText()) 
                   && b5.getText().equals(b9.getText())){
                   highlightWinningCombo(b1,b5,b9);
                   return true;
               }
               //Diagonal 2
               if (!"".equals(b3.getText()) && b3.getText().equals(b5.getText()) 
                   && b5.getText().equals(b7.getText())){
                   highlightWinningCombo(b3,b5,b7);
                   return true;
               }       
               return false;
           }
           
           private void highlightWinningCombo(Button first, Button second, Button third){
               first.getStyleClass().add("winning-button");
               second.getStyleClass().add("winning-button");
               third.getStyleClass().add("winning-button");
         
           }
           
           public void menuClickHandler(ActionEvent evt){
                MenuItem clickedMenu = (MenuItem) evt.getTarget();
                String menuLabel = clickedMenu.getText();
                
                if ("Play".equals(menuLabel)){
                    ObservableList<Node> buttons = 
                            gameBoard.getChildren();
                    
                    buttons.forEach(btn -> {
                        ((Button) btn).setText("");
                         btn.getStyleClass().remove("winning-button");
                    });
                    
                    isFirstPlayer = true;
                }
           }  
         
        <?import java.lang.*?>
        <?import java.util.*?>
        <?import javafx.scene.*?>
        <?import javafx.scene.control.*?>
        <?import javafx.scene.layout.*?>
        <?import javafx.scene.layout.*?>
        <?import javafx.scene.layout.BorderPane?>
         
        <BorderPane xmlns="http://javafx.com/javafx/8" 
               xmlns:fx="http://javafx.com/fxml/1" 
               fx:controller="tictactoe.FXMLDocumentController">
           <top>
              <MenuBar BorderPane.alignment="CENTER">
                <menus>
                  <Menu text="Actions">
                    <items>
                      <MenuItem text="Play" 
                                />
                      <MenuItem text="Quit" />
                    </items>
                  </Menu>
                </menus>
              </MenuBar>
           </top>
           <center>
              <GridPane fx:id = "gameBoard" BorderPane.alignment="CENTER">
                 <children>
                    <Button fx:id="b1" />
                    <Button fx:id="b2" />
                    <Button fx:id="b3" />
                    <Button fx:id="b4" />
                    <Button fx:id="b5" />
                    <Button fx:id="b6" />
                    <Button fx:id="b7" />
                    <Button fx:id="b8" />
                    <Button fx:id="b9" />
                 </children>
              </GridPane>
           </center>
        </BorderPane>


What I have tried:

My image https://drive.google.com/file/d/0Bw2HP34V0kobUlhhNGpzMmFuTjg/view?usp=sharing
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900