import java.awt.*;
import java.awt.event.*;
import java.util.EventListener;
/*打算你按上,乌龟上爬,按下,乌龟下爬。但是一直没有repaint,怎么回事呀,请教下什么地方错了,怎么才能动。*/
public class WuGui{
public static void main(String args[]){
Frame f = new Frame();
f.setSize(1024, 860);
MyPanel m = new MyPanel();
f.add(m);
f.setVisible(true);
}
}
class MyPanel extends Panel implements KeyListener{
int b = 250;//body Y
int h = 200;//head Y
int f1 = 270;//hand F
int f2 = 270;//hand F
int s1 = 400;//hang S
int s2 = 400;//hang S
int ey = 220; boolean a = true;
public MyPanel(){
this.addKeyListener(this);
}
public void paint(Graphics g){
//super.paint(g);
g.setColor(Color.BLUE); /* draw backGround begin*/
g.fillRect(1,1,1024,860);
g.setColor(Color.GREEN);
g.fillOval(330,b,140,200); /* draw body */
g.fillOval(370,h,60,100);
g.setColor(Color.BLACK);
g.fillOval(370,ey,10,10);
g.fillOval(420,ey,10,10); /* draw head */
g.setColor(Color.GREEN);
g.fillOval(310,f1,70,30); /* draw left up */
g.fillOval(310,s1,70,30); /* draw left down */
g.fillOval(420,f2,70,30); /* draw right up*/
g.fillOval(420,s2,70,30); /* draw right down*/
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
// TODO: Add your code here
if(KeyEvent.VK_UP == e.getKeyCode())
{
if(a == true)
{
a = false;
f1=f1-10;
s2=s2-10;
b=b-5;
h=h-5;
ey=ey-5;
}else{
a = true;
f2=f2-10;
s1=s1-10;
b=b-5;
h=h-5;
ey=ey-5;
}
repaint();
}
if(KeyEvent.VK_DOWN == e.getKeyCode())
{
if(a == true)
{
a = false;
f2=f2+10;
s1=s1+10;
b=b+5;
h=h+5;
ey=ey+5;
}else{
a = true;
f1=f1+10;
s2=s2+10;
b=b+5;
h=h+5;
ey=ey+5;
}
repaint();
}
}
public void keyReleased(KeyEvent e) {
}
大家运行时候一定会发现,当你持续按住up或down键时候,乌龟先移动一点,然后暂停一下,最后才持续运动,各位有没有办法让乌龟持续运动,而没有延迟