JAVA 键盘监听
packageAnime;importjava.awt.Frame;importjava.awt.Graphics;importjava.awt.Image;import...
package Anime;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.ImageObserver;
public class Sample extends Frame {
private Image img = null;
private Image img2 = null;
private Image img3 = null;
int by=30;
public static void main(String[] args){
new Sample().setVisible(true);
}
public Sample(){
setSize(803,603);
img = Toolkit.getDefaultToolkit().createImage("22.gif");
img3 = Toolkit.getDefaultToolkit().createImage("231.gif");
img2 = Toolkit.getDefaultToolkit().createImage("3.jpg");
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ev){
System.exit(0);
}
});
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if((KeyEvent.VK_RIGHT)!=0)
by = by+5;
}
});
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
g.drawImage(img2,0,0,this);
g.drawImage(img3,by,490,this);
g.drawImage(img,580,260,this);
}
public boolean imageUpdate(Image im,int flag,int x,int y,int w,int h){
super.imageUpdate(im,flag,x,y,w,h);
return true;
}
}
这个代码想实现游戏角色的左右移动 但是现在只能实现向右移动,并且按方向左键角色还是向右移动,请教解决办法 展开
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.ImageObserver;
public class Sample extends Frame {
private Image img = null;
private Image img2 = null;
private Image img3 = null;
int by=30;
public static void main(String[] args){
new Sample().setVisible(true);
}
public Sample(){
setSize(803,603);
img = Toolkit.getDefaultToolkit().createImage("22.gif");
img3 = Toolkit.getDefaultToolkit().createImage("231.gif");
img2 = Toolkit.getDefaultToolkit().createImage("3.jpg");
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ev){
System.exit(0);
}
});
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if((KeyEvent.VK_RIGHT)!=0)
by = by+5;
}
});
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
g.drawImage(img2,0,0,this);
g.drawImage(img3,by,490,this);
g.drawImage(img,580,260,this);
}
public boolean imageUpdate(Image im,int flag,int x,int y,int w,int h){
super.imageUpdate(im,flag,x,y,w,h);
return true;
}
}
这个代码想实现游戏角色的左右移动 但是现在只能实现向右移动,并且按方向左键角色还是向右移动,请教解决办法 展开
3个回答
展开全部
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
by = by+5;
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
by = by-5;
}
});
这里这样改就ok了,不过你这个程序还有好多问题呢。
public void paint(Graphics g){
g.drawImage(img2,0,0,this);
g.drawImage(img3,by,490,this);
g.drawImage(img,580,260,this);
repaint();
}
这里面还要加一个repaint();方法,要不看不出移动的,问题太多改不完,你问的问题我算是解决了,嘿嘿,给分吧,有什么问题可以hi我。
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
by = by+5;
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
by = by-5;
}
});
这里这样改就ok了,不过你这个程序还有好多问题呢。
public void paint(Graphics g){
g.drawImage(img2,0,0,this);
g.drawImage(img3,by,490,this);
g.drawImage(img,580,260,this);
repaint();
}
这里面还要加一个repaint();方法,要不看不出移动的,问题太多改不完,你问的问题我算是解决了,嘿嘿,给分吧,有什么问题可以hi我。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
if((KeyEvent.VK_RIGHT)!=0)是不对的
应该改成 if(evt.getKeyCode()==KeyEvent.VK_RIGHT)
应该改成 if(evt.getKeyCode()==KeyEvent.VK_RIGHT)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询