简体   繁体   English

如何使用KeyListener在Applet中移动图像?

[英]How to move image in Applet using KeyListener?

I'm trying to make an Applet that displays two images and allows the user to move one of them using the left/ right arrow keys (or keys a and d). 我正在尝试制作一个显示两个图像的小程序,并允许用户使用向左/向右箭头键(或键a和d)移动其中一个。 On the bright side, I know I imported the images correctly because it displays both of them once I run the the program; 从好的方面来说,我知道我正确地导入了图像,因为一旦运行程序,它就会显示两个图像。 however, once I press the left or right key an error promptly pops up and the image doesn't move as I intended. 但是,一旦按向左或向右键,就会立即弹出错误,并且图像没有按预期移动。 If someone could tell me what's wrong and explain the details on what I need to fix that would be a great help. 如果有人可以告诉我出了什么问题并详细说明我需要解决的问题,那将是一个很大的帮助。 Thanks in advance. 提前致谢。

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class mainClass extends Applet implements KeyListener {
Image pic1, pic2;
int x=0, y=0,move=5;

public void init(){
    setSize (1600,1200);
    pic1 = getImage(getDocumentBase(),"beach king.jpg");
    pic2 = getImage(getDocumentBase(),"eye.jpg"); 
    addKeyListener(this);
}
public void start(){

}
public void paint(Graphics g){
    //setBackground(Color.CYAN);
    try{Thread.sleep(100);} 
    catch(Exception e){}
    //going to change position of pic1
    g.drawImage(pic1, x, y, this);
    g.drawImage(pic2, 0, 0, this);
}

private boolean[] keys;

public void keyPressed(KeyEvent e) {
    keys[e.getKeyCode()] = true;
}

public void keyReleased(KeyEvent e) {
    keys[e.getKeyCode()] = false;
}
public void keyTyped(KeyEvent e) {}

public void update() {
    if(keys[KeyEvent.VK_A] || keys[KeyEvent.VK_LEFT]){
       x-=move;
    }
    if(keys[KeyEvent.VK_D] || keys[KeyEvent.VK_RIGHT]){
       x+=move;
    }
}

} }

One issue with your code is that you never initialize the array private boolean[] keys , but since you are just trying to change the x-position of your image, it is highly impractical to use a boolean array. 代码的一个问题是,您永远不会初始化数组的private boolean[] keys ,但是由于您只是试图更改图像的x位置,因此使用布尔数组非常不切实际。 Instead, just update the x value whenever a key is pressed. 相反,只要按下一个键就更新x值。

Your keyPressed function looks then as follows: 您的keyPressed函数如下所示:

public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_LEFT) {
        x-=move;
    }
    else if (e.getKeyCode() == KeyEvent.VK_D || e.getKeyCode() == KeyEvent.VK_RIGHT) {
        x+=move;
    }
    this.repaint();
}

In the last line of this method you call repaint, such that the applet actually redraws the image. 在此方法的最后一行中,您调用repaint,以便小程序实际重绘图像。

You can then leave the keyReleased method empty, because you don't need to reset any value. 然后,您可以将keyReleased方法保留为空,因为您无需重置任何值。 Finally, delete your update method, as it is never called (the other methods in your code are called because they are either specified in the super class or in the interface). 最后,删除update方法,因为它永远不会被调用(由于在超类或接口中指定了它们,因此会调用代码中的其他方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM