canvas做太阳系怎样才能让地球绕着太阳转
今天研究的是利用HTML5的Canvas画图来模拟太阳系运转,首先,在这个太阳系里分为画轨道和画星球两个部分,
成都创新互联公司是一家专业的网站制作公司,提供的服务包括:品牌网站制作,网站设计,网页设计,我们是一家专业网络公司,做网站,我们是认真的。我们是成都网站制作,成都网站建设的先行者,一切以客户的利益为设计方向,能够为不同行业的客户提供全面、长期、深入的网络解决方案。 成都创新互联公司根据客户的具体需求,提供从策划、创意、制作、执行等服务。
对于每一个星球我们要知道它的颜色和公转周期,如下图。
采用面向对象编程的思想,代码如下
stars.html
[html] view plain copy
!DOCTYPE HTML
html
head/head
body
canvas id="canvas" width="1000" height="1000" style="background:#000"
你的浏览器不支持canvas标签!
/canvas
script src="stars.js"
/script
/body
/html
stars.js
[javascript] view plain copy
/******************************************/
/* */
/* 本节代码体现了用JavaScript编写面向对 */
/* 象程序的思想,希望能认真阅读理解。 */
/* */
/******************************************/
//设置2d绘图环境
var ctx = document.getElementById("canvas").getContext("2d");
//画轨道
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();
}
}
//画星球的类
function Star(x, y, radius, cycle, sColor, eColor){
//设置星球类的属性
this.x = x; //星球的坐标点
this.y = y;
this.radius = radius; //星球的半径
this.cycle = cycle; //设置周期
this.sColor = sColor; //星球的颜色,起始颜色和结束颜色
this.eColor = eColor;
this.color = null;
//设置一个计时器
this.time = 0;
//给星球类定义一个方法
this.draw = function(){
ctx.save(); //保存之前的内容
ctx.translate(500, 500); //重置0,0坐标
ctx.rotate(this.time * (360 / this.cycle) * Math.PI / 180); //旋转角度
//画星球
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, 360, false);
ctx.closePath();
//设置星球的填充颜色
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();
//恢复之前画布的内容
ctx.restore();
this.time += 1;
}
}
//创建一个太阳的构造函数
function Sun(){
Star.call(this, 0, 0, 20, 0, "#FFFF00", "#FF9900");
}
//创建一个水星的构造函数
function Mercury(){
Star.call(this, 0, -50, 10, 87.70, "#A69697", "#5C3E40");
}
//创建一个金星的构造函数
function Venus(){
Star.call(this, 0, -100, 10, 224.701, "#C4BBAC", "#1F1315");
}
//创建一个地球的构造函数
function Earth(){
Star.call(this, 0, -150, 10, 365.2422, "#78B1E8", "#050C12");
}
//创建一个火星的构造函数
function Mars(){
Star.call(this, 0, -200, 10, 686.98, "#CEC9B6", "#76422D");
}
//创建一个木星的构造函数
function Jupiter(){
Star.call(this, 0, -250, 10, 4332.589, "#C0A48E", "#322222");
}
//创建一个土星的构造函数
function Saturn(){
Star.call(this, 0, -300, 10, 10759.5, "#F7F9E3", "#5C4533");
}
//创建一个天王星的构造函数
function Uranus(){
Star.call(this, 0, -350, 10, 30799.095, "#A7E1E5", "#19243A");
}
//创建一个海王星的构造函数
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);
求用opengl做的 太阳系的程序 太阳系的几个星球都有rendering的。而且他们都能转起来
网上搞本红宝书(opengl编程指南)及源码。
照抄就行了,大概是第3章的一个例子。哈哈。
java大神帮我看下太阳系代码,为什么运行起来画面一闪一闪的,怎么才能正常不闪啊?
Thread.sleep(40); 隔40毫秒重画(repaint),这么快,当然一闪一闪,你把时间放长或者去掉sleep吧
java程序问题?
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 +
'}';
}
}
求java做动画代码
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;
}
}
/**
* 旋转图像为指定角度
*
* @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);
}
});
}
}
当前题目:java太阳系代码 太阳系的代码
网页地址:http://scgulin.cn/article/hheidc.html