图像不在applet中移动

时间:2016-04-10 06:16:15

标签: java swing applet

我想在applet中移动一个Image(这里是矩形),applet绘制它,但我想知道为什么图像不移动?没有编译错误!

这是我的代码:

package game;

import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;



public class Gamer extends JApplet implements KeyListener
{
    private int x=50,y=50;


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

@Override
public void keyPressed(KeyEvent e)
{
    int keyCode = e.getKeyCode();

    switch( keyCode )
    {
    case KeyEvent.VK_UP:
        if( y>0 )  //when up key is pressed and the position of the player is not on the edge
        {
            y=y-19;
            repaint();
        }
        break;
    case KeyEvent.VK_DOWN:
        if( y<171 ) //when down key is pressed and the position of the player is not on the edge
        {
            y=y+19;
            repaint();
        }
        break;
    case KeyEvent.VK_LEFT:
        if( x>0 )
        {
            x=x-15;
            repaint();
        }
        break;
    case KeyEvent.VK_RIGHT:
        if( x<285 )
        {
            x=x+15;
            repaint();
        }
        break;
    }
}
@Override
public void paint( Graphics g )  //will draw the background and the character
{
    g.fillRect(x, y, 200, 200);

}

@Override
public void keyReleased(KeyEvent arg0)
{
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0)
{
    // TODO Auto-generated method stub

}
}

当我按下上/下/左/写箭头时,矩形没有移动。 请解释原因? T.I.A。

1 个答案:

答案 0 :(得分:1)

问题是你的applet没有捕获KeyEvent,所以你的paint方法不会被调用。

实际上,如果您要将KeyListener添加到JApplet并且它无效,则会出现问题。

解决方案是实施KeyEventDispatcher而不是KeyListener。此外,我将矩形的大小从200更改为20,以便能够更好地查看矩形的移动:

package game;

import java.awt.Graphics;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;

import javax.swing.JApplet;

public class Gamer extends JApplet implements KeyEventDispatcher {
    private int x = 50, y = 50;

    @Override
    public void init() {
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);
    }

    @Override
    public void paint(Graphics g) // will draw the background and the character
    {
        super.paint(g); // <- added to your code to clear the background
                        //         before re-painting the new square

        g.fillRect(x, y, 20, 20);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent e) {
        int keyCode = e.getKeyCode();

        switch( keyCode )
        {
        case KeyEvent.VK_UP:
            if( y>0 )  //when up key is pressed and the position of the player is not on the edge
            {
                y=y-19;
                repaint();
            }
            break;
        case KeyEvent.VK_DOWN:
            if( y<171 ) //when down key is pressed and the position of the player is not on the edge
            {
                y=y+19;
                repaint();
            }
            break;
        case KeyEvent.VK_LEFT:
            if( x>0 )
            {
                x=x-15;
                repaint();
            }
            break;
        case KeyEvent.VK_RIGHT:
            if( x<285 )
            {
                x=x+15;
                repaint();
            }
            break;
        }
        return false;
    }

}

希望这会有所帮助。