当前位置: 首页 > news >正文

【140天】尚学堂高淇Java300集视频精华笔记(86-87)

每日反思

我觉得,即便遇到大的生活变故,也要自律。心法是,感性自我的反应不管,理性自我必须培养这个习惯:提醒自己任何事情都不要有用放纵感性自我获得快感的方式抵消痛感的想法,而应该用自律形成的掌控感来抵消,进而回到心情的均值线。具体方法论是,不要改变自己良好的工作、学习、生活习惯,按部就班的做,哪怕质量差点都没事,开始行动,就是解决一切问题的良药。

第86集:太阳系模型Planet对象的运行轨迹

本集知识点

  1. 使用option+command+R可以快速重命名多个变量。

  2. 方法功能要单一,如本例中提取move方法

    package com.test084_087_solar;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    
    import com.test084_087_util.GameUtil;
    
    public class Planet extends Star{
        //除了图片,坐标。行星沿着某个椭圆运行:长轴、短轴、速度。绕着某个Star飞。
        double longAxis;     //椭圆长轴
        double shortAxis;    //椭圆短轴
        double speed;        //飞行的速度
        double degree;
        Star center;        
        
        public Planet(Star center,String imgpath,double longAxis,double shortAxis,double speed){
            super(GameUtil.getImage(imgpath));
            
            this.center = center;
            this.x = center.x + longAxis;
            this.y = center.y;
    
            this.longAxis = longAxis;
            this.shortAxis = shortAxis;
            this.speed = speed;
            this.center = center;        
        } 
        
        public Planet(Image img,double x,double y){
            super(img,x,y);
        }
        
        public void drawTrace(Graphics g){
            double ovalX;
            double ovalY;
            double ovalWidth;
            double ovalHeight;
            
            ovalWidth = longAxis*2;
            ovalHeight = shortAxis*2;
            ovalX = (center.x + center.width/2) - longAxis;
            ovalY = (center.y + center.height/2) - shortAxis;
            
            Color c = g.getColor();
            g.setColor(Color.blue);
            g.drawOval((int)ovalX, (int)ovalY, (int)ovalWidth, (int)ovalHeight);
            g.setColor(c);
        }
        
        
        public void move(){
            x = (center.x + center.width/2) + longAxis*Math.cos(degree);
            y = (center.y + center.height/2) + shortAxis*Math.sin(degree);
            
            degree += speed;
        }
        
        public void draw(Graphics g){
    
            //沿着椭圆轨迹飞行
            super.draw(g);
            drawTrace(g);
            move();
        }
    }
    

第87集:太阳系模型卫星的处理轨迹的处理添加其他行星

项目完整代码

  1. MyFrame

package com.test084_087_util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MyFrame extends Frame {


    /**
     * 加载窗口类 
     */
    public void launchFrame(){ 
        setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
        setLocation(100,100);
        setVisible(true);
        
        new PaintThread().start();
        
        
        
        //下面是一个匿名内部类
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);//参数传负数表示异常结束,传0表示正常结束。
            }
        });
    }

    /**
     * 
     * 定义一个重画窗口的线程类,是一个内部类
     * 
     */
    class PaintThread extends Thread {
        public void run() {
            while(true){
                repaint();
                try {
                    Thread.sleep(40);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } //1s=1000ms
            }
        }
    }

}
  1. GameUtil

package com.test084_087_util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

/**
 * @author wangtao
 *    加载图片用的工具类
 */
public class GameUtil {
    
    private GameUtil(){}//将构造方法私有,防止创建实例
    
    public static Image getImage(String path){
        URL u = GameUtil.class.getClassLoader().getResource(path);
        BufferedImage img = null;
        try {
            img = ImageIO.read(u);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return img;
    }
}
  1. SolarFrame

package com.test084_087_solar;

import java.awt.Graphics;
import java.awt.Image;

import com.test084_087_util.Constant;
import com.test084_087_util.GameUtil;
import com.test084_087_util.MyFrame;

public class SolarFrame extends MyFrame {
    Image bg = GameUtil.getImage("images/bg.jpg");
    Star sun = new Star("images/sun.jpg",Constant.GAME_WIDTH/2,Constant.GAME_HEIGHT/2);
    Planet earth = new Planet(sun,"images/earth.jpg",150,100,0.1);
    Planet moon = new Planet(earth,"images/moon.jpg",30,20,0.3,true);
    Planet Mars = new Planet(sun,"images/Mars.jpg",200,130,0.2);

    
    public void paint(Graphics g){
        g.drawImage(bg,0,0,null);
        sun.draw(g);
        earth.draw(g);
        moon.draw(g);
        Mars.draw(g);
    }
    
    public static void main(String[] args){
         new SolarFrame().launchFrame();
    }
}
  1. Star

package com.test084_087_solar;

import java.awt.Graphics;
import java.awt.Image;

import com.test084_087_util.GameUtil;

public class Star {
    Image img;
    double x,y;
    int width,height;
    
    public void draw(Graphics g){
        g.drawImage(img,(int)x,(int)y,null);    
    }
    
    public Star(){
        
    }
    
    public Star(Image img){
        this.img = img;
        this.width = img.getWidth(null);
        this.height = img.getHeight(null);
    }
    
