title: How to add vision to a JComponent
body: <p>I have a dynamic number of JComponents moving in a JPanel. I want to make them stop when another Jcomponent is in it's way. I've tried to make a search about 2d raycasting, unfortunately i don't know how to implement it in my code. Stackoferflow have some answers for this but there they're building colliders, I need something more similar to raycasting.</p>
  <p>Heres the JPanel code <strong>Map.java:</strong></p>
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  
  public class Map extends JPanel implements ActionListener {
      final int windowWidth = 1300;
      final int windowHeight = 750;
  
      Map() {
          this.setPreferredSize(new Dimension(windowWidth, windowHeight));
          Timer timer = new Timer(3000, this);
          timer.start();
      }
  
      @Override
      public void paint(Graphics g) {
          super.paint(g);
          // painting map
      }
  
      // Update method of the frame
      @Override
      public void actionPerformed(ActionEvent e) {
          Car car = new Car();
          this.add(car);
          // For loop responds for data cleaning
          for(int i=0;i&lt;this.getComponents().length;i++) {
              Car iCar = (Car) this.getComponents()[i];
              if(iCar.x &gt;= 1400 || iCar.x &lt;= -300 || iCar.y &lt;= -300 || iCar.y &gt;= 950) {
                  this.remove(i);
              }
          }
          System.out.println(this.getComponentCount());
          this.updateUI();
      }
  }
  
  </code></pre>
  <p>Heres JComponent code <strong>Car.java:</strong></p>
  <pre><code>import javax.imageio.ImageIO;
  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  import java.awt.geom.AffineTransform;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.io.IOException;
  import java.util.Random;
  
  public class Car extends JComponent implements ActionListener {
      private BufferedImage car;
      private final GeoPoint startPoint = GeoSides.pickGeoPoint();
      public int x = 0;
      public int y = 0;
      private int xVelocity;
      private int yVelocity;
      private final int imageX = 160;
      private final int imageY = 120;
      private double turnAngle = 0;
      Timer timer;
  
      // Initializing Car() object
      Car() {
          this.setPreferredSize(new Dimension(170, 130));
          this.timer = new Timer(1, this); // Setting up the timer for update method
          this.timer.start();
          try {
              Random randomize = new Random();
              car = ImageIO.read(new File(&quot;src/assets/&quot; + (randomize.nextInt(10) + 1) + &quot;.png&quot;)); // Getting a random image of car
          } catch (IOException e) {
              e.printStackTrace();
          }
          // Setting start position
      }
  
      // This method should be called when another car is in the way
      public void stopCar() {
          xVelocity = 0;
          yVelocity = 0;
      }
  
      // Method scales the image and rotate it if needed
      private void drawCar(Graphics2D render2D, double rotationAngle) {
          //Image scaling and rotating
      }
  
      // Inherited method from JComponent
      @Override
      public void paintComponent(Graphics g) {
          super.paintComponent(g);
          this.setLocation(new Point(x, y));
  
          Graphics2D render2D  = (Graphics2D) g;
          drawCar(render2D, turnAngle);
      }
  
      // Frame update of car's x, y speed and turn angle
      @Override
      public void actionPerformed(ActionEvent e) {
          switch (startPoint){
              // Setting destination points
          }
  
          x += xVelocity;
          y += yVelocity;
          this.repaint();
  
      }
  }
  </code></pre>