【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);
        }
        
    }
    
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值