package com.xasmall.test;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
/*
* 游戏窗口类
* @author xasmall
*/
public class MyGameFrame extends Frame{//AWT,swing
private static final long serialVersionUID = 1L;
static double centerx;
static double centery;
static double sunx;
static double suny;
static double width;
static double height;
static int x;
static int y;
Image imagesun=getimage("images/sun.png");
Image imageEarth=getimage("images/Earth.png");
Image imageMercury=getimage("images/Mercury.png");
Image imageVenus=getimage("images/Venus.png");
Image imageUranus=getimage("images/Uranus.png");
Image imageNeptune=getimage("images/Neptune.png");
Image imageJupiter=getimage("images/Jupiter.png");
Image imageSaturn=getimage("images/Saturn.png");
Image imageMars=getimage("images/Mars.png");
static Plent Earth;
static Plent Mercury;//水星
static Plent Mars;//火星
static Plent Neptune;//海王星
static Plent Venus;//金星
static Plent Uranus;//天王星
static Plent Jupiter;//木星
static Plent Saturn;//土星
public MyGameFrame() {
}
public MyGameFrame(double width,double height,double sunx,double suny) {
this();
MyGameFrame.sunx=sunx;
MyGameFrame.suny=suny;
MyGameFrame.centerx=sunx+35;
MyGameFrame.centery=suny+35;
MyGameFrame.width=width;
MyGameFrame.height=height;
}
/*
*
* 加载窗口
*/
public void lanuchFrame() {
setSize((int)width, (int)height);
setLocation(20, 20);
setVisible(true);
new FrameThread().start();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
g.drawImage(imagesun, (int)sunx, (int)suny, null);
plentxy(Earth);
g.drawImage(imageEarth, (int)Earth.x, (int)Earth.y, null);
plentxy(Mercury);
g.drawImage(imageMercury, (int)Mercury.x, (int)Mercury.y, null);
plentxy(Venus);
g.drawImage(imageVenus, (int)Venus.x, (int)Venus.y, null);
plentxy(Mars);
g.drawImage(imageMars, (int)Mars.x, (int)Mars.y, null);
plentxy(Jupiter);
g.drawImage(imageJupiter, (int)Jupiter.x, (int)Jupiter.y, null);
plentxy(Saturn);
g.drawImage(imageSaturn, (int)Saturn.x, (int)Saturn.y, null);
plentxy(Uranus);
g.drawImage(imageUranus, (int)Uranus.x, (int)Uranus.y, null);
plentxy(Neptune);
g.drawImage(imageNeptune, (int)Neptune.x, (int)Neptune.y, null);
}
public void plentxy(Plent plent) {
plent.setDrgee();
plent.setX(centerx);
plent.setY(centery);
}
class FrameThread extends Thread{
@Override
public void run() {
while(true) {
repaint();
try {
Thread.sleep(40);//25帧
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public Image getimage(String path) {
URL url=MyGameFrame.class.getClassLoader().getResource(path);
BufferedImage img=null;
try {
img=ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
public static void main(String[] args) {
MyGameFrame mygame=new MyGameFrame(1500,1000,700,450);
Earth=new Plent(10, 400, 300);
Mercury=new Plent(4,150,120);
Venus=new Plent(6,250,200);
Mars=new Plent(12, 550, 450);
Jupiter=new Plent(15,750,540);
Saturn=new Plent(18,920,700);
Uranus=new Plent(21,1100,820);
Neptune=new Plent(24, 1300, 900);
mygame.lanuchFrame();
}
}
class Plent{
double speed;
double drgee=0;
double x;
double y;
double width;
double height;
public Plent() {
}
public Plent(double speed,double width,double height) {
this();
this.speed=speed/100;
this.height=height;
this.width=width;
}
public double setDrgee() {
return this.drgee+=this.speed;
}
public double setX(double centerx) {
return this.x=(int)(Math.sin(this.drgee)*(width/2)+centerx);
}
public double setY(double centery) {
return this.y=(int)(Math.cos(this.drgee)*(height/2)+centery);
}
}
图片自取:
密码: ksmu