键盘监听器应该加到哪?
我绘制了一个从左向右移动的箭头,现在想利用上下左右键控制它的移动,但是键盘监听器是加到箭头的对象上吗?应该怎么写?
回复 2楼 林月儿
程序代码:
package HappyBird; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class Stone extends Thread implements KeyListener{ /** * @param args */ private int xdir=5; private int ydir=0; private boolean running = false; private Cage cage = null; Bird[] birds=null; protected int x,y;//初始位置 Image stone = Toolkit.getDefaultToolkit().getImage("arrow.jpg"); public Stone(Cage _cage,int _x,int _y) { cage = _cage; x = _x; y = _y; start(); } public void start(){ running = true; birds=cage.getBirds(); super.start(); } public void halt(){ running = false; } public void run(){ while (running){ move(); try{ sleep(50); } catch (InterruptedException ie){ System.err.println("Thread interrupted"); } cage.repaint(); } } private void move(){ x+=xdir; y+=ydir; if(y>=cage.getSize().height){ y=(int) (cage.getSize().height*Math.random()); } if(x>cage.getSize().width){ y=(int) (cage.getSize().height*Math.random()); x=1; } if (x<0) x=1; //判断是否打中小鸟 for(int i=0;i<birds.length;i++) { if ((birds[i].x+20>=this.x && birds[i].x<=this.x+20) && (birds[i].y+20>=this.y && birds[i].y<=this.y+20) ) { int x=(int) (cage.getSize().width*Math.random()); int y=(int) (cage.getSize().height*Math.random()); birds[i]=new Bird(cage,x,y); this.y = (int) (cage.getSize().width*Math.random()); this.x=1; } } } public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } public void keyPressed(KeyEvent e){ if(e.getKeyCode()==KeyEvent.VK_DOWN){ y+=10; } else if(e.getKeyCode()==KeyEvent.VK_UP){ y--; } else if(e.getKeyCode()==KeyEvent.VK_LEFT){ x--; } else if(e.getKeyCode()==KeyEvent.VK_RIGHT){ x++; } } public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } public void draw (Graphics g){ g.drawImage(stone,x,y,30,40,cage); } public static void main(String[] args) { // TODO Auto-generated method stub } }
程序代码:
package HappyBird; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; @SuppressWarnings("serial") public class Cage extends Frame implements ActionListener{ private Button quit = new Button("Quit"); private Button start = new Button("Start"); private Button stop = new Button("Stop"); /*private Button fire = new Button("Fire");*/ private Bird birds[] = new Bird[10]; private Stone stones = null; Image bird = Toolkit.getDefaultToolkit().getImage("bird.jpg"); Image stone = Toolkit.getDefaultToolkit().getImage("arrow.jpg"); public Cage(){ super("Cage with Birds"); setLayout (new FlowLayout()); add(quit); quit.addActionListener(this); add(start); start.addActionListener(this); add(stop); stop.addActionListener(this); add(stones);stones.addKeyListener(); /*add(fire); fire.addActionListener(this);*/ validate(); setSize(600,600); setVisible(true); for(int i = 0;i<birds.length;i++){ int x = (int)(getSize().width*Math.random()); int y = (int)(getSize().height*Math.random()); birds[i] = new Bird(this,x,y); } int y = (int)(getSize().height*Math.random()); int x = 1; stones = new Stone(this,x, y); } public void actionPerformed(ActionEvent ae){ if(ae.getSource() == stop) { for(int i=0;i<birds.length;i++) birds[i].halt(); stones.halt(); } if(ae.getSource() == start) { for(int i=0;i<birds.length;i++){ int x=birds[i].x; int y=birds[i].y; birds[i].halt(); birds[i] = new Bird(this,birds[i].x,birds[i].y); } int x=stones.x; int y=stones.y; stones.halt(); stones=new Stone(this,x,y); } /*if(ae.getSource() == fire) stones = new Stone(this,stones.x,stones.y);n++;*/ if(ae.getSource() == quit) System.exit(0); } /*public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } public void keyPressed(KeyEvent e){ // TODO Auto-generated method stub int key = e.getKeyCode(); if(e.getKeyCode()==KeyEvent.VK_DOWN){ stones.y++; stones.halt(); stones=new Stone(this,stones.x,stones.y); } else if(e.getKeyCode()==KeyEvent.VK_UP){ stones.y--; } else if(e.getKeyCode()==KeyEvent.VK_LEFT){ stones.x--; } else if(e.getKeyCode()==KeyEvent.VK_RIGHT){ stones.x++; } } public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub }*/ public void paint(Graphics g){ for(int i = 0;i<birds.length;i++) if(birds[i] !=null) birds[i].draw(g); stones.draw(g); } public static void main(String arg[]){ // TODO Auto-generated method stub @SuppressWarnings("unused") Cage table = new Cage(); table.addKeyListener(table); } public Bird[] getBirds(){return birds;}; }