前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java小程序祝国庆快乐祝大家国庆节快乐

Java小程序祝国庆快乐祝大家国庆节快乐

作者头像
wblearn
发布2018-08-27 17:49:36
4960
发布2018-08-27 17:49:36
举报
文章被收录于专栏:wblearnwblearn

祝大家国庆节快乐

上面是用java的JFrame为基础实现的效果,下面用面向对象的思考分析并实现此效果:

1)找对象

最开始鼠标点击冲出来的白色圆点 Bullet 白色圆点爆炸成一片(包含很多小圆点)Piece 主程序类 Guoqing 继承于 JPanel 程序初始化类执行类GuoqingStart 继承于JFrame

2) 类的设计, 定义出 类属性(数据模型)

比如

白色圆点类Bullet |-- int x x坐标 |-- int y y坐标

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

4) 功能算法设计

原则: 一切功能 都是方法: 动词就是方法

实现策略: 将功能映射到数学模型, 研究数据的变化规律

比如白色圆点类Bullet与*Piece中各个小圆点的移动,其实都是坐标位置的变化

5) 事件绑定(利用Java Swing API 实现事件绑定) 比如鼠标点击监听事件

6)完整代码

代码语言:javascript
复制
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);
            }
        }
    }

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.10.05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档