Results 1 to 7 of 7
  1. #1
    Coupon22's Avatar
    Coupon22 is offline The Man of the Land
    Join Date
    Mar 2012
    Location
    Eastern U.S.A
    Posts
    2
    Rep Power
    0

    Default Problems with moving images down/right

    I am trying to create a simple game applet, but have ran into a roadblock. When I try to move an image up or to the right (y++ or x++), it does nothing. I have a ++ and +=1 , both do the same thing and both do not work. -- and -= works fine.

    Here is the code:
    Java Code:
    package src;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.applet.*;
    import java.io.*;
    
    import javax.imageio.ImageIO;
    
    public class DragonDon extends Applet implements Runnable, KeyListener
    {
    	private static final long serialVersionUID = -5332581391364562362L;
    	BufferedImage img, img2, img3, img4;
    	int x = 200;
    	int y = 100;
    	int x1 = 50;
    	int y1 = 60;
    	
    	public DragonDon()
    	{
    		addKeyListener(this);
    	}
    	
    	public void init()
    	{
    		   setSize(600, 300);
    	       try
    	       {
    	           img = ImageIO.read(new File("dragon.jpg"));
    	           img2 = ImageIO.read(new File("human.jpg"));
    	           img3 = ImageIO.read(new File("humano.jpg"));
    	           img4 = ImageIO.read(new File("dragono.jpg"));
    	       } catch (IOException e)
    	       {
    	       }
    	}
    	
    	public void run()
    	{
    		
    	}
    	
    	public void start()
    	{
    		
    	}
    	
    	public void stop()
    	{
    		
    	}
    	
    	public static void main(String[] arg0)
    	{
    		
    	}
    	
    	public void paint(Graphics g)
    	{
    		Graphics2D ga = (Graphics2D)g;
    	    ga.drawImage(img, null, x, y);
    	    ga.drawImage(img2, null, x1, y1);
    	}
    
    	@Override
    	public void keyReleased(KeyEvent arg0)
    	{
    	}
    
    	public void keyTyped(KeyEvent arg0)
    	{ 
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) 
    	{
    		switch (e.getKeyCode())
    		{
    			case KeyEvent.VK_RIGHT:
    				x++;
    				img = img4;
    			case KeyEvent.VK_LEFT:
    				x--;
    			case KeyEvent.VK_DOWN:
    				y++;
    			case KeyEvent.VK_UP:
    				y--;
    		}
    		switch (e.getKeyCode())
    		{
    			case KeyEvent.VK_D:
    				x1 += 1;
    				img3 = img2;
    			case KeyEvent.VK_A:
    				x1 -= 1;
    				img2 = img3;
    			case KeyEvent.VK_S:
    				y1 += 1;
    			case KeyEvent.VK_W:
    				y1 -= 1;
    		}
    		int OFFSET = 1;
    		int w = 256;
    		int h = 256;
    		int w1 = 128;
    		int h1 = 128;
            repaint(x, y,w+OFFSET,h+OFFSET);
            repaint(x1, y1,w1+OFFSET,h1+OFFSET);
    	}
    }

  2. #2
    Join Date
    Mar 2012
    Posts
    54
    Rep Power
    0

    Default Re: Problems with moving images down/right

    I don't use this part of java but I say try testing it with a system.out.println(""); thing. They always help me out and I think this would be a perfect place to do that. Try putting it in case itself so that way you can see if the case is being used at all. It might not be the problem with the +=and ++ because it shouldn't be.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,790
    Rep Power
    25

    Default Re: Problems with moving images down/right

    Print out the values of the variables before you change them and after you change them to see what if code is doing what you want.

    For debug add a default: case to the switch statements that prints out a message when it is executed so you know where the code is going in the switch.

  4. #4
    Coupon22's Avatar
    Coupon22 is offline The Man of the Land
    Join Date
    Mar 2012
    Location
    Eastern U.S.A
    Posts
    2
    Rep Power
    0

    Default Re: Problems with moving images down/right

    I have added System.out.println("(Whatever)"); to add the switch and to each case. They work fine.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,790
    Rep Power
    25

    Default Re: Problems with moving images down/right

    Have you solved the problem now?

  6. #6
    Join Date
    Mar 2012
    Posts
    54
    Rep Power
    0

    Default Re: Problems with moving images down/right

    Try printing the value of the x and y before and after of each case. Maybe from there you can see the problem.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,790
    Rep Power
    25

    Default Re: Problems with moving images down/right

    You need to add the -Xlint compiler option to the javac command to see what is the problem. The compiler will give a warning if you use that option.

Similar Threads

  1. Displaying moving images
    By Maulik in forum AWT / Swing
    Replies: 3
    Last Post: 04-02-2011, 01:00 AM
  2. Images not moving Please help!!!
    By reborn567 in forum New To Java
    Replies: 11
    Last Post: 11-09-2010, 06:25 PM
  3. ordering moving images in swing
    By IAnkit in forum AWT / Swing
    Replies: 2
    Last Post: 09-21-2010, 04:56 PM
  4. [Applet] Clicking images & moving them.
    By Perfectworld in forum Java Applets
    Replies: 17
    Last Post: 01-16-2010, 09:09 AM
  5. Moving Images in JPanel
    By killpoppop in forum AWT / Swing
    Replies: 7
    Last Post: 03-08-2009, 03:54 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
  •