Java小程序祝國慶快樂

祝大家國慶節(jié)快樂

祝大家國慶節(jié)快樂

上面是用java的JFrame為基礎(chǔ)實(shí)現(xiàn)的效果,下面用面向?qū)ο蟮乃伎挤治?/strong>并實(shí)現(xiàn)此效果:

1)找對象

最開始鼠標(biāo)點(diǎn)擊沖出來的白色圓點(diǎn) Bullet
白色圓點(diǎn)爆炸成一片(包含很多小圓點(diǎn))Piece
主程序類 Guoqing 繼承于 JPanel
程序初始化類執(zhí)行類GuoqingStart 繼承于JFrame

2) 類的設(shè)計, 定義出 類屬性(數(shù)據(jù)模型)

比如

白色圓點(diǎn)類Bullet
|-- int x x坐標(biāo)
|-- int y y坐標(biāo)

3) 界面繪制(利用Java Swing API 繪制界面)

4) 功能算法設(shè)計

原則: 一切功能 都是方法: 動詞就是方法

實(shí)現(xiàn)策略: 將功能映射到數(shù)學(xué)模型, 研究數(shù)據(jù)的變化規(guī)律

比如白色圓點(diǎn)類Bullet與*Piece中各個小圓點(diǎn)的移動,其實(shí)都是坐標(biāo)位置的變化

5) 事件綁定(利用Java Swing API 實(shí)現(xiàn)事件綁定)
比如鼠標(biāo)點(diǎn)擊監(jiān)聽事件

6)完整代碼


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class GuoqingStart extends JFrame {
    Guoqing gq;
    public static void main(String[] args) {
        new GuoqingStart();
    }
    public GuoqingStart() {
        gq=new Guoqing();
        this.addMouseListener(gq);
        this.add(gq);
        new Thread(gq).start();
        this.setTitle("國慶快樂");
        this.setLocation(100, 0);
        this.setSize(1000,700);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
}

class Guoqing extends JPanel implements MouseListener,Runnable{
    Bullet bullet;
    Vector<Bullet> bullets;
    static Vector<Piece> pieces;
    int times=1;
    public Guoqing() {
        bullets=new Vector<Bullet>();
        pieces=new Vector<Piece>();
    }
    public void paint(Graphics g) {
        super.paint(g);
        times++;
        g.setColor(Color.black);
        g.fillRect(0, 0, 2000, 1000);
        g.setColor(Color.lightGray);
        for(int i=0;i<this.bullets.size();i++){
            Bullet b=this.bullets.get(i);
            if(b.isLive){
                b.move();
                g.fillOval(b.x,b.oldY, 15, 15);
            }
        }
        
        for(int i=0;i<this.pieces.size();i++){
            Piece b=this.pieces.get(i);
            if(b.isLive){
                if(b.color==0){
                    g.setColor(Color.red);
                }else if(b.color==1){
                    g.setColor(Color.blue);
                }else if(b.color==2){
                    g.setColor(Color.cyan);
                }else if(b.color==3){
                    g.setColor(Color.green);
                }else{
                    g.setColor(Color.gray);
                }
                if(times%3==0){
                    b.move();
                }
                g.fillOval((int)b.x, (int)b.y, (int)b.w, (int)b.h);
            }
            
        }
    }
    public void mousePressed(MouseEvent e) {
        bullet=new Bullet(e.getX(),e.getY());
        this.bullets.add(bullet);
    }
    public void mouseClicked(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    @Override
    public void run() {
        while(true){
            this.repaint();
            try {
                Thread.sleep(20);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
class Piece{
    double x,y;
    double w=15;
    double h=15;
    double velocityX, velocityY;
    boolean isLive=true;
    int color=0;
    int times=1;
    public Piece(double x,double y,double velocity,double angle,int color) {
        this.x=x;
        this.y=y;
        this.color=color;
        velocityX = velocity * Math.cos(angle);
        velocityY = velocity * Math.sin(angle);
    }
    public void move(){
        velocityX *= 0.75;
        velocityY += 1.0;
        velocityY *= 0.75;
        
        y += velocityY;
        x += velocityX;
        
        times++;
        if(times%7==0){
            this.w-=2;
            this.h-=2;
            if(this.w<=4){
                Guoqing.pieces.remove(this);
            }
        }
    }
}
class Bullet{
    int x;
    int y;
    int oldY=700;
    boolean isLive=true;
    Piece piece;
    Random r=new Random(System.currentTimeMillis());
    public Bullet(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public void move(){
        oldY-=8;
        if(oldY<=y){
            this.isLive=false;
            int color=r.nextInt(4);
            for(int i=0;i<200;i++){
                double angleXy = r.nextDouble() * 2 * Math.PI;
                double angleZ  = r.nextDouble() * 2 * Math.PI;
                double velocity = 72 * Math.cos(angleZ);
                piece=new Piece(x,oldY,velocity, angleXy,color);
                Guoqing.pieces.add(piece);
            }
        }
    }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,030評論 25 709
  • 面向?qū)ο笾饕槍γ嫦蜻^程。 面向過程的基本單元是函數(shù)。 什么是對象:EVERYTHING IS OBJECT(萬物...
    sinpi閱讀 1,220評論 0 4
  • 古人類的故事一直是我感興趣的內(nèi)容,回顧這段我們還不算“人”的歷程,我們今天面對的很多問題就有了一個新的視角。...
    碎葉屋閱讀 718評論 2 3
  • 愛情,能純粹些嗎?結(jié)婚,能簡單些嗎?婚戀是兩個人的事,能不能不要將兩個家庭外加三姑六婆、七叔四伯也扯進(jìn)來嗎? 01...
    未可_Win閱讀 512評論 0 3
  • 每到放學(xué)時間,我家柴柴都會準(zhǔn)時守在門口,翹首等待孩子回家。動物很有靈性的,它不會看時鐘,可它體內(nèi)的生物鐘比...
    靜心漫時光閱讀 379評論 11 9

友情鏈接更多精彩內(nèi)容