java 太阳系模型练习3-- 行星运行轨迹练习
package solar;
import util.GameUtil;
import java.awt.*;
/**
-
星球的共同屬性
*/
public class Star {//Star星
Image img;
double x,y;
int width,height;//創(chuàng)建方法 :draw畫(huà);Graphics制圖
public void draw(Graphics g){//畫(huà)圖
g.drawImage(img,(int)x,(int)y,null);}
this(GameUtil.getImage(imgpath),x,y);
//2:創(chuàng)建空構(gòu)造器
public Star() {
}
//4:
public Star(Image img) {
this.img = img;
//這樣也可以
this.width = img.getWidth(null);
this.height = img.getHeight(null);
}
//創(chuàng)建構(gòu)造器 加載星圖片
public Star(Image img, double x, double y) {
this(img);//這樣也可以
//this.img = img;
this.x = x;
this.y = y;
}
//獲取圖片路徑 用這個(gè)
public Star(String imgpath,double x,double 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{//提示增加構(gòu)造器
//行星自己還有特點(diǎn); 行星有橢圓運(yùn)行,長(zhǎng)軸,短軸,速度;圍繞某個(gè)星球運(yùn)行
Star center;//圍繞某個(gè)星球運(yùn)行;center中心
double longAxis;//橢圓長(zhǎng)軸
double shortAxis;//橢圓短軸
double speed;//速度
double degree;//角度
//5:
boolean satellite;//satellite衛(wèi)星//3:這里重寫(xiě)父類方法;Star()父類里有,這里需重寫(xiě);
//5:
public void draw(Graphics g) {
//4: g.drawImage(img, (int) x, (int) y, null);//強(qiáng)制轉(zhuǎn)型
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);
//每次循環(huán)后變量逐漸增加
degree += speed;
*/
move();//調(diào)用
//5:加判斷
if(!satellite){
drawTrace(g);
}}
//4:Trace軌跡
//設(shè)顏色Color c = g.getColor();//原來(lái)的顏色g.setColor(Color.blue);//藍(lán)色g.drawOval((int)x2,(int)y2,(int)width2,(int)height2);//需要強(qiáng)轉(zhuǎn)g.setColor(c);
public void drawTrace(Graphics g){
double x2,y2,width2,height2;//橢圓的變量
//算坐標(biāo)
width2 = longAxis2;
height2 = shortAxis2;
x2 = (center.x+center.width/2) - longAxis;
y2 = (center.y+center.height/2)-shortAxis;}
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);
//4:創(chuàng)建新的方法;move 移動(dòng)
public void move(){
//沿著橢圓軌跡飛行
x = (center.x+center.width/2)+longAxisMath.cos(degree);
y = (center.y+center.height/2)+shortAxisMath.sin(degree);
//每次循環(huán)后變量逐漸增加
degree += speed;
}
//2:給上面屬性創(chuàng)建構(gòu)造器;
public Planet(Star center,String imgpath, double
longAxis, double shortAxis, double speed) {
super(GameUtil.getImage(imgpath));//super上級(jí)}
//5:添加衛(wèi)星后的構(gòu)造器
public Planet(Star center,String imgpath, double longAxis,
double shortAxis, double speed,boolean satellite) {
this(center, imgpath, longAxis, shortAxis, speed);
this.satellite = satellite;
}
//5:創(chuàng)建構(gòu)造器
public Planet(Image img, double x, double y) {
super(img, x, y);
}
//5:創(chuàng)建構(gòu)造器
public Planet(String imgpath, double x, double y) {//直接調(diào)用父類
super(imgpath, x, y);//super上級(jí)
}
}
//---------------------------------------------------------
package solar;
import util.Constant;
import util.GameUtil;
import util.MyFrame;
import java.awt.*;
/**
-
太陽(yáng)系主窗口
*/
public class SolarFrame extends MyFrame {
Image bg = GameUtil.getImage(“images/bg.jpg”);
// 創(chuàng)建太陽(yáng)對(duì)象;調(diào)用Constant常量類;Constant.GAME_WIDTH/2, Constant.GAME_HEIGHT/2:中心點(diǎn)
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:給地球添加衛(wèi)星
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);
//按照離太陽(yáng)的距離從近到遠(yuǎn),它們依次為水星、金星、地球、火星、木星、土星、天王星、海王星。八大行星
//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) {
//5:調(diào)用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);
g.drawImage(bg, 0, 0, null);// 背景
sun.draw(g);// 把背景傳進(jìn)去
earth.draw(g);}
public static void main(String[] args) {
new SolarFrame().launchFrame();}
}
//---------------------------------------
總結(jié)
以上是生活随笔為你收集整理的java 太阳系模型练习3-- 行星运行轨迹练习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: fundamentals of powe
- 下一篇: tpx色卡电子版_潘通色卡电子版Pant