Results 1 to 6 of 6

Thread: Gui con JavaFX

  1. #1
    Julik is offline Member
    Join Date
    Nov 2016
    Posts
    2
    Rep Power
    0

    Default Gui con JavaFX

    Hi everybody here) I have some problem with my GUI in javafx and the problem is that I can't shade the scene which contains a moveble
    image. I mean when the image moves the shade of scene must moves as well this image, this link will explain better what i want: http://i.imgur.com/PYn2Nsj.gif
    I have alredy tried to create an AnchorPane and set a radial gradient to it but when i move image(throw arrows) it remains centered in the same point
    like this:Gui con JavaFX-screenshot_1.png How can solve this issue, if anybody here has some idea please shae it here with me) Thanks.
    Last edited by Julik; 11-26-2016 at 11:34 PM. Reason: Javafx,shade

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,963
    Rep Power
    31

    Default Re: Gui con JavaFX

    Please post in English.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Gui con JavaFX

    Could you post the relevant code snippet that you already have ?

  4. #4
    Julik is offline Member
    Join Date
    Nov 2016
    Posts
    2
    Rep Power
    0

    Default Re: Gui con JavaFX

    public class GameField extends Stage{


    private Scene scene;
    private Pane rootPane;
    private ImageView dot;
    private Pane shade;

    private static final GameField instance = new GameField();

    public static GameField getInstance(){
    return instance;
    }

    private GameField(){
    setTitle("Maze game | "+System.getProperty("os.name"));
    initScene();
    initDot();
    rootPane.getChildren().add(dot);

    }

    private void initRootPane(){
    rootPane = new Pane();

    }
    private void initScene(){
    initRootPane();
    scene = new Scene(rootPane,500,500);
    scene.setOnKeyPressed(new EventHandler<KeyEvent>(){
    @Override
    public void handle(KeyEvent event) {
    switch (event.getCode()) {
    case UP: dot.setY(dot.getY()-2);
    break;
    case RIGHT: dot.setX(dot.getX()+2);
    break;
    case DOWN: dot.setY(dot.getY()+2);
    break;
    case LEFT: dot.setX(dot.getX()-2);
    break;
    }
    }

    });
    makeShadeable();
    setScene(scene);

    }

    private void initDot(){
    dot = new ImageView(new Image("file:imgs/dot.png"));
    dot.setX(0);
    dot.setY(0);
    }
    private void makeShadeable() {
    shade = new Pane();
    shade.setPrefSize(rootPane.getWidth(),rootPane.get Height());
    RadialGradient shadePaint = new RadialGradient(
    10, 0, 0,0, 0.4, true, CycleMethod.NO_CYCLE,
    new Stop(1, Color.BLACK),
    new Stop(0, Color.TRANSPARENT)
    );


    shade.setBackground(
    new Background(
    new BackgroundFill(
    shadePaint, null, new Insets(-10)
    )
    )
    );
    rootPane.getChildren().add(shade);
    }
    }


    Here is my code and this is what i have: Gui con JavaFX-screenshot_2.png As you can see when i move image the center of gradient still in the same position and don't moves with image(
    Instead I wanted this behavior : http://i.imgur.com/PYn2Nsj.gif
    I have already tried to update the gradient's center position when image moves but it dosen't work(
    Last edited by Julik; 11-27-2016 at 11:53 AM.

  5. #5
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Gui con JavaFX

    The third and the fourth parameter in the RadialGradient Constructor determine the center of the shade. If the 6th parameter is set to true, the values are proportional to the shape the container fills, (in your case the pane that covers the scene) so you would have to enter 0.5 for 50% of the width, 0.25 for a quarter of the width, etc.
    Moving the center of an existing RadialGradient instance is not possible to my knowledge, but you could remove the existing and create a new RadialGradiant with new coordinates and set that as background.

  6. #6
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Gui con JavaFX

    Sometimes I have the feeling I have to proof to myself that can still do it-LOL. So, this is what I came up with.

    Java FX Code:
    package ShadeTest;
    
    import javafx.application.Application;
    import javafx.event.EventHandler;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.input.KeyCode;
    import javafx.scene.input.KeyEvent;
    import javafx.scene.layout.Background;
    import javafx.scene.layout.BackgroundFill;
    import javafx.scene.layout.Pane;
    import javafx.scene.layout.StackPane;
    import javafx.scene.paint.Color;
    import javafx.scene.paint.CycleMethod;
    import javafx.scene.paint.RadialGradient;
    import javafx.scene.paint.Stop;
    import javafx.stage.Stage;
    
    public class ShadedScene extends Application {
    
    	Scene scene;
    	StackPane layout;
    	Double xPosition = 0.5;
    	Double yPosition = 0.6;
    	RadialGradient rg1;
    
    	@Override
    	public void start(Stage PrimaryStage) {
    		layout = new StackPane();
    		layout.setPrefSize(500, 500);
    		scene = new Scene(layout);
    		createShade();
    		keyEventHandling();
    		PrimaryStage.setScene(scene);
    		PrimaryStage.show();
    	}
    
    	private void createShade() {
    
    		Pane root = (Pane) scene.getRoot();
    		Pane shade = new Pane();
    
    		rg1 = new RadialGradient(0, 0, xPosition, yPosition, 0.5, true, CycleMethod.NO_CYCLE, new Stop(1, Color.BLACK),
    				new Stop(0, Color.TRANSPARENT));
    
    		shade.setBackground(new Background(new BackgroundFill(rg1, null, new Insets(0))));
    		if (root.getChildren().isEmpty()) {
    			root.getChildren().add(shade);
    		} else {
    			root.getChildren().clear();
    			root.getChildren().add(shade);
    		}
    	}
    
    	private void keyEventHandling() {
    		Pane root = (Pane) scene.getRoot();
    		scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
    			public void handle(KeyEvent event) {
    				KeyCode key = event.getCode();
    				if (key == KeyCode.DOWN) {
    					yPosition = yPosition + 0.2;
    					createShade();
    				}
    				if (key == KeyCode.UP) {
    					yPosition = yPosition - 0.2;
    					createShade();
    				}
    				if (key == KeyCode.RIGHT) {
    					xPosition = xPosition + 0.2;
    					createShade();
    				}
    				if (key == KeyCode.LEFT) {
    					xPosition = xPosition - 0.2;
    					createShade();
    				}
    			}
    		});
    
    	}
    
    	public static void main(String[] args) {
    		launch(args);
    	}
    }

Similar Threads

  1. JavaFX
    By bodylojohn in forum New To Java
    Replies: 1
    Last Post: 02-14-2014, 06:11 PM
  2. Need Help with JavaFX
    By incredibleX in forum New To Java
    Replies: 4
    Last Post: 01-22-2014, 05:07 PM
  3. JavaFX Book | Learn JavaFX More In Depth
    By SnakeDoc in forum JavaFX
    Replies: 0
    Last Post: 05-17-2013, 11:10 PM
  4. Help with javaFX?!
    By JavaNoob!1 in forum JavaFX
    Replies: 7
    Last Post: 07-18-2012, 10:56 PM
  5. JavaFX Script and JavaFX Mobile
    By levent in forum Java Software
    Replies: 1
    Last Post: 01-27-2010, 04:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •