A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

本帖最后由 蒋映辉 于 2012-5-24 19:51 编辑
  1. package com.tanqiu;


  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.KeyAdapter;
  6. import java.awt.event.KeyEvent;
  7. import java.util.Timer;
  8. import java.util.TimerTask;

  9. import javax.swing.JFrame;



  10. public class PinBall {
  11.         private final int  X_TABLE=300;//设置窗口的宽
  12.         private final int  Y_TABLE=400;//设置窗口的搞
  13.         private final int RACKET_WIDTH=60;//板的宽度
  14.         private final int RACKET_HEIGHT=10;//板的高度

  15.         private int X_Speed=10;
  16.         private int Y_Speed=10;
  17.         private int BALL_SIZE=16;
  18.         private int ballx=10,bally=10;//小球的初始位置
  19.         private int racketx=0;//板的初始x位置
  20.         private final int rackety=300;
  21.         private boolean islose=false;
  22.         Frame f=new Frame();
  23.         MyCanvas drawArea=new MyCanvas();
  24.         Timer t;
  25.         public void init(){
  26.                 drawArea.setPreferredSize(new Dimension(X_TABLE,Y_TABLE));
  27.                
  28.                 KeyAdapter key = new KeyAdapter(){
  29.                         public void keyPressed(KeyEvent ke){
  30.                                 
  31.                                 if(ke.getKeyCode()==KeyEvent.VK_LEFT){
  32.                                        
  33.                                 if(racketx>0)racketx-=10;
  34.                                 }
  35.                                 else if(ke.getKeyCode()==KeyEvent.VK_RIGHT){
  36.                                        
  37.                                         if(racketx+RACKET_WIDTH< X_TABLE) racketx+=10;
  38.                                 }
  39.                         
  40.                         }
  41.                 };
  42.                 f.addKeyListener(key);
  43.                 drawArea.addKeyListener(key);
  44.                 drawArea.setBackground(new Color(20,30,40));
  45.         
  46.                 ActionListener task=new ActionListener(){

  47.                         @Override
  48.                         public void actionPerformed(ActionEvent e) {
  49.                                 // TODO Auto-generated method stub
  50.                                 
  51.                         }
  52.                         
  53.                 };
  54.                  t=new Timer();
  55.                  t.schedule(new TimerTask(){

  56.                         @Override
  57.                         public void run() {
  58.                                 // TODO Auto-generated method stub
  59.                                 if(ballx>X_TABLE-BALL_SIZE*2||ballx<=0) X_Speed=-X_Speed;
  60.                                 if(bally>Y_TABLE-BALL_SIZE*3||bally<=0) Y_Speed=-Y_Speed;
  61.                                 else if((racketx-RACKET_WIDTH<ballx&&ballx<racketx+RACKET_WIDTH)&&(bally>=rackety-BALL_SIZE)) Y_Speed=-Y_Speed;
  62.                                 else if(bally>rackety){
  63.                                         islose=true;
  64.                                         t.cancel();
  65.                                 
  66.                                 }
  67.                                 
  68.                                 ballx+=X_Speed;
  69.                                 bally+=Y_Speed;
  70.                                 drawArea.repaint();
  71.                                 
  72.                                 
  73.                         }
  74.                         
  75.                  }, 100,100);
  76.                  
  77.                 f.setBackground(new Color(20,30,40));
  78.                 f.pack();
  79.                 f.add(drawArea);
  80.                 f.setVisible(true);
  81.                 f.setSize(X_TABLE, Y_TABLE);
  82.                 //f.setDefaultCloseOperation(3);
  83.                
  84.                
  85.                
  86.                
  87.                
  88.         }
  89.         public static void main(String[] args){
  90.                 new PinBall().init();
  91.         }
  92.         
  93.         
  94.         class MyCanvas extends Canvas{
  95.                 public void paint(Graphics g){
  96.                         if(islose==false){
  97.                         g.setColor(new Color(220,100,80));
  98.                         g.fillOval(ballx, bally, BALL_SIZE, BALL_SIZE);
  99.                         g.setColor(new Color(80,80,200));
  100.                         g.fillRect(racketx, rackety, RACKET_WIDTH, RACKET_HEIGHT);
  101.                         }
  102.                         else{
  103.                                 g.setColor(new Color(255,0,0));
  104.                                 g.setFont(new Font("Times",Font.BOLD,30));
  105.                                 g.drawString("您输了", 50, 200);
  106.                         }
  107.                 }
  108.         }
  109.         
  110.         

  111. }
复制代码

评分

参与人数 1技术分 +2 收起 理由
贠(yun)靖 + 2 赞一个!

查看全部评分

2 个回复

倒序浏览
刷新慢是因为 t 执行schedule方法时period的值为100,每100毫秒刷新一次
t.schedule(new TimerTask()..., 100,100);
schedule(TimerTask task, long delay, long period)
回复 使用道具 举报
栗培文 发表于 2012-5-24 20:41
刷新慢是因为 t 执行schedule方法时period的值为100,每100毫秒刷新一次
t.schedule(new TimerTask()...,  ...

那你觉得是该改大或者改小呢   我都试过了  改小的话图片一直在闪  改大的话也不行  你可以试试
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马