在以下程序中我想在红色字体(或******处)处写入一句if语句,
当开始按键盘方向键的时候跳出循环,从而转向执行该按键事件,不知道这个if语句的条件怎么写?
请各位指点指点!
在此谢谢咯!
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Test2 extends Applet implements KeyListener{
int x = 0,y = 0;
int r = 20;

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

public void paint(Graphics g){
g.setColor(Color.red) ;
g.fillOval(x, y, r,r) ;

}

public void keyPressed(KeyEvent e) {
switch(e.getKeyCode())
{  case KeyEvent.VK_LEFT:

  
x = x - 2 ;
    if( x<= 0 )  x = 800;
    setLocation(x, y) ; break ;
   case KeyEvent.VK_RIGHT:
      
   {
    x = x + 2 ;
    if( x>800 ) x = 0 ;
        setLocation( x, y ) ;
    break ;
    }

   case KeyEvent.VK_UP:
    {
     y = y - 2 ;
     if(y<=0)
 y=600;
              setLocation(x, y);
     break;
    }
   case KeyEvent.VK_DOWN:
    {
     y = y + 2;
     if(y>=600) y = 0 ;
                 setLocation(x, y) ;
     break;
   }

}

}
public void keyReleased(KeyEvent e) {
switch(e.getKeyCode())
{  case KeyEvent.VK_LEFT:
while(true)
{x = x - 2 ; setLocation( x, y );
try {
Thread.sleep(100);
}
catch (InterruptedException e1) {

e1.printStackTrace();
}

******         if()break;
    if( x<= 0 ) {
     x = 800;
        break;}
    }
   case KeyEvent.VK_RIGHT:
   while(true){
    x = x + 2 ; setLocation( x, y ) ;
    try {
Thread.sleep(100);
}
catch (InterruptedException e1) {

e1.printStackTrace();
}

    if( x>800 ){
     x = 0 ;
            //setLocation( x, y ) ;
        break ;}
    }

   case KeyEvent.VK_UP:
    while(true){
     y = y - 2 ; setLocation( x, y ) ;
     try {
Thread.sleep(100);
}
catch (InterruptedException e1) {

e1.printStackTrace();
}

     if(y<=0){
    y=600;
                //setLocation(x, y);
        break;}
    }
   case KeyEvent.VK_DOWN:
   while(true){
     y = y + 2; setLocation( x, y ) ;
     try {
Thread.sleep(100);
}
catch (InterruptedException e1) {

e1.printStackTrace();
}

     if(y>=600){
     y = 0 ;
                    //setLocation(x, y) ;
        break;}
   }

}


}
public void keyTyped(KeyEvent e) {
}
}

