-1

我想在applet中左右上下移动角色,但它根本不动。这是我的代码,请帮助

import javax.swing.JPanel;

import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;

public class drawCenter extends Applet
{
    private int x,y;// the x and y of the position of the player
    private BufferedImage image, pos;

    public void init(  )
    {
        try
        {
            image = ImageIO.read(new File("pokemonCenter.png"));
            pos = ImageIO.read(new File("player/maleInGame.png"));
        }
        catch (IOException ex)
        {

        }
        x = 150; y = 171;
    }

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

        switch( keyCode )
        {
            case KeyEvent.VK_UP: if( y>0 )
            {
                y=y-19;
                repaint();

            }

            break;
            case KeyEvent.VK_DOWN: if( y<171 )
            {
                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;
        }
        e.consume();
    }

    public void keyReleased(){

    }

    public void paint( Graphics g )
    {
        g.drawImage(image, 0, 0, null);
        g.drawImage(pos, x, y, null);
    }
}
4

4 回答 4

1

您的代码假装有一个 KeyListener,但在任何地方都找不到 KeyListener,更不用说添加到 GUI 的组件中了。您的解决方案:创建一个实现 KeyListener 或扩展 KeyAdapter 的类,然后将其添加到具有焦点的 GUI 组件中。

请查看:如何编写 KeyListener

此外,我建议您避免让您的 GUI 类、Applet 实现 KeyListener,因为这会给类带来太多责任。更好的是,我认为要么为您的 KeyListener 创建一个匿名内部类,甚至是一个单独的独立类。

于 2012-09-02T01:10:08.870 回答
0

让你的类实现KeyListener,然后super.addKeyListener(this)在你的构造函数中调用。正如您现在所拥有的,您既没有指定类实现接口(尽管实现了它需要的方法),也没有使用Applet.

于 2012-09-02T01:11:21.637 回答
0

尝试这个。

import javax.swing.*;
import java.awt.*;

import static java.awt.event.KeyEvent.*;

public class DrawCenter extends JApplet implements KeyListener {

    Image character = null;
    int x = 0;
    int y = 0;

    public Image loadImage(String name){
        return new ImageIcon(getClass().getClassLoader().getResource(name)).getImage();
    }

    public void init(){
        character = getImage("pokemonCenter.png");
        x = getWidth()/2 - character.getWidth(null)/2;
        y = getHeight()/2 - character.getHeight(null)/2;
        addKeyListener(this);
    }

    public void keyPressed(KeyEvent e){
        switch (e.getKeyCode()){
            case VK_LEFT: x--; break;
            case VK_RIGHT: x++; break;
            case VK_UP:    y--; break;
            case VK_DOWN: y++; break;
        }
        repaint();
    }

    public void paint(Graphics g){
        g.drawImage(character, x, y, null);
    }

}

如果您正在制作游戏,请查看游戏循环或尝试Game-Engine (If you'r interested, go to http://game-engine-for-java.googlecode.com/ )

于 2012-09-02T01:14:05.993 回答
0

你应该是这样的:

package stack;


import java.applet.*;

import java.awt.event.*;

import java.awt.*;

public class DrawCenter extends Applet implements KeyListener{

    private int x=50,y=50;// the x and y of the position of the player


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

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

    public void keyReleased(){

    }


    public void paint( Graphics g ){ //will draw the background and the character
        g.fillRect(x, y, 20, 20);

    }

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

    }

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

    }
}
于 2012-09-02T01:15:03.943 回答