今天研究的是利用HTML5的Canvas畫(huà)圖來(lái)模擬太陽(yáng)系運(yùn)轉(zhuǎn),首先,在這個(gè)太陽(yáng)系里分為畫(huà)軌道和畫(huà)星球兩個(gè)部分,
成都創(chuàng)新互聯(lián)公司是一家專業(yè)的網(wǎng)站制作公司,提供的服務(wù)包括:品牌網(wǎng)站制作,網(wǎng)站設(shè)計(jì),網(wǎng)頁(yè)設(shè)計(jì),我們是一家專業(yè)網(wǎng)絡(luò)公司,做網(wǎng)站,我們是認(rèn)真的。我們是成都網(wǎng)站制作,成都網(wǎng)站建設(shè)的先行者,一切以客戶的利益為設(shè)計(jì)方向,能夠?yàn)椴煌袠I(yè)的客戶提供全面、長(zhǎng)期、深入的網(wǎng)絡(luò)解決方案。 成都創(chuàng)新互聯(lián)公司根據(jù)客戶的具體需求,提供從策劃、創(chuàng)意、制作、執(zhí)行等服務(wù)。
對(duì)于每一個(gè)星球我們要知道它的顏色和公轉(zhuǎn)周期,如下圖。
采用面向?qū)ο缶幊痰乃枷耄a如下
stars.html
[html] view plain copy
!DOCTYPE HTML
html
head/head
body
canvas id="canvas" width="1000" height="1000" style="background:#000"
你的瀏覽器不支持canvas標(biāo)簽!
/canvas
script src="stars.js"
/script
/body
/html
stars.js
[javascript] view plain copy
/******************************************/
/* */
/* 本節(jié)代碼體現(xiàn)了用JavaScript編寫(xiě)面向?qū)?*/
/* 象程序的思想,希望能認(rèn)真閱讀理解。 */
/* */
/******************************************/
//設(shè)置2d繪圖環(huán)境
var ctx = document.getElementById("canvas").getContext("2d");
//畫(huà)軌道
function drawTrack(){
for(var i = 0; i 8; i++){
ctx.beginPath();
ctx.arc(500, 500, (i + 1) * 50, 0, 360, false);
ctx.closePath();
ctx.strokeStyle = "#fff";
ctx.stroke();
}
}
//畫(huà)星球的類
function Star(x, y, radius, cycle, sColor, eColor){
//設(shè)置星球類的屬性
this.x = x; //星球的坐標(biāo)點(diǎn)
this.y = y;
this.radius = radius; //星球的半徑
this.cycle = cycle; //設(shè)置周期
this.sColor = sColor; //星球的顏色,起始顏色和結(jié)束顏色
this.eColor = eColor;
this.color = null;
//設(shè)置一個(gè)計(jì)時(shí)器
this.time = 0;
//給星球類定義一個(gè)方法
this.draw = function(){
ctx.save(); //保存之前的內(nèi)容
ctx.translate(500, 500); //重置0,0坐標(biāo)
ctx.rotate(this.time * (360 / this.cycle) * Math.PI / 180); //旋轉(zhuǎn)角度
//畫(huà)星球
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, 360, false);
ctx.closePath();
//設(shè)置星球的填充顏色
this.color = ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.radius);
this.color.addColorStop(0, this.sColor);
this.color.addColorStop(1, this.eColor);
ctx.fillStyle = this.color;
ctx.fill();
//恢復(fù)之前畫(huà)布的內(nèi)容
ctx.restore();
this.time += 1;
}
}
//創(chuàng)建一個(gè)太陽(yáng)的構(gòu)造函數(shù)
function Sun(){
Star.call(this, 0, 0, 20, 0, "#FFFF00", "#FF9900");
}
//創(chuàng)建一個(gè)水星的構(gòu)造函數(shù)
function Mercury(){
Star.call(this, 0, -50, 10, 87.70, "#A69697", "#5C3E40");
}
//創(chuàng)建一個(gè)金星的構(gòu)造函數(shù)
function Venus(){
Star.call(this, 0, -100, 10, 224.701, "#C4BBAC", "#1F1315");
}
//創(chuàng)建一個(gè)地球的構(gòu)造函數(shù)
function Earth(){
Star.call(this, 0, -150, 10, 365.2422, "#78B1E8", "#050C12");
}
//創(chuàng)建一個(gè)火星的構(gòu)造函數(shù)
function Mars(){
Star.call(this, 0, -200, 10, 686.98, "#CEC9B6", "#76422D");
}
//創(chuàng)建一個(gè)木星的構(gòu)造函數(shù)
function Jupiter(){
Star.call(this, 0, -250, 10, 4332.589, "#C0A48E", "#322222");
}
//創(chuàng)建一個(gè)土星的構(gòu)造函數(shù)
function Saturn(){
Star.call(this, 0, -300, 10, 10759.5, "#F7F9E3", "#5C4533");
}
//創(chuàng)建一個(gè)天王星的構(gòu)造函數(shù)
function Uranus(){
Star.call(this, 0, -350, 10, 30799.095, "#A7E1E5", "#19243A");
}
//創(chuàng)建一個(gè)海王星的構(gòu)造函數(shù)
function Neptune(){
Star.call(this, 0, -400, 10, 60152, "#0661B2", "#1E3B73");
}
var sun = new Sun();
var mercury = new Mercury();
var venus = new Venus();
var earth = new Earth();
var mars = new Mars();
var jupiter = new Jupiter();
var saturn = new Saturn();
var uranus = new Uranus();
var neptune = new Neptune();
function Move(){
ctx.clearRect(0, 0, 1000, 1000);
drawTrack();
sun.draw();
mercury.draw();
venus.draw();
earth.draw();
mars.draw();
jupiter.draw();
saturn.draw();
uranus.draw();
neptune.draw();
}
setInterval(Move,10);
網(wǎng)上搞本紅寶書(shū)(opengl編程指南)及源碼。
照抄就行了,大概是第3章的一個(gè)例子。哈哈。
Thread.sleep(40); 隔40毫秒重畫(huà)(repaint),這么快,當(dāng)然一閃一閃,你把時(shí)間放長(zhǎng)或者去掉sleep吧
public class Star {
private String name;
private int d;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getD() {
return d;
}
public void setD(int d) {
this.d = d;
}
public String introduce() {
return "Star{" +
"name='" + name + '\'' +
", d=" + d +
'}';
}
}
import?java.awt.Canvas;
import?java.awt.Color;
import?java.awt.Dimension;
import?java.awt.EventQueue;
import?java.awt.Frame;
import?java.awt.Graphics;
import?java.awt.Graphics2D;
import?java.awt.Image;
import?java.awt.RenderingHints;
import?java.awt.event.KeyEvent;
import?java.awt.event.KeyListener;
import?java.awt.event.WindowAdapter;
import?java.awt.event.WindowEvent;
import?java.awt.image.BufferedImage;
import?java.io.File;
import?java.io.IOException;
import?javax.imageio.ImageIO;
public?class?TestImage?extends?Frame
{
private?static?final?long?serialVersionUID?=?1L;
private?static?boolean?PRESSED?=?false;
private?static?int?pointX?=?0;
private?static?int?pointy?=?200;
private?static?int?RIGHT_GO?=?0;
private?static?int?LEFT_GO?=?0;
private?static?int?DIR?=?0;
private?static?int?ANGLE?=?0;
private?static?int?W?=?50;
private?static?int?H?=?60;
private?_Canvas?canvas?=?null;
public?TestImage?()
{
add?(canvas?=?new?_Canvas?());
setIgnoreRepaint?(true);
requestFocus?();
}
public?class?_Canvas?extends?Canvas?implements?Runnable
{
private?static?final?long?serialVersionUID?=?1L;
private?BufferedImage?bi?=?null;
private?Image?bufferedImage?=?null;
private?Thread?thread?=?null;
private?long?sleepTime?=?10;
public?_Canvas?()
{
try
{
bi?=?ImageIO.read?(new?File?("go.png"));
}
catch?(IOException?e)
{}
setBackground?(Color.BLACK);
requestFocus?();
addKeyListener?(new?KeyListener?()
{
@Override
public?void?keyTyped?(?KeyEvent?e?)
{}
@Override
public?void?keyReleased?(?KeyEvent?e?)
{
RIGHT_GO?=?0;
PRESSED?=?false;
}
@Override
public?void?keyPressed?(?KeyEvent?e?)
{
//?38?40?37?39上下左右
DIR?=?e.getKeyCode?();
PRESSED?=?true;
}
});
}
@Override
public?void?paint?(?Graphics?g?)
{
Graphics2D?g2d?=?(Graphics2D)?g;
g2d.setRenderingHint?(RenderingHints.KEY_INTERPOLATION,?RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage?(rotateImage?(bi.getSubimage?(RIGHT_GO,?LEFT_GO,?W,?H),?ANGLE,?true),?pointX,?pointy,?W,?H,
this);
g2d.dispose?();
}
@Override
public?void?update?(?Graphics?g?)
{
if?(null?==?bufferedImage)
{
bufferedImage?=?createImage?(getWidth?(),?getHeight?());
}
Graphics?bufferedG?=?bufferedImage.getGraphics?();
bufferedG.clearRect?(0,?0,?getWidth?(),?getHeight?());
paint?(bufferedG);
bufferedG.dispose?();
g.drawImage?(bufferedImage,?0,?0,?this);
g.dispose?();
}
public?void?start?()
{
thread?=?new?Thread?(this);
thread.setName?("TestImage");
thread.setPriority?(Thread.MIN_PRIORITY);
thread.start?();
}
public?synchronized?void?stop?()
{
thread?=?null;
notify?();
}
@Override
public?void?run?()
{
Thread?me?=?Thread.currentThread?();
while?(thread?==?me??!isShowing?()?||?getSize?().width?==?0)
{
try
{
Thread.sleep?(555);
}
catch?(InterruptedException?e)
{
return;
}
}
while?(thread?==?me??isShowing?())
{
if?(PRESSED)
{
try
{
if?(DIR?==?39)
{
RIGHT_GO?=?RIGHT_GO?+?50;
LEFT_GO?=?0;
pointX?=?pointX?+?1;
if?(pointX??420)
{
ANGLE?=?90;
pointX--;
pointy--;
W?=?60;
H?=?50;
}
if?(RIGHT_GO??50)
{
RIGHT_GO?=?0;
}
}
else?if?(DIR?==?37)
{
pointX?=?pointX?-?1;
RIGHT_GO?=?RIGHT_GO?+?50;
LEFT_GO?=?60;
if?(pointX??0)
{
ANGLE?=?-90;
pointX++;
pointy--;
W?=?60;
H?=?50;
}
if?(RIGHT_GO??50)
{
RIGHT_GO?=?0;
}
}
else?if?(DIR?==?38)
{
W?=?50;
H?=?60;
pointy?=?150;
ANGLE?=?0;
RIGHT_GO?=?100;
}
else?if?(DIR?==?40)
{
W?=?50;
H?=?60;
ANGLE?=?0;
pointy?=?200;
RIGHT_GO?=?0;
}
Thread.sleep?(sleepTime);
repaint?();
}
catch?(InterruptedException?e)
{
break;
}
}
else
{
RIGHT_GO?=?RIGHT_GO?+?50;
LEFT_GO?=?0;
pointX?=?pointX?+?1;
if?(RIGHT_GO??50)
{
RIGHT_GO?=?0;
}
if?(pointX??500)
{
pointX?=?0;
}
try
{
Thread.sleep?(sleepTime);
repaint?();
}
catch?(InterruptedException?e)
{
break;
}
}
}
thread?=?null;
}
}
/**
?*?旋轉(zhuǎn)圖像為指定角度
?*?
?*?@param?degree
?*?@return
?*/
public?static?BufferedImage?rotateImage?(?final?BufferedImage?image,?final?int?angdeg,?final?boolean?d?)
{
int?w?=?image.getWidth?();
int?h?=?image.getHeight?();
int?type?=?image.getColorModel?().getTransparency?();
BufferedImage?img;
Graphics2D?graphics2d;
(?graphics2d?=?(?img?=?new?BufferedImage?(w,?h,?type)?).createGraphics?()?).setRenderingHint?(
RenderingHints.KEY_INTERPOLATION,?RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate?(d???-Math.toRadians?(angdeg)?:?Math.toRadians?(angdeg),?w?/?2,?h?/?2);
graphics2d.drawImage?(image,?0,?0,?null);
graphics2d.dispose?();
return?img;
}
public?static?void?main?(?String[]?args?)
{
EventQueue.invokeLater?(new?Runnable?()
{
@Override
public?void?run?()
{
final?TestImage?ti?=?new?TestImage?();
ti.setSize?(new?Dimension?(500,?300));
ti.setLocationRelativeTo?(null);
ti.addWindowListener?(new?WindowAdapter?()
{
@Override
public?void?windowClosing?(?WindowEvent?e?)
{
System.exit?(0);
}
@Override
public?void?windowDeiconified?(?WindowEvent?e?)
{
ti.canvas.start?();
}
@Override
public?void?windowIconified?(?WindowEvent?e?)
{
ti.canvas.stop?();
}
});
ti.setResizable?(false);
ti.canvas.start?();
ti.setVisible?(true);
}
});
}
}