-2

I am making a program where the user can use the arroy keys on the keyboard to move through a maze. I am able to get the droid object to move from left to right just fine; but I cannot do the same for moving up or down. When I try to move it in those directions nothing happens at all which I am not understanding. What am I doing wrong? Here is the code :

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Droid2 extends JPanel
{
int x=10, y=200;


   ImageIcon icon = new ImageIcon("robot.jpg");


   public Droid2()
   {

      addKeyListener (new DotsListener());
      setFocusable(true);
      setPreferredSize(new Dimension(850, 600));
   }

public void paintComponent(Graphics page)
   {
       super.paintComponent(page);
       File input = new File("maze.png");
       Image  img = new ImageIcon("maze.png").getImage();
       Image  img2 = new ImageIcon("robot.png").getImage();
        BufferedImage bufferedImage;
        try {
            bufferedImage = ImageIO.read(input);
            Color c=new Color(bufferedImage.getRGB(x, y));// & 255;int d=(bufferedImage.getRGB(x, y)<<16)&0xFF;
            System.out.println("Red:"+c.getRed()+",\t Green:"+c.getGreen()+",\t Blue:"+c.getBlue());
        if(c.getBlue()<255)
            y-=10;
        } catch (IOException e) {
            e.printStackTrace();
        }



        page.drawImage(img, 0, 0, null);
        page.drawImage(img2, x, 260, null);
       System.out.println();

   }

 private class DotsListener implements KeyListener
 {

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
                if(e.getKeyCode()==KeyEvent.VK_RIGHT)
                    x+=10;
                else if(e.getKeyCode()==KeyEvent.VK_DOWN)
                    y+=10;
                else if(e.getKeyCode() ==KeyEvent.VK_LEFT)
                    x-=10;
                else if(e.getKeyCode() ==KeyEvent.VK_UP)
                    y-=10;

           repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
   }
}
`
ognimoddd
  • 87
  • 1
  • 1
  • 8
  • Possible duplicate of [How to use Key Bindings instead of Key Listeners](http://stackoverflow.com/questions/22741215/how-to-use-key-bindings-instead-of-key-listeners) – user1803551 Oct 09 '16 at 17:47
  • Also, if you're using images, it's a good idea to give them to us. – user1803551 Oct 09 '16 at 17:52

1 Answers1

0

Probably because you are not using the y variable to draw the image, instead you just use a hard-coded 260.

fdreger
  • 12,264
  • 1
  • 36
  • 42
  • When you are redrawing image you need to refresh y coordinate as well. Also a word of advice try using keyreleased to update the coordinates instead of pressed. – Akshay Anand Oct 09 '16 at 16:25
  • Seems like it. I had it at just "y" before but couldn't position the droid object at the beginning of the maze so I hard-coded it to be at the maze entrance. Still trying to figure it out; thanks! – ognimoddd Oct 09 '16 at 16:27
  • When I switch it back to y the object moves in weird directions. I'm stumped on this one :/ – ognimoddd Oct 09 '16 at 17:08