下面的3个类都在包cn.bjsxt.util中
1,Constant.java 中程序如下:
package cn.bjsxt.util;
public class Constant {
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;
}
2,GameUtil.java 中程序如下:
package cn.bjsxt.util;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class GameUtil {
private GameUtil(){} //工具类将构造函数私有
public static Image GetImage(String
path){
URL u =
GameUtil.class.getClassLoader().getResource(path);
BufferedImage img = null;
try{
img =
ImageIO.read(u);
}catch (IOException e){
e.printStackTrace();
}
return img;
}
}
3,MyFrame.java中程序如下:
package cn.bjsxt.util;
import java.awt.Frame;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//import cn.bjsxt.test.GameFrame.PaintThread;
public class MyFrame extends Frame{
public void launchFrame(){
setSize(Constant.GAME_WIDTH,
Constant.GAME_HEIGHT);
setLocation(100, 100);
setVisible(true);
addWindowListener(new
WindowAdapter(){
@Override
public void
windowClosed(WindowEvent e) {
System.exit(0);
} });
new
PaintThread().start();//启动重画线程
}
class PaintThread extends
Thread{
public void
run(){
while(true){
repaint();
try{
Thread.sleep(200);
}
catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
}
下面的类都在包cn.bjsxt.solar中
4,Planet.java 中程序如下:
package cn.bjsxt.solar;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import cn.bjsxt.util.GameUtil;
public class Planet extends Star {
//除了star中拥有的图片,坐标外,行星沿着椭圆运行:长轴,短轴,速度,角度绕着一个star飞
double longAxis;//长轴
double shortAxis;//短轴
double speed;//速度
double degree;
Star center;
boolean satellite;
public void draw(Graphics g){
//g.drawImage(img, (int)x,
(int)y, null);
super.draw(g);
if (!satellite){
drawTrace(g);
}
move(); }
public void drawTrace(Graphics g){
double ovalX, ovalY, ovalWidth,
ovalHeight;
ovalWidth = longAxis*2;
ovalHeight = shortAxis*2;
ovalX = (center.x +
center.width/2 ) - longAxis;
ovalY = (center.y +
center.height/2) - shortAxis;
Color c = g.getColor();
g.setColor(Color.blue);
g.drawOval((int)ovalX,
(int)ovalY, (int)ovalWidth, (int)ovalHeight);
g.setColor(c);
}
//
center.width/2 center.height/2
public void move(){
//沿着椭圆轨迹飞行
x = (center.x + center.width/2)
+ longAxis*Math.cos(degree);
y = (center.y +
center.height/2) +
shortAxis*Math.sin(degree); degree += speed;
}
public Planet(String imgpath, double
longAxis,
double
shortAxis, double speed, Star center) {
super(GameUtil.GetImage(imgpath));
this.x = center.x +
longAxis;
this.y = center.y;
//this.img =
GameUtil.GetImage(imgpath);
this.longAxis = longAxis;
this.shortAxis =
shortAxis;
this.speed = speed;
this.center = center;
}
//?????理论上来说,应该先有构造基类构造函数的情况,但是上面基类的x,
y,都没有构造???/??//star中有个默认构造函数,理解成直接调用不用参数的默认构造函数了????其实super还是调用了
public Planet(String imgpath, double
longAxis,
double
shortAxis, double speed, Star center, boolean satellite) {
this(imgpath, longAxis,
shortAxis, speed, center);
this.satellite =
satellite;
}
//??????/、/?//下面两个函数没有调用??????/?//下面测试当我注释掉时整个程序是可以运行的
public Planet(Image img, double x, double
y){
super(img, x, y);
}
public Planet(String imgpath, double x, double
y){
super(imgpath, x, y);
}
}
5, star.java 中程序如下:
package cn.bjsxt.solar;
import java.awt.Graphics;
import java.awt.Image;
import cn.bjsxt.util.GameUtil;
public class Star {
Image img;
double x, y;
int width, height;
public void draw(Graphics g){
g.drawImage(img, (int)x,
(int)y, null);
}
public Star(){}
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 = img;
this(img);
this.x = x;
this.y = y;
//this.width =
img.getWidth(null);
//this.height =
img.getHeight(null);
}
public Star(String imgpath, double x, double
y){
this(GameUtil.GetImage(imgpath),
x, y);
}
}
6, solarFrame.java 中程序如下
package cn.bjsxt.solar;
import java.awt.Graphics;
import java.awt.Image;
import cn.bjsxt.util.Constant;
//import cn.bjsxt.test.MyFrame.PaintThread;
import cn.bjsxt.util.GameUtil;
import cn.bjsxt.util.MyFrame;
public class solarFrame extends MyFrame {
Image bg =
GameUtil.GetImage("images/bg4.jpg");
//????有一个疑问,此处Image类怎么可以自动调用???,难道是下面主函数中创建new出solarFrame对象时会自动调用???一般是从主函数开始啊,主函数调用继承自MyFrame的launchFrame()
//该方法中规定了加载窗口的一些内容,包括起始位置,长宽等参数,同时该方法类中调用了 new PaintThread().start();//启动重画线程
//然后在同一个类中调用 class PaintThread extends
Thread,其中有repaint()函数调用,即会自动调用太阳主窗口中下面的paint方法,并在while循环中线程会每隔一段时间调用一次,实现动态效果
Star sun = new Star("images/sun5.jpg",
Constant.GAME_HEIGHT/1.5, Constant.GAME_WIDTH/2.8);
Planet earth = new Planet( "images/planet.jpg",
150, 100, 0.1, sun);
Planet moon = new Planet( "images/planet.jpg",
25, 10, 0.3, earth, true);
Planet mars = new Planet( "images/planet.jpg",
200, 130, 0.2, sun);
public void paint(Graphics g){
g.drawImage(bg,
0, 0, null);
sun.draw(g);
earth.draw(g);
moon.draw(g);
mars.draw(g);
}
public static void main(String[] args){
new
solarFrame().launchFrame();
}
//?????????利用双缓冲技术解决背景屏幕经常闪烁的问题
private Image
offScreenImage = null;
public void
update(Graphics g){
if
(offScreenImage == null)
offScreenImage
= this.createImage(Constant.GAME_WIDTH,
Constant.GAME_HEIGHT);
Graphics
gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage,
0, 0, null);
}
}
运行效果如下: