Jump to content

Java Help

DevilKrugeer

Hey Everyone,

 

As the title says, I need some help with the code for my java class. I have to add a mouse click function that will change the image that is moving around the application. I have placed the function, but its not working. I'm not sure what to do, I tried following examples from class, but still doesn't work. Any help is appreciated.

 

for some reason im unable to upload the images to the forum, i believe it's because of the format .gif.

 

 

thank you.

 

 

	 
	import javafx.application.Application;
	import javafx.stage.Stage;
	import java.awt.event.MouseListener;
	import javafx.animation.AnimationTimer;
	import javafx.animation.KeyFrame;
	import javafx.animation.KeyValue;
	import javafx.animation.Timeline;
	import javafx.event.ActionEvent;
	import javafx.event.EventHandler;
	import javafx.geometry.Rectangle2D;
	import javafx.scene.Group;
	import javafx.scene.Scene;
	import javafx.scene.effect.Lighting;
	import javafx.scene.image.Image;
	import javafx.scene.image.ImageView;
	import javafx.scene.input.MouseEvent;
	import javafx.scene.layout.StackPane;
	import javafx.scene.paint.Color;
	import javafx.scene.shape.Circle;
	import javafx.scene.shape.Rectangle;
	import javafx.scene.text.Text;
	import javafx.util.Duration;
	 
	public class ReboundImageOriginal extends Application {
	    
	    //main timeline
	    private AnimationTimer timer;
	    private ImageView image;
	    private ImageView image2;
	    private ImageView imageTemp;
	    private int x,y,moveX,moveY;
	    final int HEIGHT = 300;
	    final int WIDTH = 500;
	    final int IMAGE_SIZE = 35;
	 
	    //variable for storing actual frame
	    private Integer i=0;
	    
	    
	   
	 
	    @Override public void start(Stage stage) {
	        Group p = new Group();
	        Scene scene = new Scene(p);
	        stage.setScene(scene);
	        stage.setWidth(550);
	        stage.setHeight(400);
	        p.setTranslateX(0);
	        p.setTranslateY(20);
	        x = 0;
	        y = 0;
	        moveX=moveY=3;
	        //create an ImageView object
	        Rectangle rectangle = new Rectangle(0,0,550,350);
	        image = new ImageView(new Image("happyFace.gif"));
	        image2 = new ImageView (new Image("redSmiley.gif"));
	        imageTemp = new ImageView();
	        //Imagine Change 
	       rectangle.setOnMouseClicked(new EventHandler<MouseEvent>()
	       {
	        @Override
	        public void handle(MouseEvent event) {
	            System.out.println("shit works");
	            
	                imageTemp = image;
	                image = image2;
	                image2 = imageTemp;
	             
	        }
	           
	       });
	        //create a layout for the image
	        final StackPane stack = new StackPane();
	        stack.getChildren().addAll(image);
	        stack.setLayoutX(30);
	        stack.setLayoutY(30);
	        p.getChildren().addAll(rectangle);
	        p.getChildren().add(stack);
	        stage.show();
	 
	        //You can add a specific action when each frame is started.
	        timer = new AnimationTimer() {
	            @Override
	            public void handle(long l) {
	                x += moveX;
	                y += moveY;
	                
	                if (x <= 0 || x >= WIDTH)
	                    moveX = moveX * -1;
	                 if (y <= 0 || y >= HEIGHT)
	                    moveY = moveY * -1;
	                stack.setLayoutX(x);
	                stack.setLayoutY(y);
	            }
	 
	        };
	        
	        timer.start();
	    }
	        
	        
	    public static void main(String[] args) {
	        Application.launch(args);
	    }
	  } 
	

Link to comment
Share on other sites

Link to post
Share on other sites

What is the expected behavior of your application and what is the actual behavior you observe?

 

When you click the button, "Mouse pressed" is printed in the console. Is this not working for you?

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, PlutoNZL said:

What is the expected behavior of your application and what is the actual behavior you observe?

 

When you click the button, "Mouse pressed" is printed in the console. Is this not working for you?

the Imagine is suppose to change, when I click it, nothing happens. So I'm not 100% sure of what could be. It does work when i have something to print out into the console.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, DevilKrugeer said:

the Imagine is suppose to change, when I click it, nothing happens. So I'm not 100% sure of what could be. It does work when i have something to print out into the console.

What image? Your code does not reference any image. All your on pressed method does is print "Mouse pressed" to the console, it makes no attempt to modify an image.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, PlutoNZL said:

What image? Your code does not reference any image. All your on pressed method does is print "Mouse pressed" to the console, it makes no attempt to modify an image.

this is awkward i have placed the example that i had, instead of assignment i need it help with it.

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, DevilKrugeer said:

this is awkward i have placed the example that i had, instead of assignment i need it help with it.

I'm going to try and illustrate your issue with a small example.

class App {
    
    static String s;
    
    public static void main(String ...args) {
        // Right now the variable "s" is not point to any memory location
        
        // myStringOne is equal to a memory location (lets say X). X contains the string "Hello"
        String myStringOne = "Hello";
        
        // myStringTwo is equal to a memory location Y. Y contains the string "World"
        String myStringTwo = "World";
        
        // s is now equal to X.
        s = myStringOne;
        
        // "Hello" is printed because that is what the memory location X contains
        System.out.println(s);

        // temp now points to memory location X
        String temp = myStringOne;
        
        // myStringOne now points to memory location Y
        myStringOne = myStringTwo;
        
        // myStringTwo now points to memory location X
        myStringTwo = temp;
        
        // "Hello" is still printed because s still points to memory location X and the value in X has not changed from "Hello"
        System.out.println(s);
    }
}

I recommend you go read these two very short articles:
http://www.javaworld.com/article/2077424/learn-java/does-java-pass-by-reference-or-pass-by-value.html

http://javadude.com/articles/passbyvalue.htm

 

You can resolve your issue by replacing:

imageTemp = image;
image = image2;
image2 = imageTemp;

With:

imageTemp.setImage(image.getImage());
image.setImage(image2.getImage());
image2.setImage(imageTemp.getImage());

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×