解决方案 »

  1.   

    楼主,这样做的结构不好,换一种思路看看;
    你的代码应该是用按键驱动一个东西在屏幕上跑,这算是一个最基本的游戏,在处理按键的时候,应该把逻辑处理和按键监听分开来,大体思路是:开一个线程来做一个循环,用线程来驱动每一帧,在驱动中去检测一个整形变量keyCode,分辨这个值并且对应的去处理;在按键中,只需要把你的按键值取到,并且赋值到keyCode就可以了
      

  2.   

    import java.applet.Applet; 
    import java.awt.Color; 
    import java.awt.Graphics; 
    import java.awt.event.KeyEvent; 
    import java.awt.event.KeyListener; 
    public class Test2 extends Applet implements KeyListener{ 
    int x = 0,y = 0; 
    int r = 20; 
    private int flag = 0;
    public void init(){ 
    this.addKeyListener(this); 
    } public void paint(Graphics g){ 
    g.setColor(Color.red) ; 
    g.fillOval(x, y, r,r) ; } public void keyPressed(KeyEvent e) { 
    switch(e.getKeyCode()) 
    {  case KeyEvent.VK_LEFT: 
       flag = 1; 
      
    x = x - 2 ; 
        if( x <= 0 )  x = 800; 
        setLocation(x, y) ; break ; 
      case KeyEvent.VK_RIGHT: 
          
      { 
    flag = 2;     x = x + 2 ; 
        if( x>800 ) x = 0 ; 
            setLocation( x, y ) ; 
        break ; 
        }   case KeyEvent.VK_UP: 
        {
    flag = 3; 
     
        y = y - 2 ; 
        if(y <=0) 
    y=600; 
                  setLocation(x, y); 
        break; 
        } 
      case KeyEvent.VK_DOWN: 
        {
    flag = 4; 
     
        y = y + 2; 
        if(y>=600) y = 0 ; 
                    setLocation(x, y) ; 
        break; 
      } } } 
    public void keyReleased(KeyEvent e) { 
    switch(e.getKeyCode()) 
    {  case KeyEvent.VK_LEFT:
     ]
    while(true&&flag ==1) 
    {x = x - 2 ; setLocation( x, y ); 
    try { 
    Thread.sleep(100); 

    catch (InterruptedException e1) { e1.printStackTrace(); 
    } ******         if()break; 
        if( x <= 0 ) { 
        x = 800; 
            break;} 
        } 
      case KeyEvent.VK_RIGHT: 
      while(true&&flag ==2){ 
        x = x + 2 ; setLocation( x, y ) ; 
        try { 
    Thread.sleep(100); 

    catch (InterruptedException e1) { e1.printStackTrace(); 
    }     if( x>800 ){ 
        x = 0 ; 
                //setLocation( x, y ) ; 
            break ;} 
        }   case KeyEvent.VK_UP: 
        while(true&&flag ==3){
        y = y - 2 ; setLocation( x, y ) ; 
        try { 
    Thread.sleep(100); 

    catch (InterruptedException e1) { e1.printStackTrace(); 
    }     if(y <=0){ 
        y=600; 
                    //setLocation(x, y); 
            break;} 
        } 
      case KeyEvent.VK_DOWN: 
      while(true&&flag ==4){   
     y = y + 2; setLocation( x, y ) ; 
        try { 
    Thread.sleep(100); 

    catch (InterruptedException e1) { e1.printStackTrace(); 
    }     if(y>=600){ 
        y = 0 ; 
                        //setLocation(x, y) ; 
            break;} 
      } } 

    public void keyTyped(KeyEvent e) { 

    }
      

  3.   


    import java.applet.Applet;
    import java.awt.Button;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    public class Dest  extends Applet implements KeyListener
    {
    int x,y;
    int w=0;
    public void start()
    {
    x=100;
    y=100;
    }
    public void init()
    {

    this.addKeyListener(this);

    }


    public void paint(Graphics g)
    {

    g.setColor(Color.red);
    g.fillOval(x,y,10,10);
    if(w==1)
    {
    if(x<=0)
    x=1024;
    else x=x-3;
    try
    {
    Thread.sleep(20);
    }
    catch(InterruptedException k)
    {
    return;
    }
    repaint();
    }

    if(w==2)
    {
    if(x>=1024)
    x=0;
    else x=x+3;
    try
    {
    Thread.sleep(20);
    }
    catch(InterruptedException e)
    {
    return;
    }
    repaint();
    } if(w==3)
    {
    if(y<=0)
    y=768;
    else y=y-3;
    try
    {
    Thread.sleep(20);
    }
    catch(InterruptedException e)
    {
    return;
    }
    repaint();
    }
    if(w==4)
    {
    if(y>=768)
    y=0;
    else y=y+3;
    try
    {
    Thread.sleep(20);
    }
    catch(InterruptedException e)
    {
    return;
    }
    repaint();
    }


    }

    public void keyPressed(KeyEvent e) {
    switch(e.getKeyCode())
    {
    case KeyEvent.VK_LEFT:
      w=1;

    repaint();
    break;
    case KeyEvent.VK_RIGHT:
    w=2;
    repaint();
    break;
    case KeyEvent.VK_UP:
    w=3;
    repaint();
    break;

    case KeyEvent.VK_DOWN:
    w=4;
    repaint();
    break;

    }
    }
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {}