为什么我的头像不会移动。

时间:2013-05-15 00:24:09

标签: java applet awt keylistener

这是代码。即使单击方向键,该框也不会移动。所以,我想知道发生了什么。你能帮助我吗?提前致谢!

import java.awt.*;
import javax.swing.*;      
import java.applet.*;
import java.awt.event.*;
import java.awt.Robot.*;
import java.awt.Color.*;
public class Project extends Applet implements MouseListener, MouseMotionListener, KeyListener
{
   Robot robos;
   Color colour;
   String s = "";
   int mouseX = 0, mouseY = 0;
   int width, height;
   int x, y;
   int bSx, bSy;
   int blSx, blSy;
   int lTO, lTT, rTO, rTT; 
   boolean canPass;
   public void init(){
    canPass = true;
    colour = new Color(0, 0, 0);
    addMouseListener(this);
    addMouseMotionListener(this);
    addKeyListener(this);
    width = getSize().width;
    height = getSize().height;
    bSx = width / 2 - 3;
    bSy = height / 2 - 3;
    blSx = width / 2 - 2;
    blSy = height / 2 - 2;
    repaint();
    lTO = height;
    lTT = (width / 2)/ 2;
    rTO = height;
    rTT = (int)((double)((width / 2) * 1.5));
}
public void mouseClicked(MouseEvent me){
    x = me.getX();
    y = me.getY();               //This method has no relevance to the problem.
    s = "";
    repaint();
    me.consume();
    }
    public void mouseEntered(MouseEvent me){ }
    public void mouseExited(MouseEvent me){ }
    public void mousePressed(MouseEvent me){ }
    public void mouseReleased(MouseEvent me){ }
    public void mouseDragged(MouseEvent me){ }
    public void keyPressed( KeyEvent me ) {   //This is the method we want to look at(And paint too)
        if((me.getKeyCode() == KeyEvent.VK_DOWN || me.getKeyCode() == KeyEvent.VK_KP_DOWN) && (bSy + 10 <= height)){
             for(int a = bSx; a < bSx + 5; a++){
                colour = robos.getPixelColor(a, bSy + 6);                  
                 if(colour.equals(Color.black)){
                     canPass = false;                    
                }
            }
            if(canPass){
                 bSy++;
                 blSy++;
            }       
         }                
         if(me.getKeyCode() == KeyEvent.VK_UP || me.getKeyCode() == KeyEvent.VK_KP_UP){
              for(int a = bSx; a < bSx + 5; a++){ 
                 colour = robos.getPixelColor(a, bSy - 1);
                 if(colour.equals(Color.black)){
                    canPass = false;
                 }
              }
             if(canPass){
              bSy--;
              blSy--;
             }
        }
        if(me.getKeyCode() == KeyEvent.VK_LEFT || me.getKeyCode() == KeyEvent.VK_KP_LEFT){
             for(int a = bSy + 6; a > bSy; a--){  
                    colour = robos.getPixelColor(bSx - 1, a);
                    if(colour.equals(Color.black)){
                        canPass = false;
                    }
             }
             if(canPass){
                 bSx--;
                 blSx--;
             }  
        }
        if(me.getKeyCode() == KeyEvent.VK_RIGHT || me.getKeyCode() == KeyEvent.VK_KP_RIGHT){
            for(int a = bSy + 6; a < bSy + 11; a++){ 
                    colour = robos.getPixelColor(bSx + 6, a);
                    if(colour.equals(Color.black)){
                        canPass = false;
                    }
            }
            if(canPass){
                bSx++;
                blSx++;
            }
        }
        repaint();
        me.consume();
        canPass = true;
    }
    public void keyReleased( KeyEvent me ) { }
    public void keyTyped( KeyEvent me ) { }
    public void mouseMoved(MouseEvent me){
        showStatus("Moving mouse at " + me.getX() + ", " + me.getY());
    }
    public void paint(Graphics g){ 
        g.drawLine(lTT, lTO, lTT, 0);
        g.drawLine(rTT, rTO, rTT, 0);
        g.setColor(Color.cyan);
        g.drawRect(bSx, bSy, 5, 5);
        g.setColor(Color.blue);
        g.fillRect(blSx, blSy, 4, 4);
        g.setColor(Color.black);
        g.drawLine(0, 400, 500, 400);
    }
}

如果可能的话,我可以解释一下出了什么问题吗?所以我知道将来在编码时要避免什么?再次感谢提前!

0 个答案:

没有答案