| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1995 人关注过本帖
标题:键盘监听器应该加到哪?
取消只看楼主 加入收藏
啡因
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2014-4-17
结帖率:12.5%
收藏
已结贴  问题点数:10 回复次数:3 
键盘监听器应该加到哪?
我绘制了一个从左向右移动的箭头,现在想利用上下左右键控制它的移动,但是键盘监听器是加到箭头的对象上吗?应该怎么写?
搜索更多相关主题的帖子: 键盘 左右 
2015-11-16 13:37
啡因
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2014-4-17
收藏
得分:0 
回复 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;};
}
2015-11-16 19:49
啡因
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2014-4-17
收藏
得分:0 
回复 2楼 林月儿
我想实现Stone对象的移动,贴了相关的代码,我看了很多例子,还是不太清楚键盘监听的用法
2015-11-16 19:51
啡因
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2014-4-17
收藏
得分:0 
回复 6楼 林月儿
程序代码:
package HappyBird;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
public class Bird extends Thread{
    private int xdir = 2*(1-2*(int)Math.round(Math.random()));
    private int ydir = 2*(1-2*(int)Math.round(Math.random()));
    private  boolean running = false;
    private Cage cage = null;
    protected int x,y;
    Image bird = Toolkit.getDefaultToolkit().getImage("bird.jpg");
    public Bird(Cage _cage,int _x,int _y)
    {
        cage = _cage;
        x = _x;
        y = _y;
        start();
    }
    public void start(){
        running = true;
        super.start();
    }
    public void halt(){
        running = false;
    }
    public void run(){
        while (running){
            move();
            try{
                sleep(120);
            }
            catch (InterruptedException ie){
                System.err.println("Thread interrupted");
            }
            cage.repaint();
            
        }
    }
    private void move(){
        x+=xdir;
        y+=ydir;
    if(x>cage.getSize().width){
        x = cage.getSize().width;
        xdir *= (-1);
    }
    if(x<0)
        xdir *= (-1);
    if(y>cage.getSize().height){
        y = cage.getSize().height;
        ydir *= (-1);
    }
    if(y<0)
        ydir *= (-1);
    }
    public void draw (Graphics g){
        g.drawImage(bird,x,y,30,40,cage);
    }
}
2015-11-16 20:29
快速回复:键盘监听器应该加到哪?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042201 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved