站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题一个不断旋转显示六个面的正方体

一个不断旋转显示六个面的正方体

摘要: 一个不断旋转显示六个面的正方体

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="48" align="left" valign="top"> </td> </tr> </table>
/*



*/

import java.awt.*;

import java.applet.*;

public class Wuerfel extends Applet {

// 8 Eckpunkte 1-8

// mit je 3 Koordinaten 1,2,3

double p[][] = new double[9][4];

int x=1, y=2, z=3;

public void init() {

setBackground(new Color(255,255,255));

//构造图形

p[1][x] = -100; p[1][y] = -100; p[1][z] = -100;

p[2][x] = +100; p[2][y] = -100; p[2][z] = -100;

p[3][x] = +100; p[3][y] = -100; p[3][z] = +100;

p[4][x] = -100; p[4][y] = -100; p[4][z] = +100;

p[5][x] = -100; p[5][y] = +100; p[5][z] = -100;

p[6][x] = +100; p[6][y] = +100; p[6][z] = -100;

p[7][x] = +100; p[7][y] = +100; p[7][z] = +100;

p[8][x] = -100; p[8][y] = +100; p[8][z] = +100;

// 8 - - - - - 7

// / | / |

// 5 - - - - - 6 |

// | | | |

// | 4 - - - -|- 3

// | / | /

// 1 - - - - - 2

}

// Rotationswinkel in rad

double angle_x = 0.01;

double angle_y = 0.0075;

double angle_z = 0.005;

Image buffer;

Graphics2D gBuffer;

public void paint(Graphics g) {

// Double-Buffering

if (buffer==null) {

buffer=createImage(this.getSize().width, this.getSize().height);

gBuffer=(Graphics2D)buffer.getGraphics();

}

gBuffer.clearRect(0,0,this.getSize().width, this.getSize().height);

// Antialiasing

gBuffer.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

gBuffer.drawLine((int)(p[1][x])+200,(int)(p[1][y])+200,(int)(p[2][x])+200,(int)(p[2][y])+200);

gBuffer.drawLine((int)(p[2][x])+200,(int)(p[2][y])+200,(int)(p[3][x])+200,(int)(p[3][y])+200);

gBuffer.drawLine((int)(p[3][x])+200,(int)(p[3][y])+200,(int)(p[4][x])+200,(int)(p[4][y])+200);

gBuffer.drawLine((int)(p[4][x])+200,(int)(p[4][y])+200,(int)(p[1][x])+200,(int)(p[1][y])+200);

gBuffer.drawLine((int)(p[5][x])+200,(int)(p[5][y])+200,(int)(p[6][x])+200,(int)(p[6][y])+200);

gBuffer.drawLine((int)(p[6][x])+200,(int)(p[6][y])+200,(int)(p[7][x])+200,(int)(p[7][y])+200);

gBuffer.drawLine((int)(p[7][x])+200,(int)(p[7][y])+200,(int)(p[8][x])+200,(int)(p[8][y])+200);

gBuffer.drawLine((int)(p[8][x])+200,(int)(p[8][y])+200,(int)(p[5][x])+200,(int)(p[5][y])+200);

gBuffer.drawLine((int)(p[1][x])+200,(int)(p[1][y])+200,(int)(p[5][x])+200,(int)(p[5][y])+200);

gBuffer.drawLine((int)(p[2][x])+200,(int)(p[2][y])+200,(int)(p[6][x])+200,(int)(p[6][y])+200);

gBuffer.drawLine((int)(p[3][x])+200,(int)(p[3][y])+200,(int)(p[7][x])+200,(int)(p[7][y])+200);

gBuffer.drawLine((int)(p[4][x])+200,(int)(p[4][y])+200,(int)(p[8][x])+200,(int)(p[8][y])+200);

g.drawImage (buffer, 0, 0, this);

//线程

try {Thread.sleep(10);}

catch (InterruptedException e) {}

double px, py, pz;

for (int i=1;i<9;i++) {

px = p[i][x];

py = p[i][y];

pz = p[i][z];

// Rotation um x-Achse

p[i][y] = py*Math.cos(angle_x)-pz*Math.sin(angle_x);

p[i][z] = py*Math.sin(angle_x)+pz*Math.cos(angle_x);

py = p[i][y];

pz = p[i][z];

// Rotation um y-Achse

p[i][x] = px*Math.cos(angle_y)+pz*Math.sin(angle_y);

p[i][z] =-px*Math.sin(angle_y)+pz*Math.cos(angle_y);

px = p[i][x];

// Rotation um z-Achse

p[i][x] = px*Math.cos(angle_z)-py*Math.sin(angle_z);

p[i][y] = py*Math.cos(angle_z)+px*Math.sin(angle_z);

}

repaint();

}

public void update(Graphics g) {paint(g);}

}



                  

</td> </tr> <tr>


↑返回目录
前一篇: 接口与抽象类
后一篇: 中英文混合字符截取方法