我的MouseMotionListener阻止我的keyDown工作

时间:2014-12-19 04:00:51

标签: java applet keydown mousemotionlistener

这是我的代码

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);

}

问题是当我在init方法中添加mouseMotionlistener和MouseListener时,keyDown停止运行。当我删除它时,它可以工作......这里发生了什么?请帮帮忙?

1 个答案:

答案 0 :(得分:1)

请勿使用Component类中已弃用的方法。请改用事件侦听器。在这种情况下,您需要使用KeyListenerKeyEvent

在此处添加KeyListener

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

在init方法中使用Component类的addKeyListener方法。

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

然后覆盖必要的方法(即public void keyPressed(KeyEvent ev))。

keyPressed方法中,使用KeyEvent类的getKeyCode方法及其字段来执行您需要执行的操作。

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

这几乎是处理键事件和鼠标事件等输入事件的标准操作。正如上面提到的MadProgrammer,最好使用JPanelJApplet之类的Swing工具,并使用它们的键绑定。