-2

I'm working on a simple project and I can't achieve how to move a drawable object as (a)cross. What I mean is, for example, when I press up and left arrow keys at the same time, I want my drawable to move towards exact north-west. However, by using simple if statements in KeyListener methods, I couldn't fulfill it. Is there a special way to do it or kinda extra package? Here is my code:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import javax.swing.*;
import javax.swing.JPanel;


public class Top extends JPanel implements ActionListener, KeyListener {

/**
 * 
 */
private static final long serialVersionUID = 1L;
Timer t = new Timer(5, this);
int xR = 0;int yR = 0; int zR = 0;
 double x = 0, y = 0, xVel = 0, yVel = 0, width = 0, height = 0;

public Top() {
    t.start();
    setBackground(Color.black);
    addKeyListener(this);
    setFocusable(true);
}
public void actionPerformed(ActionEvent e) {
    repaint();
    x+=xVel;
    y+=yVel;
}
public void paintComponent(Graphics g)  {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D)g;

    g2d.setPaint(Color.blue);
    g2d.fill(new Ellipse2D.Double(this.x, this.y, 50, 50));
}
@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();

    if(keyCode == KeyEvent.VK_DOWN) {
        down();
    }
    if(keyCode == KeyEvent.VK_UP) {
        up();
    }
    if(keyCode == KeyEvent.VK_RIGHT) {
        right();
    }
    if(keyCode == KeyEvent.VK_LEFT) {
        left();
    }
}
public void down() {
    this.yVel= 1;
    this.xVel= 0;
}
public void up() {
    this.yVel = -1;
    this.xVel = 0;
}
public void left() {
    this.xVel = -1;
    this.yVel = 0;
}
public void right() {
    this.xVel = 1;
    this.yVel = 0;
}
@Override
public void keyReleased(KeyEvent e) {
    int keyCode = e.getKeyCode();
     if(keyCode == KeyEvent.VK_DOWN || 
               keyCode == KeyEvent.VK_UP ||            
               keyCode == KeyEvent.VK_RIGHT || 
               keyCode == KeyEvent.VK_LEFT ) {
                   xVel=0;
                   yVel=0;  
    }
  }
}

*this code has only ability to move an ellipse exactly left right up or down *

HexagonSun
  • 39
  • 1
  • 6
  • You probably need to post your code in order to be helped with this. – Jonatan Apr 11 '12 at 13:50
  • We don't know what you've tried without some code to look at, please post some so we don't have to guess a solution – Chris Apr 11 '12 at 13:50
  • See the accepted answer here: http://stackoverflow.com/questions/752999/how-do-i-handle-multiple-key-press-in-java - there's no "event" for multiple keys because the keyboard sends them as separate events – Brian Roach Apr 11 '12 at 13:56
  • If your question has been answered, or if it is no longer valid, please 'tick' to choose the most appropriate answer so everyone knows that the problem has been resolved. Thanks. – wattostudios May 14 '12 at 13:35

2 Answers2

0

You need to use a KeyListener to listen for the keyboard events, then re-draw the object in the correct spot according to the particular key pressed. You will possibly need to repaint() the JPanel as well, depending on how you have the image implemented.

Could you please post some code that shows what you're trying to do, and we will attempt to assist your problem. The first thing to do would be to validate that the KeyListener is working correctly, so the code for your KeyListener would probably be a good start.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
0

I would use a boolean for each direction, left, right, up, and down. So when another keyboard event happens, it will check to see if one of the arrow keys is still being held down. For example, if you press (and do not release) the right arrow key, a boolean for the right arrow key will be set as true.

That's only for setting the variables up, now to handle diagonal directions. Again, go to your keyPressed method. For the up and down keys, the format should look like this...

if (keyCode == VK.DOWN) {
   if (leftKeyHeld) {
         DownLeft(); 
   } else if (rightKeyHeld) {
         DownRight();
   } else {
         Down();
   }

}

Just be sure for your keyReleased method, you set the booleans to false so the program does not think that you're constantly holding down the keys when you aren't.

Anthony
  • 496
  • 3
  • 9