请教:一只乌龟的运动
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 void paint(Graphics 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;
}
}
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) {
}
}
搜索更多相关的解决方案:
乌龟 运动
----------------解决方案--------------------------------------------------------
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) {
}
}
----------------解决方案--------------------------------------------------------
你没注册监听器 所以他就不动 不过你的乌龟画的还真强 牛
----------------解决方案--------------------------------------------------------
这只乌龟果然够强,哈哈.
努力!
----------------解决方案--------------------------------------------------------
555 我运行他的乌龟,结果不能结束~~~!气死我了
----------------解决方案--------------------------------------------------------
哇,我运行了一下,乌龟画得太强了,人才
至于为什么不会动,那是因为你没有注册事件监听
还有,键盘事件应该注册到顶级容器上,比如JFrame
----------------解决方案--------------------------------------------------------
乌龟画的真不错
两个眼睛炯炯有神
事件没有被监听
----------------解决方案--------------------------------------------------------
真牛,得向你学习 !
----------------解决方案--------------------------------------------------------
谢谢各位
就是没有加上addKeyListener()函数
谢谢各位帮忙啊!
----------------解决方案--------------------------------------------------------
乌龟画得好厉害啊,能不能把改好的程序贴出来看一下?学习一下。
----------------解决方案--------------------------------------------------------