Results 1 to 4 of 4
  1. #1
    dotabyss is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default Problem with a thread

    Hello everyone.

    I started to make a little "game" and I got a problem I can't fix.

    I had this:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class GameNave extends JFrame implements Runnable, KeyListener{
    	
    	public static void main (String args[])
    	{
    		GameNave game = new GameNave();
    		game.construirJanela();
    	}
    	
    	public void construirJanela()
    	{
    		Container container = getContentPane();
    		container.setBackground(Color.black);
    		setTitle("Game: Nave");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setSize(800, 600);
    		setLocationRelativeTo(null);
    		setVisible(true);
    		setResizable(false);
    		addKeyListener(this);
    		setFocusable(true);
    		
    		x = 400;
    		y = 500;
    	}
    	
    	private Thread animador;
    	private int x, y, x2, y2;
    	private Image nave;
    	
    	public void paint (Graphics g)
    	{
    		super.paint(g);
    		nave = new ImageIcon("nave.png").getImage();
    		Graphics2D g2d = (Graphics2D)g;
    		g2d.drawImage(nave, x, y, this);
    	}
    
    	public void addNotify()
    	{
    		super.addNotify();
    		animador = new Thread(this);
    		animador.start();		
    	}
    	
    	public void keyPressed(KeyEvent e) {
    
            int key = e.getKeyCode();
    
            if (key == KeyEvent.VK_LEFT) {
                x2 = -5;
            }
    
            if (key == KeyEvent.VK_RIGHT) {
                x2 = 5;
            }
    
            if (key == KeyEvent.VK_UP) {
                y2 = -5;
            }
    
            if (key == KeyEvent.VK_DOWN) {
                y2 = 5;
            }
        }
    
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
    
            if (key == KeyEvent.VK_LEFT) {
                x2 = 0;
            }
    
            if (key == KeyEvent.VK_RIGHT) {
                x2 = 0;
            }
    
            if (key == KeyEvent.VK_UP) {
                y2 = 0;
            }
    
            if (key == KeyEvent.VK_DOWN) {
                y2 = 0;
            }
        }
        
        public void keyTyped (KeyEvent e)
        {
        	
        }
        
        public void movimento()
        {
        	x += x2;
        	y += y2;
        }
    	public void run()
    	{
    		while (true)
    			{
    			movimento();
    			repaint();
    			
    			try
    			{
    				Thread.sleep(25);
    			}
    			catch (InterruptedException e)
    			{
    			}
    			
    			}
    	}
    }
    It was fine.
    After that, I wanted to make a spacecraft that was able to shoot. Then I did this:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    
    public class GameNave2 extends JFrame implements Runnable, KeyListener{
    	
    	public static void main (String args[])
    	{
    		GameNave2 game = new GameNave2();
    		game.construirJanela();
    	}
    	
    	public void construirJanela()
    	{
    		Container container = getContentPane();
    		container.setBackground(Color.black);
    		setTitle("Game: Nave");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setSize(800, 600);
    		setLocationRelativeTo(null);
    		setVisible(true);
    		setResizable(false);
    		addKeyListener(this);
    		setFocusable(true);
    		
    		x = 400;
    		y = 500;
    	}
    	
    	private Thread animador;
    	private int x, y, x2, y2;
    	private Image nave;
    	private ArrayList tiros;
    	
    	public void paint (Graphics g)
    	{
    		super.paint(g);
    		
    		nave = new ImageIcon("nave.png").getImage();
    		tiros = new ArrayList();
    		
    		Graphics2D g2d = (Graphics2D)g;
    		g2d.drawImage(nave, x, y, this);
    		
    		ArrayList ms = tiros;
    		for (int i = 0; i < ms.size(); i++)
    		{
    			Tiro t = (Tiro) ms.get(i);
    			g2d.drawImage(t.tiro, t.x, t.y, this);
    		}
    	}
    
    	public void atirar()
    	{
    		tiros.add(new Tiro(x + 32, y + 16));
    	}
    	
    	public void addNotify()
    	{
    		super.addNotify();
    		animador = new Thread(this);
    		animador.start();		
    	}
    	
    	public void keyPressed(KeyEvent e) {
    
            int key = e.getKeyCode();
    
            if (key == KeyEvent.VK_SPACE){
            	atirar();
            }
            
            if (key == KeyEvent.VK_LEFT) {
                x2 = -5;
            }
    
            if (key == KeyEvent.VK_RIGHT) {
                x2 = 5;
            }
    
            if (key == KeyEvent.VK_UP) {
                y2 = -5;
            }
    
            if (key == KeyEvent.VK_DOWN) {
                y2 = 5;
            }
        }
    
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
    
            if (key == KeyEvent.VK_LEFT) {
                x2 = 0;
            }
    
            if (key == KeyEvent.VK_RIGHT) {
                x2 = 0;
            }
    
            if (key == KeyEvent.VK_UP) {
                y2 = 0;
            }
    
            if (key == KeyEvent.VK_DOWN) {
                y2 = 0;
            }
        }
        
        public void keyTyped (KeyEvent e)
        {
        	
        }
        
        public void movimento()
        {
        	x += x2;
        	y += y2;
        }
    	public void run()
    	{
    		while (true)
    			{
    			ArrayList ts = tiros;
    			for (int i = 0; i < ts.size(); i++)
    			{
    				Tiro m = (Tiro) ts.get(i);
    				if (m.visivel = true)
    				{
    					m.movimentoTiro();
    				}
    				else
    				{
    					ts.remove(i);
    				}
    			}
    			
    			movimento();
    			repaint();
    			
    			try
    			{
    				Thread.sleep(25);
    			}
    			catch (InterruptedException e)
    			{
    			}
    			
    			}
    	}
    }
    And this:

    Java Code:
    import java.awt.Image;
    import javax.swing.ImageIcon;
    
    public class Tiro {
    	
    	int x, y;
    	Image tiro;
    	boolean visivel;
    	
    	private final int ALTURA_JANELA = 600;
    	private final int VELOCIDADE_TIRO = 2;
    	
    	public Tiro(int x, int y)
    	{
    		tiro = new ImageIcon("tiro.png").getImage();
    		visivel = true;
    		this.x = x;
    		this.y = y;
    	}
    	
    	public void movimentoTiro()
    	{
    		x += VELOCIDADE_TIRO;
    		if (x < ALTURA_JANELA)
    		{
    			visivel = false;
    		}
    	}
    }
    As a result, I have a window with the craft, but I can't move it.

    Eclipse gives me this error:
    "Exception in thread "Thread-3" java.lang.NullPointerException
    at tutorial.pack.GameNave2.run(GameNave2.java:128)
    at java.lang.Thread.run(Unknown Source)"

    I wish someone could help me. Thanks in advance.

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

    Default

    Quote Originally Posted by dotabyss View Post
    Eclipse gives me this error:
    "Exception in thread "Thread-3" java.lang.NullPointerException
    at tutorial.pack.GameNave2.run(GameNave2.java:128)
    at java.lang.Thread.run(Unknown Source)"

    I wish someone could help me. Thanks in advance.
    That's a bit of code there. Could you give us a hint as to which line it is that is throwing this NPE?

    Also, on cursory glance, I see that you've got some basic problems with your code including:

    1) Drawing directly in the JFrame instead of a JPanel or JComponent -- losing all advantages of Swing such as double buffering
    2) Overriding a paint method rather than a JPanel's paintComponent with the same problem as above.
    3) Reading in an image file from within the paint method -- something guaranteed to slow your game graphics incredibly.
    4) Performing program logic within the paint method -- this method (or better paintComponent) is for painting only not logic as you want it to be small and fast for fast painting, and you can't control when it will or won't be called.
    Last edited by Fubarable; 02-14-2010 at 10:29 PM.

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

    Default

    I've run your code and your NPE occurs here:
    Java Code:
       public void run() {
          while (true) {
             ArrayList ts = tiros;
             for (int i = 0; i < ts.size(); i++) {  // *** this line throws the NPE
    The reason it throws NPE is because the array list tiros is null. And this is null because of point 4) above -- performing program logic in the paint method -- you try to instantiate tiros inside of paint, but paint has not been called yet:
    Java Code:
       private ArrayList tiros;
    
       public void paint(Graphics g) {
          super.paint(g);
          nave = new ImageIcon("nave.png").getImage();  // and don't try to read an image here
          tiros = new ArrayList();  // you try to instantiate tiros here.
    Solution: see points 1) - 4) above.

  4. #4
    dotabyss is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    Wow, thanks a lot.
    I'll spend some more time with this code to improve it.

    Thanks again.

Similar Threads

  1. Animation Delay - Thread problem
    By wererabit in forum Advanced Java
    Replies: 3
    Last Post: 04-10-2009, 10:35 PM
  2. [SOLVED] Can't figure out my thread/awt problem
    By Zosden in forum Advanced Java
    Replies: 7
    Last Post: 07-29-2008, 05:13 PM
  3. [SOLVED] Thread in GUI Problem
    By terox13 in forum Threads and Synchronization
    Replies: 3
    Last Post: 05-27-2008, 08:32 PM
  4. [SOLVED] HELP! Thread Problem
    By nvidia in forum NetBeans
    Replies: 2
    Last Post: 05-24-2008, 04:56 AM
  5. Problem using thread +rmi in my homework
    By IbrahimAbbas in forum Threads and Synchronization
    Replies: 10
    Last Post: 04-14-2008, 09:24 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
  •