日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 太阳系模型练习3-- 行星运行轨迹练习

發布時間:2024/3/24 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 太阳系模型练习3-- 行星运行轨迹练习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

package solar;

import util.GameUtil;

import java.awt.*;

/**

  • 星球的共同屬性
    */
    public class Star {//Star星
    Image img;
    double x,y;
    int width,height;

    //創建方法 :draw畫;Graphics制圖
    public void draw(Graphics g){//畫圖
    g.drawImage(img,(int)x,(int)y,null);

    }
    //2:創建空構造器
    public Star() {
    }
    //4:
    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.img = img;
    this.x = x;
    this.y = y;
    }
    //獲取圖片路徑 用這個
    public Star(String imgpath,double x,double y){

    this(GameUtil.getImage(imgpath),x,y);

    }

    /public Star(Image img, double x, double y, int width, int height) {
    this.img = img;
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    }/
    }
    //-----------------------------------------------------------
    package solar;

import util.GameUtil;

import java.awt.*;

/**

  • Planet行星
    */
    public class Planet extends Star{//提示增加構造器
    //行星自己還有特點; 行星有橢圓運行,長軸,短軸,速度;圍繞某個星球運行
    Star center;//圍繞某個星球運行;center中心
    double longAxis;//橢圓長軸
    double shortAxis;//橢圓短軸
    double speed;//速度
    double degree;//角度
    //5:
    boolean satellite;//satellite衛星

    //3:這里重寫父類方法;Star()父類里有,這里需重寫;
    //5:
    public void draw(Graphics g) {
    //4: g.drawImage(img, (int) x, (int) y, null);//強制轉型
    super.draw(g);
    //5:x , y;移到move類//沿著橢圓軌跡飛行
    /* x = (center.x + center.width / 2) + longAxis * Math.cos(degree);
    y = (center.y + center.height / 2) + shortAxis * Math.sin(degree);
    //每次循環后變量逐漸增加
    degree += speed;
    */
    move();//調用
    //5:加判斷
    if(!satellite){
    drawTrace(g);
    }

    }

    //4:Trace軌跡
    public void drawTrace(Graphics g){
    double x2,y2,width2,height2;//橢圓的變量
    //算坐標
    width2 = longAxis2;
    height2 = shortAxis2;
    x2 = (center.x+center.width/2) - longAxis;
    y2 = (center.y+center.height/2)-shortAxis;

    //設顏色Color c = g.getColor();//原來的顏色g.setColor(Color.blue);//藍色g.drawOval((int)x2,(int)y2,(int)width2,(int)height2);//需要強轉g.setColor(c);

    }
    //4:創建新的方法;move 移動
    public void move(){
    //沿著橢圓軌跡飛行
    x = (center.x+center.width/2)+longAxisMath.cos(degree);
    y = (center.y+center.height/2)+shortAxisMath.sin(degree);
    //每次循環后變量逐漸增加
    degree += speed;
    }
    //2:給上面屬性創建構造器;
    public Planet(Star center,String imgpath, double
    longAxis, double shortAxis, double speed) {
    super(GameUtil.getImage(imgpath));//super上級

    this.center = center;this.x = center.x+longAxis;//this.y = center.y;this.longAxis = longAxis;this.shortAxis = shortAxis;this.speed = speed;this.width = img.getWidth(null);this.height = img.getHeight(null);

    }
    //5:添加衛星后的構造器
    public Planet(Star center,String imgpath, double longAxis,
    double shortAxis, double speed,boolean satellite) {
    this(center, imgpath, longAxis, shortAxis, speed);
    this.satellite = satellite;
    }
    //5:創建構造器
    public Planet(Image img, double x, double y) {
    super(img, x, y);
    }
    //5:創建構造器
    public Planet(String imgpath, double x, double y) {//直接調用父類
    super(imgpath, x, y);//super上級
    }
    }
    //---------------------------------------------------------
    package solar;

import util.Constant;
import util.GameUtil;
import util.MyFrame;

import java.awt.*;

/**

  • 太陽系主窗口
    */
    public class SolarFrame extends MyFrame {
    Image bg = GameUtil.getImage(“images/bg.jpg”);
    // 創建太陽對象;調用Constant常量類;Constant.GAME_WIDTH/2, Constant.GAME_HEIGHT/2:中心點
    Star sun = new Star(“images/sun.jpg”, Constant.GAME_WIDTH/2,Constant.GAME_HEIGHT/2);
    //earth地球
    Planet earth = new Planet(sun,“images/earth.jpg”, 150, 100, 0.1);
    //5:給地球添加衛星
    Planet moon = new Planet(earth,“images/moon.jpg”, 15, 30, 0.07,true);//去掉軌跡

    //5:添加行星 mars火星
    Planet mars = new Planet(sun,“images/Mars.jpg”, 200, 130, 0.09);
    //按照離太陽的距離從近到遠,它們依次為水星、金星、地球、火星、木星、土星、天王星、海王星。八大行星
    //6:水星
    Planet mercury = new Planet(sun,“images/mercury.jpg”, 100, 60, 0.2);
    //6:金星
    Planet venus = new Planet(sun,“images/venus.jpg”, 130, 80, 0.15);
    //6:木星
    Planet jupiter = new Planet(sun,“images/jupiter.jpg”, 220, 160, 0.08);
    //6:土星
    Planet saturn = new Planet(sun,“images/saturn.jpg”, 250, 200, 0.07);
    //6:天王星
    Planet uranus = new Planet(sun,“images/uranus.jpg”, 280, 220, 0.06);
    //6:海王星
    Planet neptune = new Planet(sun,“images/neptune.jpg”, 300, 250, 0.05);

    public void paint(Graphics g) {
    g.drawImage(bg, 0, 0, null);// 背景
    sun.draw(g);// 把背景傳進去
    earth.draw(g);

    //5:調用mars.draw(g);moon.draw(g);//6:mercury.draw(g);venus.draw(g);jupiter.draw(g);saturn.draw(g);uranus.draw(g);neptune.draw(g);

    }
    public static void main(String[] args) {
    new SolarFrame().launchFrame();

    }

}

//---------------------------------------

總結

以上是生活随笔為你收集整理的java 太阳系模型练习3-- 行星运行轨迹练习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。