So I have an application that successfully moves a ball (ellipse) left, right, up, or down depending on which button of the keypad is being pressed. However, I'm having trouble getting the ball to move at an angle. I'm trying to do this by saying that if the user holds 2 direction keys, the ball will go in diagonally. For example, If they click the left key and the up key, I want the ball to move in an NorthWest direction. If any of you see what is causing the problem, I'd really appreciate the help!
Engine
class KeyClickListener implements KeyListener
{
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_DOWN )
{
component.moveCircle(1);
}
else if (e.getKeyCode() == KeyEvent.VK_UP)
{
component.moveCircle(2);
}
else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
{
component.moveCircle(3);
}
else if (e.getKeyCode() == KeyEvent.VK_LEFT)
{
component.moveCircle(5);
}
//this is my attempt at telling whether or not the user was holding two keys
else if (e.getKeyCode() == KeyEvent.VK_LEFT && e.getKeyCode() == KeyEvent.VK_UP)
{
component.moveCircle(5);
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
}
CircleComponent
public void moveCircle(int w)
{
dir = w;
if (dir == 1)
{
y+=20;
repaint();
if (y>=560)
{
dir=2;
}
}
.
.
.
else if (dir == 5)
{
x-=10;
y-=10;
repaint();
if(x<=0 )
{
dir=3;
}
}
}