1

I have to create a very basic GPS for a project. In my interface, I have an animation of a car that moves from point A to point B. The code that makes the car move is implemented in the actionPerformed(ActionEvent e) method and is working fine. But I can't feed this method a different parameter.

The parameters I want to give it are the coordinates of some points (nodes) in the interface ( I am using Dijkstra's algoritm for shortest path). So is there a way to create a method that takes the desired parameters while keeping the same functionalities as the actionPerformed() method? I am new to java GUI interface.

**By actionPerformed() like method I mean a method that loops the code inside of it. This is what the actionPerformed() is doing in my code. **For example, if I print something inside of the actionPerformed() methode, it keeps printing it until I stop the code :

//This is going to print "Example for as long as the code is running
actionPerformed(ActionEvent e){
System.out.print("Example");
}

My actual code :

    @Override
    public void actionPerformed(ActionEvent e) {

        autoRegulTimer++;

        auto_1.setX(auto_1.getX() + 1);  // moves the car on the x axis  

        // basic code that directs the path of the car
        if (auto_1.getX() > DijkstraMain.X1) {      
            
            if(auto_1.getY()>=DijkstraMain.Y2) {    
                auto_1.setY(DijkstraMain.Y2);       
                auto_1.setX(auto_1.getX() + 1);     
            }
            else {                                          
            auto_1.setX(DijkstraMain.X1);            
            auto_1.setY(auto_1.getY() + 1);         
            }
        }
        if (autoRegulTimer % 125 == 0) {
            if (auto_G.size() > 0) {
                auto_G.add(auto_1);
            }
        }
        repaint(); 
    }

This is what I want to do :

    public void actionPerformed(ActionEvent e, Node node) {
        //code 
    }

Any help is appreciated. Thanks.

Filzo
  • 69
  • 7
  • 2
    Can you explain what you mean by an "actionPerformed-like method"? I'm also not sure what you mean by the "functionalities of `actionPerformed`". _You_ are supposed to implement what `actionPerformed` should do. `actionPerformed` itself has no functionalities - it's an interface method. I think you might be misunderstanding `actionPerformed` - it's not `actionPerformed` that is special - all the "magic" happens in the component to which you passed the `ActionListener`. – Sweeper Apr 16 '21 at 02:33
  • The functionality is it keeps looping the code inside of it. For example, if I write in it a print function like so: actionPerformed(ActionEvent e) { System.out.println("Hi") } it is going to keep printing "Hi" until I stop it. – Filzo Apr 16 '21 at 02:36
  • No, `actionPerformed` doesn't do that. What did you give the `ActionListener` to? Anyway, it seems like you need to compute and store a bunch of points that the car needs to go through, and interpolate between them. – Sweeper Apr 16 '21 at 02:39
  • The points are already created in another class that has the Dijkstra algorithm. "What did you give the ActionListener to?" I'm not sure what you mean, but my JPanel implements the ActionListener. – Filzo Apr 16 '21 at 02:45
  • Could you please show us your attempt at what you want the code to look like, even if it doesn't compile, so we understand better what you have in mind? – Bohemian Apr 16 '21 at 02:47
  • I've updated my code above – Filzo Apr 16 '21 at 03:01
  • *I have an animation of a car that moves from point A to point B* - So in your Car class you need a method like `move()`. This will update the location of the car. In the ActionListener all the code does is 1) invoke the `move()` method 2) invoke repaint() on the panel containing the Car object. See: https://stackoverflow.com/questions/54028090/get-width-and-height-of-jpanel-outside-of-the-class/54028681#54028681 for an example of this approach. – camickr Apr 16 '21 at 14:18

0 Answers0