Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: Java problem. Thread wont stop(FIXED)

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java problem. Thread wont stop(FIXED)

    Hello again guys!(I wrote another post yesterday)
    I have encountered another problem. It is probably another one of these things were i forget to put some code in and it doesn´t work. Well the problem is that the threads im using wont stop when i want them to. I have 2 threads wich each contains a while loop that makes the squares in my little game move. I have made 2 booleans and put them to true. So the while loop looks like this: while(isGoing){}, so i have made a method called collision and have the intersects method in an if statement for the 2 squares. So if they intersect i basically want to switch the booleans to false and stop the whole thing from moving. But i doesn´t work. This post probably looks a bit messy. But i will post the code and go to bed because im pretty tired. The code might be a bit messy/bad because i dont have much experience with games. But anyways here is the code:
    First class:
    import javax.swing.*;
    import java.awt.*;
     
     
    public class Main extends JFrame{
     
    	Image dbImage;
    	Graphics dbg;
     
    	Player hero;
     
    	AI bot;
    	public Main(){
    		bot = new AI();
    		hero = new Player();
    		addKeyListener(hero);
    		setVisible(true);
    		setSize(500,500);
    		setResizable(false);
    		setDefaultCloseOperation(3);
    		setLocation(500,250);
     
    	}
     
    	public void draw(Graphics g){
    		g.setColor(Color.GRAY);
    		g.fillRect(0,0,500,500);
    		bot.draw(g);
    		hero.draw(g);
     
    	}
     
     
    	public void paint(Graphics g){
    		dbImage = createImage(getWidth(),getHeight());
    		dbg = dbImage.getGraphics();
    		draw(dbg);
    		g.drawImage(dbImage,0,0,this);
    		repaint();
    	}
     
    	public static void main(String[] args) {
    		new Main();
     
    	}
     
    }
    Second class:
    import java.awt.*;
    import java.util.Random;
     
    public class AI implements Runnable{
    	Player player;
     
    	Rectangle enemy;
    	int x = 250,y = 250;
    	int randXDir;
    	int randYDir;
     
    	boolean isGoing = true;
     
    	Random r;
     
    	Thread t1;
     
    	public AI(){
     
    		r = new Random();
    		t1 = new Thread(this);
    		player = new Player();
    		enemy = new Rectangle(x,y,15,15);
    		x = enemy.x;
    		y = enemy.y;
    		setRandomXDir();
    		setRandomYDir();
     
    		t1.start();
    	}
     
    	public void setRandomXDir(){
     
    		randXDir = 0;
    		randXDir = r.nextInt(485);
     
    	}
    	public void setRandomYDir(){
     
    		randYDir = 0;
    		randYDir = r.nextInt(485);
     
    	}
     
    	public void collision(){
    		if(enemy.intersects(player.hero)){
    			isGoing = false;
    			player.isGoing = false;
    		}
    	}
     
    	public void draw(Graphics g){
    		g.setColor(Color.GREEN);
    		g.fillRect(x, y, enemy.width, enemy.height);
    		g.drawString("X: " + x + "Y: " + y, 25,50);
     
    	}
    	public void setDirection(){
     
    		if(x > randXDir){
     
    			x--;
    		}
     
    		if(x < randXDir){	
     
    			x++;
    		}
     
    		if(y > randYDir){
     
    			y--;
     
    		}
     
    		if(y < randYDir){
     
    			y++;
     
    		}
     
    		if(x == randXDir && y == randYDir){
    			setRandomXDir();
    			setRandomYDir();
     
    		}
     
    	}
    	public void move(){
    		collision();
    		setDirection();
    	}
     
    	public void run(){
     
    		try{
    			while(isGoing){
     
    				move();
    			Thread.sleep(5);
    			}
    		}catch(Exception e){System.out.println("error");}
     
    	}
    }
    Third class:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
     
    public class Player implements KeyListener, Runnable{
    	Rectangle hero;
     
    	int heroX = 100,heroY = 100;
    	int xDir, yDir;
     
    	boolean isGoing = true;
     
    	Thread t1;
     
    	public Player(){
     
    		hero = new Rectangle(heroX,heroY,30,30);
    		t1 = new Thread(this);
     
    		t1.start();
    	}
     
    	public void draw(Graphics g){
    		g.setColor(Color.BLUE);
    		g.fillRect(heroX, heroY,hero.width ,hero.height);
     
    		collision();
    	}
    	public void collision(){
    		if(heroX >= 470)
    			heroX = 470;
     
    		if(heroX <= 0)
    			heroX = 0;
     
    		if(heroY >= 470)
    			heroY = 470;
     
    		if(heroY <= 20)
    			heroY = 20;
    	}
     
    	public void setXDirection(int xDir){
    		this.xDir = xDir;
    	}
    	public void setYDirection(int yDir){
    		this.yDir = yDir;
    	}
     
    	public void keyPressed(KeyEvent e) {
    		int keyCode = e.getKeyCode();
    		if(keyCode == KeyEvent.VK_UP){
    			setYDirection(-1);
    		}
    		if(keyCode == KeyEvent.VK_DOWN){
    			setYDirection(1);
    		}
    		if(keyCode == KeyEvent.VK_RIGHT){
    			setXDirection(1);
    		}
    		if(keyCode == KeyEvent.VK_LEFT){
    			setXDirection(-1);
    		}
    	}
     
     
    	public void keyReleased(KeyEvent e) {
    		int keyCode = e.getKeyCode();
    		if(keyCode == KeyEvent.VK_UP){
    			setYDirection(0);
    		}
    		if(keyCode == KeyEvent.VK_DOWN){
    			setYDirection(0);
    		}
    		if(keyCode == KeyEvent.VK_RIGHT){
    			setXDirection(0);
    		}
    		if(keyCode == KeyEvent.VK_LEFT){
    			setXDirection(0);
    		}
    	}
    	public void keyTyped(KeyEvent arg0) {}
     
    	public void move(){
     
    		heroX += xDir;
    		heroY += yDir;
    	}
     
     
    	public void run() {
    		try{
    			while(isGoing){
    				move();
    				Thread.sleep(3);
    			}
    		}catch(Exception e){System.out.println("Error");}
    	}
     
    }
    I have also tried switching things around so some things might be even more weird but i dont know im pretty tired so i cant reply until tomorrow.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,606
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Java problem. Thread wont stop

    Try debugging the code by adding some println statements to print out the values of variables used to control the logic. For example in collision() print out the two rectangles that are being tested for intersection.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java problem. Thread wont stop

    Well i haven´t fixed the problem. But it seems if(enemy.intersects(player.hero)) is never true...

    --- Update ---

    This is so annoying. I cant find the problem! Why doesn´t the intersecs work!?

    --- Update ---

    Can someone look at my intersecs method in collision? The if statement doesn´t get executed and i dont know why

    --- Update ---

    Sigh

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,606
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Java problem. Thread wont stop

    Did you do what I suggested in my post? What was printed? The output will help you find what the problem is.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java problem. Thread wont stop

    Well i put a println in the following way:
    public void collision(){
    System.out.println("COLLISION");
    if(enemy.intersects(player.hero)){
    		System.out.println("FALSE");
    		isGoing = false;
    		player.isGoing = false;
    	}
     
    }
    And when i did that it only printed "COLLISION", so i know that the if statement is never executed. But i dont know how to fix it...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,606
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Java problem. Thread wont stop

    know how to fix it...
    Reread post #2 about what to print out. What you are printing does not show any variable's value. You need to see the values to know what the code is doing so you can work on fixing it.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java problem. Thread wont stop

    I dont know what the problem is and i will probably just give up.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,606
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Java problem. Thread wont stop

    I dont know what the problem is
    You will never find the problem if you don't debug the code.

    What was printed out when you added the println statements I suggested? Did you look at the printout to see if it made sense?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java problem. Thread wont stop

    Im not even sure what im supposed to put and where. I have never debugged with println before and i have no idea what variables i should print and where even after i red your first post.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,606
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Java problem. Thread wont stop

    what variables i should print
    Here is what I suggested:
    For example in collision() print out the two rectangles that are being tested for intersection
    if(enemy.intersects(player.hero)){
    enemy
    player.hero

    Print them before the if statement
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java problem. Thread wont stop

    Do you mean like the x and y coordinates or?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,606
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Java problem. Thread wont stop

    The Rectangle class has a toString() method that will return the values of all 4 variables. Just use the name of the variable in the println() statement.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java problem. Thread wont stop

    Oh okay i will try that in a while

    --- Update ---

    When i do that it only displays the same coordinates all the time. Its like the rectangles i drew and created as objects arent the same. Im not experienced with making games and this is just for fun and see if i can make just a little thing like this. So is it like that? They arent the same?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,606
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Java problem. Thread wont stop

    displays the same coordinates all the time
    That would mean that the code is not changing the coordinates of those rectangles. Check the code to see why it doesn't change the rectangle's contents.

    What values does the code use to draw the objects at changing locations?
    Are those values in the rectangle objects?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java problem. Thread wont stop

    This is some code i have for the rectangle:
    	Rectangle hero;
            int heroX,heroY;
    	int xDir, yDir;
    	int width,height;
                    heroX = 100;
    		heroY = 100;
     
    		width = 30;
    		height = 30;
     
    		hero = new Rectangle(heroX,heroY,width,height);
     
                    g.fillRect(heroX,heroY,width,height);
     
                     public void collision(){
    		if(heroX >= 470)
    			heroX = 470;
     
    		if(heroX <= 0)
    			heroX = 0;
     
    		if(heroY >= 470)
    			heroY = 470;
     
    		if(heroY <= 20)
    			heroY = 20;
    	}
    There are some parts of the code that might help. They are from different methods and I only included one full method wich is the collision detection for the sides of the screen wich works fine. This is only for 1 of the 2 rectangles but they are built in the same way.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,606
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Java problem. Thread wont stop

    Where does the code change the values of the hero retangle?
    The separate variable: heroX has nothing to do with the rectangle: hero that is used in the collision() method where you printed out the rectangle's values.
    You should be using the x and y variables in the Rectangle class, not separate variables.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java problem. Thread wont stop

    Well i have changed everything with seperate variables to for example hero.x. Here is the code that makes the Player rectangle move:
    public void setXDirection(int xDir){
    		this.xDir = xDir;
    	}
    	public void setYDirection(int yDir){
    		this.yDir = yDir;
    	}
     
    	public void keyPressed(KeyEvent e) {
    		int keyCode = e.getKeyCode();
    		if(keyCode == KeyEvent.VK_UP){
    			setYDirection(-1);
    		}
    		if(keyCode == KeyEvent.VK_DOWN){
    			setYDirection(1);
    		}
    		if(keyCode == KeyEvent.VK_RIGHT){
    			setXDirection(1);
    		}
    		if(keyCode == KeyEvent.VK_LEFT){
    			setXDirection(-1);
    		}
    	}
    public void move(){
     
    		hero.x += xDir;
    		hero.y += yDir;
    	}
    I also have a keyReleased that just sets xDir and yDir to 0.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,606
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Java problem. Thread wont stop

    Does the collision test work now?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java problem. Thread wont stop

    Not yet but its getting there i think.

  20. #20
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java problem. Thread wont stop

    I was wrong

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,606
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Java problem. Thread wont stop

    It happens.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java problem. Thread wont stop

    Fixed. Im sorry for the long post and thank you for answering

Similar Threads

  1. Java game problem! Square wont move(FIXED)
    By albin1228 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 14th, 2013, 01:22 PM
  2. Having problem to stop reading strings
    By scsa316 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2011, 10:24 AM
  3. Timer wont stop...
    By pottsiex5 in forum AWT / Java Swing
    Replies: 8
    Last Post: October 7th, 2011, 02:49 PM
  4. How to Stop a Thread Safley
    By viper_07 in forum Threads
    Replies: 4
    Last Post: July 17th, 2011, 05:26 PM
  5. Replies: 4
    Last Post: April 27th, 2010, 01:18 AM