This discussion is archived
0 Replies Latest reply: Mar 9, 2013 6:35 PM by KonradZuse RSS

Why does my rectangle/cube get stuck?

KonradZuse Explorer
Currently Being Moderated
I found some code which I incorporated into my code and it works for the most part except it seems I'm stick in a certain area. I create a rectangle, add it to a group, then the group to a scene. I tried adjusting the scene and group size, but no differences.... I also tried setting the actions from "root" to "scene" but no luck...
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.PointLight;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;

/**
 *
 * @author Konrad
 */
public class draw extends Application
{
    double x,y, mousex,mousey;
    Rectangle r = new Rectangle(100,100,100,100);
        private PerspectiveCamera addCamera(Scene scene)
        {
        PerspectiveCamera perspectiveCamera = new PerspectiveCamera(false);
        scene.setCamera(perspectiveCamera);
        return perspectiveCamera;
    }
        
        
        
    
    @Override
    public void start(Stage primaryStage)
    { 

         Group root = new Group(r);
 
root.prefHeight(1000);
root.prefWidth(1000);

                      final Scene scene = new Scene(root, 5000, 5000, true);
        root.setOnMousePressed((MouseEvent event) -> 
        {
             if(r.contains(event.getX(), event.getY()))
            {
             mousex = event.getX();

               mousey= event.getY();

               //get the x and y position measure from Left-Top

               x = r.getLayoutX();

               y = r.getLayoutY();
            }
        });
 
        root.setOnMouseDragged((MouseEvent event) -> 
          {
            if(r.contains(event.getX(), event.getY()))
            {
                //Get the exact moved X and Y

                x += event.getX()-mousex ;

                y += event.getY()-mousey ;

 

                //set the positon of Node after calculation

              r.setLayoutX(x);

              r.setLayoutY(y);

 

                //again set current Mouse x AND y position

              mousex = event.getX();

                mousey= event.getY();

            }
        });
        
        PointLight light = new PointLight();
        
        light.setTranslateX(scene.getWidth()/2);
        light.setTranslateY(scene.getHeight()/2);
        root.getChildren().add(light);
        addCamera(scene);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.setProperty("prism.dirtyopts", "false");
        launch(args);
    }
}

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points