Results 1 to 14 of 14
  1. #1
    Wirezaid is offline Member
    Join Date
    Mar 2012
    Location
    Lithuania
    Posts
    9
    Rep Power
    0

    Question Can't get the Rectangle to move.

    Hello everyone.
    I'm currently working on a game idea that I have and I struck a problem. The problem is that I have two rectangles that need to interact whit each other. One is a character rectangles that is controlled by the player and the other is a "Push Wall" that is a rectangles that should move when "pushed" by the character rectangle. What happens is the method that should move the "Push Wall" does not change the x and y coordinates but is geting called. If I put a command to move the wall in the PushWall class's paint method it moves. Like this:
    Java Code:
    public void paint(Graphics g) {
                    x++;
    		g.setColor(Color.RED);
    		g.fillRect(x, y, size, size);
    	}
    But not like this:
    Java Code:
    public void movePW(int dir) {
    		switch (dir) {
    		case MOVE_LEFT:
    			x -= size;
    			break;
    		case MOVE_RIGHT:
    			x += size;
    			break;
    		case MOVE_UP:
    			y -= size;
    			break;
    		case MOVE_DOWN:
    			y += size;
    			break;
    		}
    	}
    If anyone can help it would be truly appreciated. Thank you.
    Also if you need more info please ask.

    P.S. Sorry for bad English.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can't get the Rectangle to move.

    I have a feeling that your problem is in code that you've not shown us yet.

  3. #3
    Wirezaid is offline Member
    Join Date
    Mar 2012
    Location
    Lithuania
    Posts
    9
    Rep Power
    0

    Default Re: Can't get the Rectangle to move.

    I actually managed to figure it out. I just made the veriables that I need to change static and it worked perfectly don't really know why though... For some reason not all methods were updated whit the new values... If anyone can tell me why this is please do. And yes it was in the code that I haven't yet showed cuz that code was just an exemple. Still, tnx for your help.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can't get the Rectangle to move.

    Quote Originally Posted by Wirezaid View Post
    I actually managed to figure it out. I just made the veriables that I need to change static and it worked perfectly don't really know why though... For some reason not all methods were updated whit the new values... If anyone can tell me why this is please do. And yes it was in the code that I haven't yet showed cuz that code was just an exemple. Still, tnx for your help.
    This isn't a solution but rather a fragile kludge -- making the variables static, one I don't advise you to do.

  5. #5
    Wirezaid is offline Member
    Join Date
    Mar 2012
    Location
    Lithuania
    Posts
    9
    Rep Power
    0

    Default Re: Can't get the Rectangle to move.

    Quote Originally Posted by Fubarable View Post
    This isn't a solution but rather a fragile kludge -- making the variables static, one I don't advise you to do.
    It may be a bad solution but I just can't see a way to solve it correctly. If it's not too complex maybe you can give me some example of a good solution? If you'll need some of my code just say so.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can't get the Rectangle to move.

    Quote Originally Posted by Wirezaid View Post
    It may be a bad solution but I just can't see a way to solve it correctly. If it's not too complex maybe you can give me some example of a good solution? If you'll need some of my code just say so.
    OK, "so" (else how will I know what you're doing wrong?).

  7. #7
    Wirezaid is offline Member
    Join Date
    Mar 2012
    Location
    Lithuania
    Posts
    9
    Rep Power
    0

    Default Re: Can't get the Rectangle to move.

    For some reason I can post it all in one post so I'll do it in separate ones. Here you go:

    The Main class:

    Java Code:
    package example;
    
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    
    public class Main extends Applet implements Runnable, KeyListener {
    	private static final long serialVersionUID = 1L;
    	private static final int MOVE_LEFT = 0;
    	private static final int MOVE_RIGHT = 1;
    	private static final int MOVE_UP = 2;
    	private static final int MOVE_DOWN = 3;
    	private Thread th;
    	private Character ch;
    	private PushWall pw;
    
    
    	@Override
    	public void init() {
    		setBackground(Color.DARK_GRAY);
    		setSize(800, 600);
    		addKeyListener(this);
    		ch = new Character();
    		pw = new PushWall();
    		th = new Thread(this);
    		th.start();
    	}
    
    
    	@Override
    	public void paint(Graphics g) {
    		pw.paint(g);
    		ch.paint(g);
    	}
    
    
    	@Override
    	public void run() {
    		while (true) {
    			repaint();
    			try {
    				Thread.sleep(17);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    
    	@Override
    	public void keyPressed(KeyEvent ke) {
    		switch (ke.getKeyCode()) {
    		case KeyEvent.VK_LEFT:
    			ch.moveCharacter(MOVE_LEFT);
    			break;
    
    
    		case KeyEvent.VK_RIGHT:
    			ch.moveCharacter(MOVE_RIGHT);
    			break;
    
    
    		case KeyEvent.VK_UP:
    			ch.moveCharacter(MOVE_UP);
    			break;
    
    
    		case KeyEvent.VK_DOWN:
    			ch.moveCharacter(MOVE_DOWN);
    			break;
    		}
    	}
    
    
    	@Override
    	public void keyReleased(KeyEvent ke) {
    	}
    
    
    	@Override
    	public void keyTyped(KeyEvent ke) {
    	}
    }

  8. #8
    Wirezaid is offline Member
    Join Date
    Mar 2012
    Location
    Lithuania
    Posts
    9
    Rep Power
    0

    Default Re: Can't get the Rectangle to move.

    The Character class:

    Java Code:
    package example;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    
    public class Character {
    	private static final int MOVE_LEFT = 0;
    	private static final int MOVE_RIGHT = 1;
    	private static final int MOVE_UP = 2;
    	private static final int MOVE_DOWN = 3;
    	private int size = 40;
    	private int x = 2 * size, y = 2 * size;
    	private PushWall pw;
    
    
    	public Character() {
    		pw = new PushWall();
    	}
    
    
    	public void paint(Graphics g) {
    		g.setColor(Color.LIGHT_GRAY);
    		g.fillRect(x, y, size, size);
    	}
    
    
    	public void moveCharacter(int dir) {
    		switch (dir) {
    		case MOVE_LEFT:
    			x -= size;
    			if (isCollisionWhitPushWall(x, y))
    				pw.movePushWall(dir);
    			break;
    
    
    		case MOVE_RIGHT:
    			x += size;
    			isCollisionWhitPushWall(x, y);
    			if (isCollisionWhitPushWall(x, y))
    				pw.movePushWall(dir);
    			break;
    
    
    		case MOVE_UP:
    			y -= size;
    			isCollisionWhitPushWall(x, y);
    			if (isCollisionWhitPushWall(x, y))
    				pw.movePushWall(dir);
    			break;
    
    
    		case MOVE_DOWN:
    			y += size;
    			isCollisionWhitPushWall(x, y);
    			if (isCollisionWhitPushWall(x, y))
    				pw.movePushWall(dir);
    			break;
    		}
    	}
    
    
    	public boolean isCollisionWhitPushWall(int x, int y) {
    		if (x == pw.getX() && y == pw.getY())
    			return true;
    		else
    			return false;
    	}
    }

  9. #9
    Wirezaid is offline Member
    Join Date
    Mar 2012
    Location
    Lithuania
    Posts
    9
    Rep Power
    0

    Default Re: Can't get the Rectangle to move.

    Lastly the PushWall class, the one that has the problem (I think):

    Java Code:
    package example;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    
    public class PushWall {
    	private static final int MOVE_LEFT = 0;
    	private static final int MOVE_RIGHT = 1;
    	private static final int MOVE_UP = 2;
    	private static final int MOVE_DOWN = 3;
    	private int size = 40;
    
    
    	/* These are the variables that I make static to fix the problem */
    	private static int x = 200, y = 160;
    
    
    	public void paint(Graphics g) {
    		g.setColor(Color.ORANGE);
    		g.fillRect(x, y, size, size);
    	}
    
    
    	public void movePushWall(int dir) {
    		switch (dir) {
    		case MOVE_LEFT:
    			x -= size;
    			break;
    
    
    		case MOVE_RIGHT:
    			x += size;
    			break;
    
    
    		case MOVE_UP:
    			y -= size;
    			break;
    
    
    		case MOVE_DOWN:
    			y += size;
    			break;
    		}
    	}
    
    
    	public int getX() {
    		return x;
    	}
    
    
    	public int getY() {
    		return y;
    	}
    }

  10. #10
    Wirezaid is offline Member
    Join Date
    Mar 2012
    Location
    Lithuania
    Posts
    9
    Rep Power
    0

    Default Re: Can't get the Rectangle to move.

    That's it. Just an example that I made. Hope you can tell me what's the problem.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can't get the Rectangle to move.

    OK, your main problem is that you have more than one PushWall object.

    In your Main class you have a PushWall variable, pw, that is initialized in Main's init method, and which is displayed in the Applet:

    Java Code:
    public class Main extends Applet implements Runnable, KeyListener {
       private static final long serialVersionUID = 1L;
    
       //... code deleted for sake of brevity and clarity
    
       private PushWall pw; // declared here
    
       @Override
       public void init() {
          setBackground(Color.DARK_GRAY);
          // ...
          pw = new PushWall(); // initialized here
          // ...
       }
    
       @Override
       public void paint(Graphics g) {
          pw.paint(g);  // and displayed here
          ch.paint(g);
       }
    You also have another PushWall object in a variable of the same name, pw, in the Character class. This object is never displayed, but instead you try to move it:

    Java Code:
    class Character {
       private static final int MOVE_LEFT = 0;
       // ...
       private PushWall pw; // declared here
    
       public Character() {
          pw = new PushWall(); // initialized here
    
       }
    
       //... 
    
       public void moveCharacter(int dir) {
          switch (dir) {
          case MOVE_LEFT:
             x -= size;
             if (isCollisionWhitPushWall(x, y))
                pw.movePushWall(dir); // moved here
             break;
    
          // ... etc
    The key to understanding your problem is that these two PushWall objects are completely distinct. Only one is being displayed, the one declared and initialized in the Main class, and only one is being moved, the one declared and initialized in the Character class, and you found that moving the second non-displayed PushWall object, had no effect on the PushWall object that was displayed in the Applet. To solve this, you made the variables that represent the PushWall's position static, which changes them from variables that are held by and unique to each object to variables that are held by the class. Now you can think of this as every PushWall object shares the very same x and y variable, and changing the value held by x for one PushWall object will have an effect on all PushWall objects. And yes, this works, but at a price, that price being that a PushWall object now doesn't have it's very own x and y but instead must share it with the class.

    But why is this bad? Say you take the next logical extension to your program and decide that you want not one PushWall object displayed but rather several, all scattered about the applet. If you do this, you'll find that while yes, you can create multiple PushWall objects, you can't place them in different locations since they all share the very same x and y variable, and this limits your program unnecessarily.

    A better solution is not to have this program create two PushWall objects but rather only create one, one that is both displayed in Main and moved in Character. You would do this by declaring PushWall variables in both the Main and Character class, but only initializing the PushWall object once, in Main. And then passing this reference into your Character object so that it can have a reference to the very same PushWall object as is being displayed. This can be done using either constructor parameters or a setter method.

    For example this could work:

    in your Main.java
    Java Code:
    public class Main extends Applet implements Runnable, KeyListener {
       private static final long serialVersionUID = 1L;
    
       //... 
       private Character ch;
       private PushWall pw; // declared here
    
       @Override
       public void init() {
          setBackground(Color.DARK_GRAY);
          // ...
          ch = new Character();
          pw = new PushWall(); // initialized here
          
          ch.setPushWall(pw); // ****** note new method ******
    
          // ...
       }
    And in Character.java
    Java Code:
    class Character {
       private static final int MOVE_LEFT = 0;
       // ...
       private PushWall pw; // declared here
    
       public Character() {
          // pw = new PushWall(); // get rid of this
    
       }
    
       // ***** new method ****
       public void setPushWall(PushWall pw) {
          this.pw = pw;
       }
    Now you can make PushWall's x and y non-static and all is good.

  12. #12
    Wirezaid is offline Member
    Join Date
    Mar 2012
    Location
    Lithuania
    Posts
    9
    Rep Power
    0

    Default Re: Can't get the Rectangle to move.

    Thank you very much! It works perfectly, you're the best!

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can't get the Rectangle to move.

    Quote Originally Posted by Wirezaid View Post
    Thank you very much! It works perfectly, you're the best!
    You are quite welcome and glad it helped. Most important though is do you understand the issue now?

  14. #14
    Wirezaid is offline Member
    Join Date
    Mar 2012
    Location
    Lithuania
    Posts
    9
    Rep Power
    0

    Default Re: Can't get the Rectangle to move.

    Quote Originally Posted by Fubarable View Post
    You are quite welcome and glad it helped. Most important though is do you understand the issue now?
    Yes I do understand it now. The problem now is that I have alot of code to fix cuz I did the same mistake again and again for like 5 more block types . Nevertheless thanks again!

Similar Threads

  1. how to move a rectangle with arrow keys,
    By jmu2101 in forum AWT / Swing
    Replies: 3
    Last Post: 09-27-2011, 02:13 AM
  2. Replies: 6
    Last Post: 12-01-2010, 12:29 AM
  3. Wrong with Rectangle res = new Rectangle(0,0,0,0);???
    By jiapei100 in forum AWT / Swing
    Replies: 3
    Last Post: 09-25-2010, 04:39 PM
  4. To make slider resize rectangle and to move pictures
    By mneskovic in forum New To Java
    Replies: 11
    Last Post: 05-21-2010, 12:07 PM
  5. Rectangle Intersection
    By Gwindow in forum Java 2D
    Replies: 1
    Last Post: 04-24-2008, 04:53 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •