Results 1 to 5 of 5
  1. #1
    TomTom1 is offline Member
    Join Date
    Jul 2016
    Posts
    36
    Rep Power
    0

    Default Approach to create a Speedometer

    Hey guys,

    I need a speedometer in my JavaFX applicatio,n and was wondering what kind of approaches there are to do this.
    I'm using Scene Builder, and what I've done so far is simply using two ImageViews, one for the background, and one for the speedometer's needle.
    With a button click I rotate the ImageView with the Needle in 1°:
    Java Code:
    Imageview.getTransforms().add(new Rotate(1, Imageview.getFitWidth(), Imageview.getFitHeight()/2));
    The result looks like this:


    Are there any better approaches to do this? I could imagine that rotating an ImageView is not the best when it comes to resources?

    One more question: When i wrap a for loop around my code, to lets say move the Needle from 0° to 180° the for loop is somehow not entered?
    Java Code:
        @FXML
        public void handleButtonAction() {
         for (int i=0; i==180;i++){
             System.out.println("test");
             Imageview.getTransforms().add(new Rotate(1, Imageview.getFitWidth(), Imageview.getFitHeight()/2));
         }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Approach to create a Speedometer

    the for loop is somehow not entered?
    Look at the loop terminate condition. The loop will execute while the condition is true. Is i==180 true when the loop starts with i = 0?
    Change the == operator to one that will be true until you want the looping to end. For example < or <=.

    Note: The loop will "instantly" execute all the steps, not slow enough for you to see anything in the GUI.
    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
    399
    Rep Power
    6

    Default Re: Approach to create a Speedometer

    For the needle I would create a triangle node and rotate it around the anchor point. I would not expect the rotation to take up to too much resources since it is a one time movement. Also think about an animation solution with multiple degree rotations rather than a for loop.
    Concerning the loop: first thing I would check was whether the handleButtonAction(){} method is called at all by moving the sysout statement in front of the loop. If it is not, make sure the fxml code for the Button has a statement like
    Java Code:
    onAction="#handleButtonAction"
    Then I would check whether there is a bug in your loop ... "loop for i starting at 0 as along as i equals 180" ...
    Last edited by benji2505; 08-11-2016 at 03:04 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Approach to create a Speedometer

    I made some widgets like this some years ago. I drew everything with basic Swing graphics classes. For the needle, I simply
    used a filled polygon object and rotated it around the pivot point. I started by rotating the three points in the polygon (it was a
    narrow triangle) and then simple redisplayed it. Or you can rotate the graphics context (I presume you can still do that in JavaFX).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    TomTom1 is offline Member
    Join Date
    Jul 2016
    Posts
    36
    Rep Power
    0

    Default Re: Approach to create a Speedometer

    Thanks for your answers,

    The problem was indeed a stupid mistake with the terminating condition.
    For now I just keep the method with two images, which looks really good.

Similar Threads

  1. how to approach sjcp?
    By Narasimhan in forum OCPJP
    Replies: 1
    Last Post: 10-21-2012, 08:52 AM
  2. Graphic Approach
    By kammce in forum Java Gaming
    Replies: 3
    Last Post: 10-09-2012, 02:01 PM
  3. How to approach this problem?
    By Zigster in forum New To Java
    Replies: 16
    Last Post: 04-27-2012, 05:53 PM
  4. Regex approach
    By karlito in forum Lucene
    Replies: 1
    Last Post: 11-04-2009, 05:53 PM
  5. Best Jsp book with practical approach
    By Saurabh321 in forum New To Java
    Replies: 0
    Last Post: 02-05-2008, 08:12 AM

Posting Permissions

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