| 网站首页 | 业界新闻 | 群组 | 人才 | 技术文章 | 下载频道 | 博客 | 代码贴 | 编程论坛
绝地游戏外挂辅助教学千里之行 始于足下
共有 1743 人关注过本帖
标题:键盘监听器应该加到哪?
只看楼主 收藏
啡因
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2014-4-17
结帖率:12.5%
  已结贴   问题点数:10  回复次数:9   
键盘监听器应该加到哪?
我绘制了一个从左向右移动的箭头,现在想利用上下左右键控制它的移动,但是键盘监听器是加到箭头的对象上吗?应该怎么写?
搜索更多相关主题的帖子: 键盘  左右  
2015-11-16 13:37
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:74
帖 子:1390
专家分:5957
注 册:2015-3-19
  得分:0 
代码呢?什么都没有。。。。

like wind.
2015-11-16 16:48
啡因
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: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:74
帖 子:1390
专家分:5957
注 册:2015-3-19
  得分:4 
恩,线程用上了,Brid类没看到所以报错了。。。恩。。。
键盘监听不是这么写的。。。

like wind.
2015-11-16 20:15
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:74
帖 子:1390
专家分:5957
注 册:2015-3-19
  得分:0 
一个实时监听的帖子可以参考一下
https://bbs.bccn.net/thread-459241-1-1.html

like wind.
2015-11-16 20:17
啡因
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
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:74
帖 子:1390
专家分:5957
注 册:2015-3-19
  得分:0 
怎么说呢,你写代码得循序渐进,对象换图片是算法没问题才上的。。。。
恩,现在看你的代码不知道什么目的,运行效果是什么样的。
我先看看吧

like wind.
2015-11-16 20:58
诸葛欧阳
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:流年
等 级:贵宾
威 望:82
帖 子:2786
专家分:14619
注 册:2014-10-16
  得分:4 
直接加到面板上吧

一片落叶掉进了回忆的流年。
2015-11-18 09:01







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

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