    public Star(Image img,double x,double y){
        this(img);
        this.x = x;
        this.y = y;
    }
    
    public Star(String imgpath,double x,double y){
        this(GameUtil.getImage(imgpath),x,y);
    }
    
}
  1. Planet

package com.test084_087_solar;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import com.test084_087_util.GameUtil;

public class Planet extends Star{
    //除了图片,坐标。行星沿着某个椭圆运行:长轴、短轴、速度。绕着某个Star飞。
    double longAxis;     //椭圆长轴
    double shortAxis;    //椭圆短轴
    double speed;        //飞行的速度
    double degree;
    Star center;        
    boolean satellite;
    
    public Planet(Star center,String imgpath,double longAxis,double shortAxis,double speed){
        super(GameUtil.getImage(imgpath));
        
        this.center = center;
        this.x = center.x + longAxis;
        this.y = center.y;

        this.longAxis = longAxis;
        this.shortAxis = shortAxis;
        this.speed = speed;
        this.center = center;        
    } 
    
    public Planet(Star center,String imgpath,double longAxis,double shortAxis,double speed,boolean satellite){
        this(center,imgpath,longAxis,shortAxis,speed);
        this.satellite = satellite;    
    }
    
    
    public Planet(Image img,double x,double y){
        super(img,x,y);
    }
    
    public void drawTrace(Graphics g){
        double ovalX;
        double ovalY;
        double ovalWidth;
        double ovalHeight;
        
        ovalWidth = longAxis*2;
        ovalHeight = shortAxis*2;
        ovalX = (center.x + center.width/2) - longAxis;
        ovalY = (center.y + center.height/2) - shortAxis;
        
        Color c = g.getColor();
        g.setColor(Color.blue);
        g.drawOval((int)ovalX, (int)ovalY, (int)ovalWidth, (int)ovalHeight);
        g.setColor(c);
    }
    
    
    public void move(){
        x = (center.x + center.width/2) + longAxis*Math.cos(degree);
        y = (center.y + center.height/2) + shortAxis*Math.sin(degree);
        
        degree += speed;
    }
    
    public void draw(Graphics g){

        //沿着椭圆轨迹飞行
        super.draw(g);
        move();
        if(!satellite){
            drawTrace(g);
        }
        
    }
    
    
}

相关文章:

  • 测试框架Jest
  • 《伟大的计算原理》一交互系统
  • Maven的配置文件pom.xml
  • Spring boot设置启动监听端口
  • 幂等设计
  • 高度不固定时垂直居中
  • HTML内联元素
  • Java四中控制访问符
  • py xrange
  • JSPWiki安装配置及FCKEditor的集成
  • 1163 访问艺术馆
  • 计划任务 at   batch   cron anacron
  • 区块链技术与应用回顾
  • WiFi万能钥匙张发有:WiFi的安全性是第一位的
  • 英国电信推出FTTP和G.fast新试点项目
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • canvas 高仿 Apple Watch 表盘
  • Cookie 在前端中的实践
  • Date型的使用
  • eclipse(luna)创建web工程
  • egg(89)--egg之redis的发布和订阅
  • HTTP中GET与POST的区别 99%的错误认识
  • iOS编译提示和导航提示
  • k8s如何管理Pod
  • React组件设计模式(一)
  • SegmentFault 2015 Top Rank
  • VUE es6技巧写法(持续更新中~~~)
  • Vue.js-Day01
  • windows下mongoDB的环境配置
  • 编写高质量JavaScript代码之并发
  • 分布式事物理论与实践
  • 坑!为什么View.startAnimation不起作用?
  • 我有几个粽子,和一个故事
  • 06-01 点餐小程序前台界面搭建
  • 7行Python代码的人脸识别
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • 继 XDL 之后,阿里妈妈开源大规模分布式图表征学习框架 Euler ...
  • #我与Java虚拟机的故事#连载14:挑战高薪面试必看
  • (4)STL算法之比较
  • (C语言版)链表(三)——实现双向链表创建、删除、插入、释放内存等简单操作...
  • (Java岗)秋招打卡!一本学历拿下美团、阿里、快手、米哈游offer
  • (介绍与使用)物联网NodeMCUESP8266(ESP-12F)连接新版onenet mqtt协议实现上传数据(温湿度)和下发指令(控制LED灯)
  • (三)centos7案例实战—vmware虚拟机硬盘挂载与卸载
  • ****Linux下Mysql的安装和配置
  • ***php进行支付宝开发中return_url和notify_url的区别分析
  • .desktop 桌面快捷_Linux桌面环境那么多,这几款优秀的任你选
  • .net websocket 获取http登录的用户_如何解密浏览器的登录密码?获取浏览器内用户信息?...
  • .NET 服务 ServiceController
  • @Autowired标签与 @Resource标签 的区别
  • @hook扩展分析
  • [ vulhub漏洞复现篇 ] Django SQL注入漏洞复现 CVE-2021-35042
  • [ 数据结构 - C++]红黑树RBTree
  • [ 转载 ] SharePoint 资料
  • [<MySQL优化总结>]
  • [Android] 240204批量生成联系人,短信,通话记录的APK