Summary of Java Review

Source: Internet
Author: User
Tags cos sin

The moon goes round the earth. Write an application that simulates the moon turning around the earth.

Mainclass.java

Import javax.swing.*;
public class MainClass {
public static void Main (String args[]) {
Sky sky= New Sky (); Constructs a Sky (Tag object)
JFrame frame = new JFrame (); Constructs a framework (form)
Frame.settitle ("The moon Goes Around the earth");
Frame.add (Sky); Place the Sky (label) in the Frame (form)
Frame.setsize (400,300);
Frame.setvisible (TRUE);
Frame.setlocationrelativeto (NULL); Set the position of the form-centered
Frame.setdefaultcloseoperation (Jframe.exit_on_close);
Frame.getcontentpane (). SetBackground (Java.awt.Color.white); Convert the form to a container and set its background color to white
}
}

Earth.java

Import java.awt.*;
Import javax.swing.*;
Import java.awt.event.*;
public class Earth extends JLabel implements ActionListener {//Subclass of Tag class JLabel--portraying the Earth
JLabel Moon; Label class--depicting (showing) the appearance of the moon
Timer timer; Timer
Double pointx[]=new double[360],//double array pointx depicts horizontal coordinates-the moon is relative to the Earth's
Pointy[]=new double[360]; The double array pointy depicts the vertical coordinates-the moon is relative to the Earth
int w=200,h=200,i=0;
Earth () {
Timer=new Timer (20,this); Create timer, ring interval is 20 milliseconds current Earth object for its monitor
SetIcon (New ImageIcon ("earth.jpg")); Set the earth (label) icon to Earth.jpg
Sethorizontalalignment (Swingconstants.center); Set the earth (label) alignment to center
Moon=new JLabel (New ImageIcon ("Moon.jpg"), Swingconstants.center); Construct the Moon (label) object
Moon.setsize (60,60); Set the size of the Moon (label)
Add (moon); The Moon (the label) is placed in the label object depicting the Earth.
pointx[0]=0; The radius of the orbit of the moon H/2
POINTY[0]=H/2;
Double angle=1*math.pi/180; Scale is 1 degrees
for (int i=0;i<359;i++) {//calculates the value of each element in the array-the coordinate point on the circle
Pointx[i+1]=pointx[i]*math.cos (angle)-pointy[i]*math.sin (angle); Circle Center (0,0), first point (0,H/2), Shun time
Pointy[i+1]=pointy[i]*math.cos (angle) +pointx[i]*math.sin (angle); Coordinates after the needle rotates 1 radians
}
for (int i=0;i<360;i++) {
POINTX[I]=0.8*POINTX[I]+W/2; Coordinate zoom panning--Change Circle Center to (W/2,H/2)
POINTY[I]=0.8*POINTY[I]+H/2; Track circle size reduced by 1 time times
}
Timer.start (); Timer Start--fires every 100 milliseconds actionevent
}
public void actionperformed (ActionEvent e) {
I= (i+1)%360; 0~359 Cycle Changes
Moon.setlocation ((int) pointx[i]-30, (int) pointy[i]-30); Sets the position of the Moon Object (label) on the Earth Object (label)
}
}

Sky.java

Import java.awt.*;
Import javax.swing.*;
Import java.awt.event.*;
public class Sky extends JLabel implements ActionListener {//Subclass of Tag class JLabel--depicting the sky
Earth Earth;
Timer timer; Timer
Double pointx[]=new double[360],//double array pointx depicts horizontal coordinates-the Earth's relative to the sky
Pointy[]=new double[360]; The double array pointy depicts the vertical coordinates-the Earth's relative to the sky
int w=400,h=400,i=0;
Sky () {
Timer=new Timer (100,this); Create timer--ring interval is 100 milliseconds-the current Sky object is its monitor
earth = New Earth (); Constructs a globe (actually a label object)
Earth.setsize (200,200); Earth (Label object) size is 200*200
Add (earth); The earth is in the label that portrays the sky (the label can also be a container)
pointx[0]=0; The radius of the Earth's orbit H/2
POINTY[0]=H/2;
Double angle=1*math.pi/180; Scale is 1 degrees-radians
for (int i=0;i<359;i++) {//calculates the value of each element in the array-the coordinate point on the circle
Pointx[i+1]=pointx[i]*math.cos (angle)-pointy[i]*math.sin (angle); Circle Center (0,0), first point (0,H/2), Shun time
Pointy[i+1]=pointy[i]*math.cos (angle) +pointx[i]*math.sin (angle); Coordinates after the needle rotates 1 radians
}
for (int i=0;i<360;i++) {
POINTX[I]=0.5*POINTX[I]+W/2; Coordinate zoom panning--Change Circle Center to (W/2,H/2)
POINTY[I]=0.5*POINTY[I]+H/2; Track circle size reduced by 1 time times
}
Timer.start (); Timer Start--fires every 100 milliseconds actionevent
}
public void actionperformed (ActionEvent e) {
I= (i+1)%360; 0~359 Cycle Changes
Earth.setlocation ((int) pointx[i]-100, (int) pointy[i]-100); Set the Earth Object (label) position on the Sky Object (label)
}
}

Summary of Java Review

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.