My MouseMotionListener is stopping my keyDown from working

66 Views Asked by At

Here is my code

Image duelSGX;
Image selectionBG;
Image Ultor;
Image serin;
int picWidth = 500;
int picHeight = 500;

int[] duelSGXStats = {4424, 1067, 2000};
int[] UltorStats = {4950, 2415, 1605};
int[] serinStats = {5993, 1647, 1527};

int appletsize_x = 300;
int appletsize_y = 200;

int x_pos = appletsize_x / 2;         // x - Position of ball
int y_pos = appletsize_y / 2;        // y - Position of ball

int radius = 20;        // Radius of ball

int x_coordinate = 500;
int y_coordinate;
private Image dbImage;
private Graphics dbg;

boolean isButtonPressed = false;
public void init ()
{
    resize (1900, 960);
    selectionBG = getToolkit ().getImage (PICTURE_PATH1);
    duelSGX = getToolkit ().getImage (PICTURE_PATH);
    Ultor = getToolkit ().getImage (PICTURE_PATH2);
    serin = getToolkit ().getImage (PICTURE_PATH3);


    addMouseListener (this);
    addMouseMotionListener (this);


}


public void stop ()
{
}


public void destroy ()
{
}


public void run ()
{



}


public void update (Graphics g)
{

    // initialize buffer
    if (dbImage == null)
    {
        dbImage = createImage (this.getSize ().width, this.getSize ().height);
        dbg = dbImage.getGraphics ();
    }

    // clear screen in background
    dbg.setColor (getBackground ());
    dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);

    // draw elements in background
    dbg.setColor (getForeground ());
    paint (dbg);

    // draw image on the screen
    g.drawImage (dbImage, 0, 0, this);

}


public boolean keyDown (Event e, int key)
{
    Thread.currentThread ().setPriority (Thread.MIN_PRIORITY);


    if (key == Event.LEFT)
    {
        if (x_coordinate >= (-877 - 877))
        {
            x_coordinate -= 100;
        }

    }

    else if (key == Event.RIGHT)
    {
        if (x_coordinate <= 1900 - (677 + 200))
        {
            x_coordinate += 100;
        }
    }






    // DON'T FORGET (although it has no meaning here)
    repaint ();
    Thread.currentThread ().setPriority (Thread.MAX_PRIORITY);
    return true;

}


public void mouseEntered (MouseEvent e)
{
    // called when the pointer enters the applet's rectangular area
}


public void mouseExited (MouseEvent e)
{
    // called when the pointer leaves the applet's rectangular area
}


public void mouseClicked (MouseEvent e)
{
    // called after a press and release of a mouse button
    // with no motion in between
    // (If the user presses, drags, and then releases, there will be
    // no click event generated.)
}


public void mousePressed (MouseEvent e)
{
    ;
}


public void mouseReleased (MouseEvent e)
{
}


public void mouseMoved (MouseEvent e)
{
}


public void mouseDragged (MouseEvent e)
{ // called during motion with buttons down

}


public void paint (Graphics g)
{

    g.drawImage (selectionBG, 0, 0, 1960, 960, null);

    g.drawImage (duelSGX, x_coordinate, y_coordinate, 677, 960, null);
    g.drawImage (Ultor, x_coordinate + 877, y_coordinate, 677, 960, null);
    g.drawImage (serin, x_coordinate + 877 + 877, y_coordinate, 677, 960, null);

}

The problem is that keyDown stops functioning when I add the mouseMotionlistener and the MouseListener in the init method. When I remove them it works.... What's going on here? Help Please?

1

There are 1 best solutions below

1
On

Don't use the deprecated methods from the Component class. Use event listeners instead. You'll want to use KeyListener and KeyEvent in this case.

Add KeyListener at the top here.

public class SomeClass
    extends Applet
    implements KeyListener, MouseListener, MouseMotionListener {

In your init method use the Component class's addKeyListener method.

public void init() {
    addKeyListener(this);
}

Then override the necessary methods (i.e. public void keyPressed(KeyEvent ev)).

In the keyPressed method, use the KeyEvent class's getKeyCode method and its fields to do what you need to do.

if (ev.getKeyCode() == KeyEvent.VK_LEFT) {
    // code here
}

This is pretty much the standard operation for handling input events such as key events and mouse events. As MadProgrammer mentioned above, it's a good idea to use Swing tools such as JPanel or JApplet, and to use key bindings with them.