-1

I'm trying to create multiple characters(squares) on the screen that move around randomly. I have already created a CharMove class that creates a square, and moves it around randomly on the screen. However, I tried creating multiple instances of this class in a seperate java file and only 1 instance was created. What is wrong?

CharMove Class:

public class CharMove extends JPanel {
    public static int x = 250;
    public static int y = 250;

    public void paint(Graphics g) {
        Graphics pane = (Graphics2D) g;
        pane.setColor(Color.blue);
        pane.fillRect(x, y, 10, 10); 

    }

    public static void movement(int x, int y, JFrame frame) { 
        CharMove.x = x; 
                CharMove.y = y;
        while (true) {
            try {
                TimeUnit.SECONDS.sleep(1);
                CharMove.x = Getx(CharMove.x,frame); 
                CharMove.y = Gety(CharMove.y,frame);
                frame.repaint();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static int Getx(int a, JFrame frame) { 
        Random rn = new Random();
        int xnum = rn.nextInt(10)-5; 
        a += xnum; 
        System.out.println("x:" + a); 
        return a;
    } 
    public static int Gety(int b, JFrame frame){ 
        Random rn = new Random();
        int ynum = rn.nextInt(10)-5; 
        b += ynum; 
        System.out.println("y:" + b); 
        return b;
    } 
}

World Class

public static void main(String[] args) {
    JFrame game = new JFrame();
    game.setTitle("Matrix");
    game.setSize(500, 500);;
    game.getContentPane().setBackground(Color.white);
    game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.setVisible(true);  
    CharMove char1 = new CharMove(); 
    CharMove char2 = new CharMove();
    game.add(char1);   
    game.add(char2);
    char1.movement(100,100,game); 
    char2.movement(250,250,game);
}
Ajean
  • 5,528
  • 14
  • 46
  • 69
Ninjaman494
  • 180
  • 3
  • 10
  • 1
    Where is the error? What is the expected output? What is "does not work"? – Yassin Hajaj Oct 14 '15 at 22:52
  • 1
    I guess the problem is You didn't mean to actually make x and y static. Remove "static" and it will work the way you expect it to. – Arsen Oct 14 '15 at 22:54
  • The problem is that only one square shows up, so either only one instance is being created or two identical instances are being created. The expected output is two squares that move randomly around the screen. – Ninjaman494 Oct 14 '15 at 22:54
  • 1
    Your x and y are not instance variables, they are static variables. So every instance of CharMove shares the same x and y. – Achintha Gunasekara Oct 14 '15 at 22:56
  • Arsen- If I remove static from the x and y,I create the compiler error: "Cannot make a static reference to the non-static field CharMove.x" – Ninjaman494 Oct 14 '15 at 22:59

3 Answers3

2

However, I tried creating multiple instances of this class in a seperate java file and only 1 instance was created.

Nope, you're creating multiple instances. However, that doesn't make any difference because you don't have any per-instance state. Your only fields are these:

public static int x = 250;
public static int y = 250;

Those are static fields, which means they're not related to any specific instance of the class. You probably just want to remove the static keyword from the declarations. (I'd also make the fields private and provide public getters/setters if necessary, but that's a different matter.)

You'll also need to make your static methods into instance methods - because they're meant to act on individual instances, right? Basically, I think you should revise the meaning of static via whatever book/tutorial you're using to learn Java. (Also revise Java naming conventions.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • +1 for Java naming conventions. Formatting the code nicely (with whatever code style fits you best) usually also helps. – brimborium Oct 14 '15 at 22:59
  • If I remove static from the x and y,I create the compiler error: "Cannot make a static reference to the non-static field CharMove.x" – Ninjaman494 Oct 14 '15 at 23:01
  • 1
    @Ninjaman44: Then you need to fix that too. Basically, you want to operate on instances... You should review *all* your code in the light of that change. – Jon Skeet Oct 14 '15 at 23:03
  • 1
    As @JonSkeet suggested, you should first revise your knowledge about the keyword `static` and member fields. – brimborium Oct 14 '15 at 23:04
0

You should not use public static void movement() as it is not instance method (well the name say it, it is static). Matter of fact, you code should not be able to compile at char1.movement(100,100,game);. Should declare it as instance method public void movement() instead. Actually for the rest of the method, you might want to do it that way too. Static work without instance of the class.

smurf
  • 59
  • 7
  • 3
    The code should be able to compile, because Java allows you to access static members via instances. It sucks IMO, but that's what the JLS says... – Jon Skeet Oct 14 '15 at 23:01
  • Why should it not compile? You can call static methods using instances. I never understood why, though. :) – brimborium Oct 14 '15 at 23:01
0

Your x and y are not instance variables, they are static variables. So every instance of CharMove shares the same x and y

Try this,

public class CharMove extends JPanel {
    private int x = 250;
    private int y = 250;

    public void paint(Graphics g) {
        Graphics pane = (Graphics2D) g;
        pane.setColor(Color.blue);
        pane.fillRect(x, y, 10, 10); 

    }

    public void movement(JFrame frame) { 
        while (true) {
            try {
                TimeUnit.SECONDS.sleep(1);
                this.x = CharMove.Getx(this.x,frame); 
                this.y = CharMove.Gety(this.y,frame);
                frame.repaint();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static int Getx(int a, JFrame frame) { 
        Random rn = new Random();
        int xnum = rn.nextInt(10)-5; 
        a += xnum; 
        System.out.println("x:" + a); 
        return a;
    } 
    public static int Gety(int b, JFrame frame){ 
        Random rn = new Random();
        int ynum = rn.nextInt(10)-5; 
        b += ynum; 
        System.out.println("y:" + b); 
        return b;
    } 
}

and

public static void main(String[] args) {
    JFrame game = new JFrame();
    game.setTitle("Matrix");
    game.setSize(500, 500);;
    game.getContentPane().setBackground(Color.white);
    game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.setVisible(true);  
    CharMove char1 = new CharMove(); 
    CharMove char2 = new CharMove();
    game.add(char1);   
    game.add(char2);
    char1.movement(game); 
    char2.movement(game)
}
Achintha Gunasekara
  • 1,165
  • 1
  • 15
  • 29
  • doesn't work. The square doesn't move at all now. It works when only one instance of the class is created but not when i introduce a 2nd one. – Ninjaman494 Oct 14 '15 at 23:12
  • I've had a typo, it should be CharMove.Getx, and CharMove.Gety. I've corrected it above. So what happens when you call move on the second square? – Achintha Gunasekara Oct 14 '15 at 23:29
  • Only one square appears,and it doesn't move,if I try only adding 1 square (game.add) buy two movements it works with 1 square,but the other one doesn't appear – Ninjaman494 Oct 15 '15 at 10:52