| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1003 人关注过本帖
标题:双缓冲实现不了望大神指点
只看楼主 加入收藏
信管
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2016-4-9
结帖率:50%
收藏
 问题点数:0 回复次数:0 
双缓冲实现不了望大神指点
import java.awt.*;

import java.awt.event.*;

public class MyChar {

    public static void main(String[] args) {
        Frame w = new Frame() ;
        w.setSize(300 , 400) ;
        
        MyPanel mp = new MyPanel() ;
        w.add(mp) ;
        
        Thread t = new Thread(mp) ;
        t.start() ;
        
        w.addKeyListener(mp) ;
        mp.addKeyListener(mp) ;
        
   
               
               w.show();
                    
                       
              
    }
}

class MyPanel extends Panel implements Runnable , KeyListener{
    private Image ipanel;
    private Graphics gpanel;
    int x[] = new int[10] ;
    int y[] = new int[10] ;
    char c[] = new char[10] ;
    int score = 1000 ;
    MyPanel() {
        for (int i = 0; i < 10; i++) {
            x[i] = (int)(Math.random()*300) ;
            y[i] = (int)(Math.random()*300) ;
            c[i] = (char)(Math.random()*26+97) ;
        }
    }
     
        
    public void paint(Graphics g){
        for(int i = 0 ; i < 10 ; i ++){
            g.drawString(new Character(c[i]).toString(), x[i] , y[i]) ;
        }
        //显示成绩
        g.setColor(Color.RED) ;
        g.drawString("你的成绩是:"+score, 5, 15) ;
    }
         public void update(Graphics g){
            if(ipanel==null){
                ipanel=createImage(this.getSize().width,this.getSize().height);
                gpanel=ipanel.getGraphics();
            }
            gpanel.setColor(getBackground());
            gpanel.fillRect(0, 0, getSize().width, getSize().height);
            paint(gpanel);         
            g.drawImage(ipanel,0,0,this);
        }
    public void run() {
        while(true){
            for (int i = 0; i < 10; i++) {
                y[i] ++ ;
                if(y[i]>400){
                    y[i] = 0 ;
                    x[i] = (int)(Math.random()*300) ;
                    c[i] = (char)(Math.random()*26+97) ;
                    score -= 100 ;
                }
            }
            try{
                Thread.sleep(10) ;
            }catch(Exception e){}
            repaint() ;
        }
    }
    @Override
    public void keyPressed(KeyEvent arg0) {
        char keyC = arg0.getKeyChar() ;
        int nowY = -1 ;
        int nowIndex = -1 ;
        for(int i = 0 ; i < 10 ; i ++){
            if(keyC==c[i]){
                if(y[i]>nowY){
                    nowY = y[i] ;
                    nowIndex = i ;
                }
            }
        }
        
        if(nowIndex!=-1){
            y[nowIndex] = 0 ;
            x[nowIndex] = (int)(Math.random()*300) ;
            c[nowIndex] = (char)(Math.random()*26+97) ;
            score += 10 ;
        }else {
            score -= 100 ;
        }
        
    }
    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
        
    }
}
搜索更多相关主题的帖子: public import 
2016-12-02 21:16
快速回复:双缓冲实现不了望大神指点
数据加载中...
 
   



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

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