0

Well my problem is that I'm making the stars move with a thread, they move verticaly and it works good but i do a random X for the star and sometimes it intersecs other stars like this :

enter image description here

enter image description here

This is my code for the JPanel:

class Backgroundmoving

public class Backgroundmoving extends JPanel {
    ArrayList<starmoving> star;

    public Backgroundmoving() {
        this.setSize(650, 501);
        star = new ArrayList<>();
        for (int i = 0; i < 20; i++)
            this.addStar();
    }

    public void addStar() {
        int x, y;

        x = (int) (Math.random() * 625);

        y = (int) (Math.random() * 476);

        starmoving e = new starmoving(x, y);
        star.add(e);
        Thread t = new Thread(e);
        t.start();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        draw(g);
    }

    public void draw(Graphics g) {
        g.drawImage(new ImageIcon("background.png").getImage(), 0, 0, 650, 501, null);
        for (int i = 0; i < star.size(); i++) {
            star.get(i).draw(g);
        }
        repaint();
    }

    public static void main(String[] args) {
        // TODO code application logic here
        JFrame gui = new JFrame();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(650, 510);
        gui.setResizable(false);
        gui.add(new Backgroundmoving());

        gui.setVisible(true);
    }

}

class starmoving

public class starmoving implements Runnable {

    int x;
    int y;
    int yVel;

    public starmoving(int x, int y) {
        this.x = x;
        this.y = y;
        yVel = 1;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    private void move() {
        y += yVel;
        if (y > 476) {
            y = 0;
            x = (int) (Math.random() * 625);
        }
    }

    private boolean isOffScreen() {

        if (y <= 476)
            return false;
        return true;
    }

    public void draw(Graphics g) {
        g.drawImage(new ImageIcon("star.png").getImage(), x, y, 12, 12, null);
    }

    @Override
    public void run() {
        while (true) {
            move();

            try {
                Thread.sleep(7);
            } catch (InterruptedException ex) {
                System.out.println(ex.getMessage());
            }

        }
    }

}
Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28
Steven_95
  • 75
  • 8
  • And please show us the `starmoving` class. – Christian St. Apr 26 '14 at 10:20
  • the question is that i'm not sure how to don't intersect other star, added starmoving class – Steven_95 Apr 26 '14 at 10:39
  • Why would't stars appear to intersect on a 2D projection of 3D space? See also [_parallax scrolling_](http://en.wikipedia.org/wiki/Parallax_scrolling). – trashgod Apr 26 '14 at 10:42
  • i don't want stars intersecting, was thinking in a if before the random X but how can i know if other star is in that X ? , maybe i can do it implemmenting Runnable in my Panel and do the control in the method run but is there other way? – Steven_95 Apr 26 '14 at 10:48

1 Answers1

0

i don't want stars intersecting, was thinking in a if before the random X but how can i know if other star is in that X ?

You have an ArrayList that contains all your "StarMoving" objects. So you need to iterate through that list to make sure that none of the object intersect.

On top of that you have other problems.

  1. Use proper Java class names. Java classes SHOULD start with an upper case character. (ie. "starmoving" is wrong)

  2. Don't use multiple Threads for the animation. You current code starts 20 Threads. You should have a single Thread and then iterate through your ArrayList to move all the stars.

  3. Don't read the image in the draw() method. Currently you code is reading the image every 7ms. This is not very efficient. The image should be read once and then stored as a property of your class.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • i know " starmoving" is wrong it was only a example , for the point 3 okay will read it once but i didn't understand what do you mean, how can i use a single thread for everything? ._. – Steven_95 Apr 26 '14 at 15:12
  • oh i got what you meant , use t = new ... and declarate t on the constructor – Steven_95 Apr 26 '14 at 15:18
  • `i know " starmoving" is wrong it was only a example` - if you want us to read your code then follow the standards. Why would you waste time using an incorrect class name and then change it later. Do it right the first time!!! The Thread should be `implemented and started` in the `BackgroundMoving` class AFTER you add all the stars to the ArrayList. – camickr Apr 26 '14 at 15:20
  • do you mean remove t= new Thread(e); t.start(); from addStar and after the for in the constructor do t= new Thread(star); and t.start(); ? – Steven_95 Apr 26 '14 at 15:28
  • it shouldn't work because star is an ArrayList i have to do other for like doing this t= new Thread(star.get(i)); t.start(); – Steven_95 Apr 26 '14 at 15:30
  • @HkGDota, Yes, after you add all the stars to the ArrayList you start the Thread. Then in the run(...) method you iterate through all the stars in the list and invoke the move method on each star. – camickr Apr 26 '14 at 15:40
  • i don't have a run method in BackgroundMoving, i didnt implement Runnable – Steven_95 Apr 26 '14 at 16:18
  • @HkGDota, that was my point! The BackgroundMoving class SHOULD implement Runnable. The StarMoving class SHOULD NOT. – camickr Apr 26 '14 at 21:31
  • okay but how would i move the stars without the method run in StarMoving class ? – Steven_95 Apr 27 '14 at 07:48
  • oh got it , with a for in the method run in backgroundMoving right? – Steven_95 Apr 27 '14 at 07:50
  • done it is working now but i'm still thinking about the problem of the stars intersecting – Steven_95 Apr 27 '14 at 07:56