Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Problems with JavaFX viewport

  1. #1
    Junior Member
    Join Date
    Mar 2018
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Problems with JavaFX viewport

    Hi All,

    I'm having problems with two objects from the same image that are supposed to be viewable side-by-side with viewport constraint. The first image object shows but not the second.

    https://gist.github.com/anonymous/ee...a72e59487a5a5f

    There are no syntax errors and it compiles but it is a logic issue. gull image can be substituted for any picture as long as its in same folder as the script.

    P.S. the IDE above does not work for FX, it will have to be run in another IDE.
    Last edited by bebexx; March 17th, 2018 at 12:32 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problems with JavaFX viewport

    Also posted at: JavaFX Viewport Issues

    Try removing the unused code and also some of the minor methods. Pass two objects to the StackPane constructor.
    I did and was able to get the two images to display.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2018
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with JavaFX viewport

    https://gist.github.com/9080a576efc0...923f5374e193ca

    Hi I removed the second StackPane so that the two objects only run through the first. Commenting out the viewports resulted in one full image.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problems with JavaFX viewport

    Please post any code here wrapped in code tags that you want to talk about.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2018
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with JavaFX viewport

    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.geometry.Rectangle2D;
     
    public class PP4o11TEST extends Application {
     
     
        public void start(Stage primaryStage) 
        {
     
        Image img= new Image("gull.jpg");
        ImageView imgView = new ImageView(getClass().getResource("gull.jpg").toExternalForm());
     
        Image img2= new Image("gull.jpg");
        ImageView imgView2 = new ImageView(getClass().getResource("gull.jpg").toExternalForm());;
     
        imgView.setViewport(new Rectangle2D(200,80,70,60));
        imgView2.setViewport(new Rectangle2D(400,100,30,90));
     
        StackPane pane= new StackPane(imgView,imgView2);
        pane.setStyle("-fx-background-color:cornsilk");
     
        Scene scene= new Scene(pane, 500, 350);
     
        primaryStage.setTitle("Viewport Gull");
        primaryStage.setScene(scene);
        primaryStage.show();
        }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problems with JavaFX viewport

    What is the WxH of the gull.jpg image? Need that to consider the setViewport calls.

    Why are you using the StackPane class? What does the API doc say about what that class does?

    What is the purpose of the unused Image objects?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2018
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with JavaFX viewport

    The WxH is 400 x 265 px.

    StackPane determines the position of the children element...perhaps I should use a FlowPane instead
    https://docs.oracle.com/javafx/2/lay...s.htm#CHDGHCDG

    If you mean for the viewport with Rectangle 2D that was for the constraint. Otherwise it was initalized so that the objects could be referenced for ImageView

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problems with JavaFX viewport

    StackPane determines the position of the children element.
    Yes, but where does it position them?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2018
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with JavaFX viewport

    Hmm it says that it overlaps one each other like text placed over an image. But there is an alignment that can make it look like this https://docs.oracle.com/javafx/2/lay...hbox_stack.png

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problems with JavaFX viewport

    perhaps I should use a FlowPane
    Did you try that?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2018
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with JavaFX viewport

    Yes but I figured out it's the viewport that's causing the issue so I removed the second viewport. I'm curious as to how you've resolved it using Stackpane? The code already looks very close to completion

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problems with JavaFX viewport

    I used FlowPane to get side-by-side images. StackPane stacked the images on top of each other.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2018
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with JavaFX viewport

    Same, based on the description StackPane would be more suitable for text ontop of an object or image.

    I switched over to FlowPane and got rid of the second viewport. Works now with a Group and call to children objects.

Similar Threads

  1. JavaFX
    By bodylojohn in forum JavaFX
    Replies: 2
    Last Post: October 16th, 2017, 04:06 AM
  2. JavaFX Coding problems
    By dougie1809 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 16th, 2013, 12:47 PM
  3. javafx problem
    By darshan.bhogale in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 7th, 2013, 07:29 AM
  4. Questions about javafx
    By mDennis10 in forum Java Theory & Questions
    Replies: 3
    Last Post: September 5th, 2011, 12:19 AM
  5. [SOLVED] JavaFx
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 28th, 2009, 09:13 PM

Tags for this Thread