1. 程式人生 > >java復習總結

java復習總結

per perf 運動 cos white mil etl 地球 對齊方式

月亮繞地球轉。編寫一個應用程序,模擬月亮圍繞地球轉。

MainClass.java

import javax.swing.*;
public class MainClass {
public static void main(String args[]) {
Sky sky= new Sky(); //構造了一個天空(標簽對象)
JFrame frame = new JFrame(); //構造了一個框架(窗體)
frame.setTitle("月亮繞地球轉");
frame.add(sky); //將天空(標簽)置於框架(窗體)裏
frame.setSize(400,300);
frame.setVisible(true);
frame.setLocationRelativeTo(null); //設置窗體的位置--居中
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(java.awt.Color.white); //將窗體轉換為一個容器並設置其背景色為白色
}
}

Earth.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Earth extends JLabel implements ActionListener { //標簽類JLabel的子類--刻畫地球
JLabel moon; //標簽類--刻畫(顯示)月亮的外觀
Timer timer; //計時器
double pointX[]=new double[360], //double型數組pointX刻畫水平坐標-月亮相對於地球的
pointY[]=new double[360]; //double型數組pointY刻畫垂直坐標-月亮相對於地球的
int w=200,h=200,i=0;
Earth() {
timer=new Timer(20,this); //創建timer,振鈴間隔是20毫秒當前Earth對象為其監視器
setIcon(new ImageIcon("earth.jpg")); //設置地球(標簽)的圖標為earth.jpg
setHorizontalAlignment(SwingConstants.CENTER); //設置地球(標簽)對齊方式為居中
moon=new JLabel(new ImageIcon("moon.jpg"),SwingConstants.CENTER); //構造月亮(標簽)對象
moon.setSize(60,60); //設置月亮(標簽)大小
add(moon); //月亮(標簽)放到刻畫地球的標簽對象裏
pointX[0]=0; //月亮運動軌道的半徑h/2
pointY[0]=h/2;
double angle=1*Math.PI/180; //刻度為1度
for(int i=0;i<359;i++) { //計算出數組中各個元素的值--圓上的坐標點
pointX[i+1]=pointX[i]*Math.cos(angle)-pointY[i]*Math.sin(angle); //以圓中心為(0,0),第一個點為(0,h/2),順時
pointY[i+1]=pointY[i]*Math.cos(angle)+pointX[i]*Math.sin(angle); //針旋轉1弧度後的坐標
}
for(int i=0;i<360;i++) {
pointX[i]=0.8*pointX[i]+w/2; //坐標縮放平移--將圓中心變為(w/2,h/2)
pointY[i]=0.8*pointY[i]+h/2; //軌道圓大小縮小1倍
}
timer.start(); //計時器啟動--每隔100毫秒就會觸發ActionEvent
}
public void actionPerformed(ActionEvent e) {
i=(i+1)%360; //0~359循環變化
moon.setLocation((int)pointX[i]-30,(int)pointY[i]-30); //設置moon對象(標簽)在earth對象(標簽)上的位置
}
}

Sky.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Sky extends JLabel implements ActionListener { //標簽類JLabel的子類--刻畫天空
Earth earth;
Timer timer; //計時器
double pointX[]=new double[360], //double型數組pointX刻畫水平坐標-地球相對於天空的
pointY[]=new double[360]; //double型數組pointY刻畫垂直坐標-地球相對於天空的
int w=400,h=400,i=0;
Sky() {
timer=new Timer(100,this); //創建timer--振鈴間隔是100毫秒--當前Sky對象為其監視器
earth = new Earth(); //構造了一個地球(實際上是一個標簽對象)
earth.setSize(200,200); //地球(標簽對象)大小為200*200
add(earth); //地球位於刻畫天空的標簽裏(標簽也可以是容器)
pointX[0]=0; //地球運動軌道的半徑h/2
pointY[0]=h/2;
double angle=1*Math.PI/180; //刻度為1度-弧度
for(int i=0;i<359;i++) { //計算出數組中各個元素的值--圓上的坐標點
pointX[i+1]=pointX[i]*Math.cos(angle)-pointY[i]*Math.sin(angle); //以圓中心為(0,0),第一個點為(0,h/2),順時
pointY[i+1]=pointY[i]*Math.cos(angle)+pointX[i]*Math.sin(angle); //針旋轉1弧度後的坐標
}
for(int i=0;i<360;i++) {
pointX[i]=0.5*pointX[i]+w/2; //坐標縮放平移--將圓中心變為(w/2,h/2)
pointY[i]=0.5*pointY[i]+h/2; //軌道圓大小縮小1倍
}
timer.start(); //計時器啟動--每隔100毫秒就會觸發ActionEvent
}
public void actionPerformed(ActionEvent e) {
i=(i+1)%360; //0~359循環變化
earth.setLocation((int)pointX[i]-100,(int)pointY[i]-100); //設置earth對象(標簽)在sky對象(標簽)上的位置
}
}

技術分享

java復習總結