0

这是我的代码

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 停止运行。当我删除它们时,它会起作用....这里发生了什么?请帮忙?

4

1 回答 1

1

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

在此处添加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,最好使用诸如JPanelor之类的 Swing 工具JApplet,并使用它们的键绑定。

于 2014-12-19T04:14:02.397 回答