1 Reply Latest reply: Mar 7, 2013 3:04 PM by David.Warwick RSS

    Can't get Rectangle to appear in scene

    David.Warwick
      Hello all. I am a JavaFX newbie. I am working on my first program. I am trying to get my rectangle to appear on the stage. I have scoured the web for examples and from what I have found, my code ought to work, but I am not seeing my Rectangle for some reason. My code does not have any syntax errors or exceptions. Please help if you can.
      import javafx.scene.shape.*;
      import java.util.logging.Level;
      import java.util.logging.Logger;
      import javafx.application.Application;
      import javafx.event.EventHandler;
      import javafx.fxml.FXMLLoader;
      import javafx.scene.*;
      import javafx.scene.image.ImageView;
      import javafx.scene.input.MouseEvent;
      import javafx.scene.layout.AnchorPane;
      import javafx.scene.paint.Color;
      import javafx.stage.Stage;
      
      ...
      
      public class Main extends Application {
      
          /**
           * @param args the command line arguments
           */
          public void main(String[] args) {
              Application.launch(Main.class, (java.lang.String[])null);
          }
      
          @Override
          public void start(Stage primaryStage) {
              
              
              try {
                  
                  
                  AnchorPane page = (AnchorPane) FXMLLoader.load(Main.class.getResource("TicTacToe.fxml"));
                  Scene scene = new Scene(page);
                  
                  primaryStage.setScene(scene);
                  scene.getStylesheets().add("tictactoe/tictactoe.css");
                  primaryStage.setTitle("TicTacToe");
      ...
      
      Group rectGroup=new Group();
                  final Rectangle rlt=new Rectangle(); //Rectangle Left Top
                  rlt.setX(107);
                  rlt.setY(35);
                  rlt.setHeight(150);
                  rlt.setWidth(150);
                  rlt.setFill(Color.GREY);
                  rlt.fillProperty();
                  rectGroup.getChildren().add(rlt);
                  rectGroup.setVisible(true);
                  primaryStage.show();      
                         
              
              rlt.setOnMouseClicked(new EventHandler<MouseEvent>()
              {
                  @Override
                  public void handle(MouseEvent t) {
                      rlt.setFill(Color.RED);
                  }
              });
      I have an AnchorPae with tic-tac-toe hash marks as my stage and I put a Rectangle in the upper-left corner of the tic-tac-toe grid. I am doing this because I want to detect when someone clicks in the upper left corner of the grid, an X or an O jpg should appear. Right now, I am just trying to get a Grey Rectangle to appear and when it is clicked, it should change to Red. Eventually, when I get the Rectangle set-up properly and it will respond to clicks, the 9 Rectangles will be invisible.

      Also, is there a better way to determine if someone clicks in a particular grid area on the AnchorPane?

      Thanks,

      David
        • 1. Re: Can't get Rectangle to appear in scene
          David.Warwick
          Never mind. I figured it out. Here is the code that worked. First of all, I didn't need to use Rectangles.
          package tictactoe;
          
          
          import java.util.logging.Level;
          import java.util.logging.Logger;
          import javafx.application.Application;
          import javafx.event.EventHandler;
          import javafx.fxml.FXMLLoader;
          import javafx.scene.*;
          import javafx.scene.image.Image;
          import javafx.scene.image.ImageView;
          import javafx.scene.input.MouseEvent;
          import javafx.scene.layout.AnchorPane;
          import javafx.stage.Stage;
          
          /**
           *
           * @author David
           */
          public class Main extends Application {
          
              /**
               * @param args the command line arguments
               */
              public void main(String[] args) {
                  Application.launch(Main.class, (java.lang.String[])null);
              }
          
              @Override
              public void start(Stage primaryStage) {
                  
                  
                  try {
                                  
                     
                      final AnchorPane page = (AnchorPane) FXMLLoader.load(Main.class.getResource("TicTacToe.fxml"));
                      Scene scene = new Scene(page);
                      
                      primaryStage.setScene(scene);
                      scene.getStylesheets().add("tictactoe/tictactoe.css");
                      primaryStage.setTitle("TicTacToe");
                      final ImageView mm =new ImageView();//Middle Middle
                      final ImageView lt =new ImageView();//Left Top
                      final ImageView lm =new ImageView();//Left Middle
                      final ImageView lb =new ImageView();//Left Bottom
                      final ImageView tm =new ImageView();//Top Middle
                      final ImageView bm =new ImageView();//Bottom Middle
                      final ImageView rt =new ImageView();//Right Top
                      final ImageView rm =new ImageView();//Right Middle
                      ImageView rb =new ImageView();//Right Bottom
                      mm.setImage(new Image("tictactoe/images/x.jpg"));
                     
                      mm.relocate(246,145);
                      lt.relocate(107,35);
                      lm.relocate(107,145);
                      lb.relocate(107,260);
                      tm.relocate(246,35);
                      bm.relocate(246,260);
                      rt.relocate(375,35);
                      rm.relocate(375,145);
                      rb.relocate(375,260);
                      
                      page.getChildren().add(mm);
                      //page.getChildren().remove(mm);
                      page.getChildren().add(lt);
                      page.getChildren().add(lm);
                      page.getChildren().add(lb);
                      page.getChildren().add(tm);
                      page.getChildren().add(bm);
                      page.getChildren().add(rt);
                      page.getChildren().add(rm);
                      page.getChildren().add(rb);
                  
                      primaryStage.show();    
                      
                mm.setOnMouseClicked(new EventHandler<MouseEvent>()
                  {
                      @Override
                      public void handle(MouseEvent t) {
                         page.getChildren().remove(mm);
                      }
                  }); 
                  }
                  catch (Exception ex) {
                      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                              }
                      
              
              }
          }
          So now, when I click the mm (middle-middle) x.jpg, it disappears.

          Thanks,

          David

          Edited by: David.Warwick on Mar 7, 2013 1:04 PM