zoukankan      html  css  js  c++  java
  • Java模拟弹球效果(监听键盘)

     1 import java.awt.*;
     2 import java.awt.event.*;
     3 
     4 public class BallGame  extends Frame {
     5     
     6     Image sun = Toolkit.getDefaultToolkit().getImage("sun.jpg");
     7     
     8     double x=200;
     9     double y=200;
    10     boolean left,right,up,down;
    11     public void paint(Graphics  g){
    12         g.drawImage(sun, (int)x, (int)y, null);
    13         if(left){
    14             x = x-10;
    15         }
    16         if(right){
    17             x = x+10;
    18         }
    19         if(up){
    20             y = y-10;
    21         }
    22         if(down){
    23             y = y+10;
    24         }
    25         
    26     }
    27     
    28     void launchFrame(){
    29         setSize(500,400);
    30         setLocation(80, 80);
    31         setBackground(Color.black);
    32         setTitle("火星十一郎");
    33         myEvent();
    34         setVisible(true);
    35         new PaintThread().start();  
    36         addKeyListener(new KeyMonitor());
    37     }
    38     
    39     public static void main(String[] args){  
    40         new BallGame().launchFrame();
    41     }
    42     
    43     class PaintThread extends Thread {
    44         public void run(){
    45             while(true){
    46                 repaint();
    47                 try {
    48                     Thread.sleep(40);
    49                 } catch (InterruptedException e) {
    50                     e.printStackTrace();
    51                 }   //1秒=1000毫秒!
    52             }
    53         }
    54     }
    55     
    56     class  KeyMonitor extends KeyAdapter {
    57         public void keyPressed(KeyEvent e){
    58             if(37==e.getKeyCode()){
    59                 left=true;
    60             }
    61             if(39==e.getKeyCode()){
    62                 right = true;
    63             }
    64             if(38==e.getKeyCode()){
    65                 up = true;
    66             }
    67             if(40==e.getKeyCode()){
    68                 down = true;
    69             }
    70         }
    71         
    72         public void keyReleased(KeyEvent e){
    73             if(37==e.getKeyCode()){
    74                 left=false;
    75             }
    76             if(39==e.getKeyCode()){
    77                 right = false;
    78             }
    79             if(38==e.getKeyCode()){
    80                 up = false;
    81             }
    82             if(40==e.getKeyCode()){
    83                 down = false;
    84             }
    85         }
    86     }
    87     private void myEvent()
    88     {
    89         this.addWindowListener(new WindowAdapter()//窗口监听
    90         {
    91             public void windowClosing(WindowEvent e)
    92             {
    93                 System.exit(0);
    94             }
    95         });
    96     }
    97 }
  • 相关阅读:
    CSS实现雨滴动画效果
    大型网站架构系列:电商网站架构案例
    CSS 不定宽高的垂直水平居中方式总汇
    js中尺寸类样式
    Tiling
    排序二叉树
    算术表达式的转换
    Area
    catch that cow
    R中双表操作学习[转载]
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2989578.html
Copyright © 2011-2022 走看看