Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree1Likes

Thread: If picture 'touch' picture, do action

  1. #1
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default If picture 'touch' picture, do action

    Java FX Code:
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import javafx.application.Application;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.stage.Stage;
    
    public class Testivity extends Application {
    	
    	Stage stage;
    	Scene scene1;
    	
    	Sources source = new Sources ();
    	
    	 public static void main(String[] args) {
    	        launch(args);
    	    }
    	
    	 public void start(Stage primaryStage) throws FileNotFoundException {
    		
    			   Group root = new Group ();
    		        
    			   Image picture = new Image( new FileInputStream ("ball.png") );	
    			   ImageView ball = new ImageView (picture);
    			   
    			   Image stone = new Image( new FileInputStream ("stone.png") );	
    			   ImageView obstacle = new ImageView (stone);
    			   
    			   
    			   source.obstacle (obstacle);
    			   
    			   source.startLocation (ball);
    			   source.doAction(ball);
    			    
    			    
    			   
    			    root.getChildren().addAll (obstacle,ball);
    			    primaryStage.setTitle("Moves");
    		        
    			    scene1 = new Scene(root,1200,800);
    		        stage = primaryStage;
    		        primaryStage.setScene(scene1);
    		        stage.show();
    		        
    	 
    	 } 
    	 
    }
    
    
    import java.util.Random;
    
    import javafx.animation.TranslateTransition;
    import javafx.scene.image.ImageView;
    import javafx.util.Duration;
    
    class Sources {
    	
    	 void startLocation (ImageView ball) 
    	 {
    		   ball.setTranslateX (100);
    		   ball.setTranslateY (100);
    		   ball.setFitHeight (100);
    		   ball.setFitWidth (140); 
    	 }
    	 
    	 
    	 void doAction (ImageView ball) 
    	 {
    		 TranslateTransition transition = new TranslateTransition();
    			transition.setDuration(Duration.millis(5000));
    			transition.setNode(ball);
    		 
    		 	Random random = new Random ();
    		 	
    		 	int tranX = 200 + random.nextInt (900);
    			int tranY = 50 + random.nextInt (300); 	
    		 	   
    			    transition.setByX(tranX); 
    			 	transition.setByY(tranY);  
    			 	transition.setAutoReverse(false);
    			 	transition.play();
    			 	
    			    transition.play();
    	 }
    	
    	 
    	 void obstacle (ImageView obstacle) 
    	 {
    		   obstacle.setTranslateX (660);
    		   obstacle.setTranslateY (200);
    		   obstacle.setFitHeight (200);
    		   obstacle.setFitWidth (140); 
    	 }
    	 
    	 
    	 
    }
    Every time program runs, ball have diffrend moving direction every time. Sometimes ball cross the stone.
    The dillema is that ball actually ignores stone, because I dont know what method to write.



    I want to write a code:
    Java FX Code:
    if  (  ball's atleast 1pixel touches stone ) 
    {  ball stops. }
    Perhaps there is something like coordinate class? who recognizes imageView coordinates? Any suggestions?
    Last edited by asdfg; 02-05-2017 at 08:13 PM.

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

    Default Re: If picture 'touch' picture, do action

    Collision detection can be done in a variety of ways. For regular shapes you usually use the center of the node plus the dimension and then compare it to the edge of the other node/frame/etc.
    For irregular shapes you can use the intersect() method. It returns the intersecting shape of 2 nodes/sprites. If there is no intersecting shape the width or height values of the resulting shape returns -1. You can use that info to determine whether there was a collision.
    In the case of collision you have to reverse the x- and y- directions of the movement, this way the ball will not leave the scene.
    asdfg likes this.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,225
    Rep Power
    13

    Default Re: If picture 'touch' picture, do action

    Well, I am not that familiar with JavaFX but the Node class seems to treat intersecting like Java. It just returns a boolean if the bounding rectangles overlap. Real collision is simple for circles if you write your own method. But for irregular shapes (think a 5 pointed star) where parts can actually lie within a bounding box but not touch is more difficult. I would think that to really achieve true collision, the outline of the images would need to be obtained (perhaps via the Shape interface) and then writing some methods to actually check perimeter overlap.

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

  4. #4
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default Re: If picture 'touch' picture, do action

    I didint meant that difficult.
    Lets say ball and that stone is a circles. ( Idea is that it is not default geometry figures, but pictures )
    I want to apply action if ball moves into stone atleast 5 pixels.

    Few examples was really difficult to understand, not to mention they writed for swing.

    This code is marked as syntax mistake:
    Java FX Code:
    if (ball.intersects(obstacle))
    		    {  }
    Last edited by asdfg; 02-06-2017 at 01:14 AM.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,225
    Rep Power
    13

    Default Re: If picture 'touch' picture, do action

    The problem is that you're not really checking the intersection of two obstacles. You are checking the intersection of their enclosing rectangles. So depending on the angle of approach, it could be exact or it could be off by a significant amount. But it is your game so you can be the judge of whether it is good enough.

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

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

    Default Re: If picture 'touch' picture, do action

    Quote Originally Posted by asdfg View Post
    Few examples was really difficult to understand, not to mention they writed for swing.

    This code is marked as syntax mistake:
    Java FX Code:
    if (ball.intersects(obstacle))
    		    {  }
    The intersect() method returns a new shape, which is the intersection of shape 1 and shape 2. If the width of the new shape returns -1 you know there is no collision. But this is only one out of many ways to do collision testing.

    If you google "javafx collision testing" you will find a bunch of examples. Looking at swing examples only makes sense if you want to understand collision testing.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,225
    Rep Power
    13

    Default Re: If picture 'touch' picture, do action

    Yeah. It looks like JavaFX provides better tests for intersection than javax.

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

  8. #8
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default Re: If picture 'touch' picture, do action

    Java FX Code:
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import javafx.application.Application;
    import javafx.geometry.Bounds;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.stage.Stage;
    
    public class Testivity extends Application {
    	
    	Stage stage;
    	Scene scene1;
    	
    	Sources source = new Sources ();
    	
    	 public static void main(String[] args) {
    	        launch(args);
    	    }
    	
    	 public void start(Stage primaryStage) throws FileNotFoundException {
    		
    			   Group root = new Group ();
    		        
    			   Image picture = new Image( new FileInputStream ("ball.png") );	
    			   ImageView ball = new ImageView (picture);
    			   
    			   Image stone = new Image( new FileInputStream ("stone.png") );	
    			   ImageView obstacle = new ImageView (stone);
    			   
    			   
    			   source.obstacle (obstacle);
    			   
    			   source.startLocation (ball);
    			   source.doAction(ball);
    			    
    			
    			    Bounds objA = ball.localToScene(ball.getBoundsInLocal());
    		        Bounds objB = obstacle.localToScene(obstacle.getBoundsInLocal());
    			   
    		        if (objA.intersects(objB)) 
    		          System.out.println ( "ball encounter stone");
    			   
    			   
    			    root.getChildren().addAll (obstacle,ball);
    			    primaryStage.setTitle("Moves");
    		        
    			    scene1 = new Scene(root,1200,800);
    		        stage = primaryStage;
    		        primaryStage.setScene(scene1);
    		        stage.show();
    		        
    	 
    	 } 
    	 
    }
    Doesnt work.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: If picture 'touch' picture, do action

    Are those actually added to the scene?
    And shouldn't the bounds stuff be done after the objects have been added?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default Re: If picture 'touch' picture, do action

    I have moved these to 32th line.
    Java FX Code:
     Bounds objA = ball.localToScene(ball.getBoundsInLocal());
    		  Bounds objB = obstacle.localToScene(obstacle.getBoundsInLocal());
    And this actions happens EVERY time I run a program.
    Java FX Code:
     System.out.println ( "ball encounter stone");
    And it is the problem, that program does that action, even if ball do not reach stone.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,225
    Rep Power
    13

    Default Re: If picture 'touch' picture, do action

    Quote Originally Posted by asdfg View Post
    And it is the problem, that program does that action, even if ball do not reach stone.
    Well, this might be related to what I was talking about. You are checking for collisions of the bounding rectangle, not the image. I would think you need to get a Shape of the image to detect collisions, as benji2505 said.

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

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

    Default Re: If picture 'touch' picture, do action

    How many pixels do the images have (right click on file and check)? How far apart are the start locations? No movement in your code?

  13. #13
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default Re: If picture 'touch' picture, do action

    Quote Originally Posted by benji2505 View Post
    How many pixels do the images have (right click on file and check)? How far apart are the start locations? No movement in your code?
    'ball' have 960x678. 'stone' have 580x482. They both size was modified with:
    .setFitHeight (100);
    .setFitWidth (140);

    'ball' begins at:
    ball.setTranslateX (100);
    ball.setTranslateY (100);

    Movement happens as soon as program is runned.

    Java FX Code:
    void doAction (ImageView ball) 
    	 {
    		 TranslateTransition transition = new TranslateTransition();
    			transition.setDuration(Duration.millis(5000));
    			transition.setNode(ball);
    		 
    		 	Random random = new Random ();
    		 	
    		 	int tranX = 200 + random.nextInt (900);
    			int tranY = 50 + random.nextInt (300); 	
    		 	   
    			    transition.setByX(tranX); 
    			 	transition.setByY(tranY);  
    			 	transition.setAutoReverse(false);
    			 	transition.play();
    			 	
    			    transition.play();
    	 }

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

    Default Re: If picture 'touch' picture, do action

    Where is the new location of the ball available as the ball moves so that collision can be detected when it happens?
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default Re: If picture 'touch' picture, do action

    Quote Originally Posted by Norm View Post
    Where is the new location of the ball available as the ball moves so that collision can be detected when it happens?
    It is random, but most of time - 'ball' collides with 'stone'.

    int tranX = 200 + random.nextInt (900);
    int tranY = 50 + random.nextInt (300);

    so that collision can be detected when it happens?
    I dont understand how this works.

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

    Default Re: If picture 'touch' picture, do action

    I dont understand how this works.
    I think you need to understand that to be able to detect the collision.
    Is there something like a listener that will be called each time the ball is moved?
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default Re: If picture 'touch' picture, do action

    Quote Originally Posted by Norm View Post
    I think you need to understand that to be able to detect the collision.
    Is there something like a listener that will be called each time the ball is moved?
    I think not. Code is quite primitive. I posted at #1

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

    Default Re: If picture 'touch' picture, do action

    If you can not have code that is called each time there is a change in the ball's position, I do not think you will be able to detect a collision when it happens.

    Code is quite primitive.
    I don't think so. The classes and methods used are very high level and do not appear to reveal the details of the animation.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: If picture 'touch' picture, do action

    Norm is right. The TranslateTransition is a subclass of Animation and is not really meant for this kind of application. It is more for letting things disappear out of the window or similar fancy graphic gismos. Binding a Listener to this movement is doable, but might be more than you asked for.

    Here is what i would suggest: throw the doAction() method in post #1 overboard and replace it with a regular KeyFrame animation. That Keyframe fires an event every cycle period and each event executes something, for example some methods that you might consider useful. Within these (movement ?) methods you then check whether you have collided with the scene edge or another object. Here is a little start:

    Java FX Code:
    private Timeline timeline = new Timeline();
    
    ...
    
    private void funAnimation() {      // maybe this should be publicly available depending on where you put it
    
    		KeyFrame frame = new KeyFrame(Duration.seconds(0.016), event -> {
    			ballMoveX();   // here is where your movement and collision magic should happen for x
    			ballMoveY();
    			});
    
    		timeline.setCycleCount(Timeline.INDEFINITE);
    		timeline.getKeyFrames().add(frame);
    		timeline.play();   // .stop stops it again
    	}
    Last edited by benji2505; 02-07-2017 at 02:06 AM.

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

    Default Re: If picture 'touch' picture, do action

    Quote Originally Posted by asdfg View Post
    It is random, but most of time - 'ball' collides with 'stone'.

    int tranX = 200 + random.nextInt (900);
    int tranY = 50 + random.nextInt (300);


    I dont understand how this works.
    - sorry for my oversight, there is movement in the code, no doubt.

    TranslateTransition moves an object from the original position to the specified location within the given timeframe. If you set autoreverse to false it stays there, otherwise it returns to the original position (think the front led in David Hasselhoff's knight-rider, gosh I am old). You set the target locations in the above code, but that does not mean that this target changes later.
    Last edited by benji2505; 02-07-2017 at 02:36 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 05-24-2012, 04:44 PM
  2. Picture comparing
    By xrust01 in forum Java Applets
    Replies: 0
    Last Post: 02-15-2011, 08:26 PM
  3. Getting a picture out of a jar?
    By Zman3359 in forum New To Java
    Replies: 4
    Last Post: 02-02-2011, 11:55 PM
  4. picture backround
    By safiya in forum NetBeans
    Replies: 1
    Last Post: 10-18-2008, 07:13 AM
  5. Picture help
    By deathnote11 in forum AWT / Swing
    Replies: 13
    Last Post: 06-03-2008, 05:46 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
  •