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

    Default Drawing moving object

    So... I continued to work on my little game code.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class GameNave2 extends JPanel implements Runnable, KeyListener{
    	
    	public static void main (String args[])
    	{
    		GameNave2 game = new GameNave2();
    		game.construirJanela();
    	}
    	
    	public void construirJanela()
    	{
    		JFrame janela = new JFrame();
    		janela.setTitle("Game: Nave");
    		janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		janela.setSize(800, 600);
    		janela.setLocationRelativeTo(null);
    		janela.add(this);
    		setDoubleBuffered(true);
    		setBackground(Color.black);
    		addKeyListener(this);
    		setFocusable(true);
    		janela.setVisible(true);
    		
    		x = 400;
    		y = 500;
    	}
    	
    	private Thread animador;
    	private int x, y, x2, y2;
    	private Image nave;
    	private Image tiro;
    	private boolean atirar;
    	private int tiroX, tiroY;
    	
    	public GameNave2()
    	{
    		nave = new ImageIcon("nave.png").getImage();
    		tiro = new ImageIcon("tiro.png").getImage();
    	}
    
    	public void posicaoTiro()
    	{
    		tiroX = x;
    		tiroY = y;
    	}
    	
    	public void paintComponent (Graphics g)
    	{
    		super.paintComponent(g);
    		Graphics2D g2d = (Graphics2D)g;
    		g2d.drawImage(nave, x, y, this);
    
    		if (atirar == true)
    		{
    			g2d.drawImage(tiro, tiroX, tiroY, 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;
            }
            
            if (key == KeyEvent.VK_SPACE){
            	atirar = true;
            }
        }
    
        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();
    			
    			if (atirar == true)
    			{
    				tiroY -= 5;
    			}
    			
    			try
    			{
    				Thread.sleep(25);
    			}
    			catch (InterruptedException e)
    			{
    			}
    			
    			}
    	}
    }
    I currently don't have any error with it, but things aren't working the way I wish they were.:(

    For example, when the image tiro (wich is a shot) is drawn, it's draw at the position 0,0, when I wanted it to be drawn as tiroX and tiroY being a position near the spacecraft I have in the game.
    This way, the image would be moving from the lower to the upper part of the screen.

    Also, I wanted to be able to shoot more than just one time...

    The method posicaoTiro(), when called inside the paintComponent() method just makes things worse...

    If someone could help me... I'd be glad.
    Last edited by dotabyss; 02-16-2010 at 10:24 PM.

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    I would suggest creating seperate classes for the ship and for the projectiles in order to keep track and manager the things that belong in your world. You can store them in a list that you can loop through during your update phase.

    With that, when you create your "tiro" object you can give it a position relative to your ships object.
    My Hobby Project: LegacyClone

Similar Threads

  1. moving square
    By blindfolded in forum New To Java
    Replies: 5
    Last Post: 01-22-2010, 05:58 PM
  2. moving a file
    By swati.jyoti in forum New To Java
    Replies: 8
    Last Post: 11-23-2009, 08:44 AM
  3. Moving Box
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-30-2009, 12:29 PM
  4. Moving textboxes
    By GabWit in forum New To Java
    Replies: 2
    Last Post: 01-26-2009, 04:07 PM
  5. moving a file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 07:52 PM

Posting Permissions

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