📄 cubic.java
字号:
// 程序:立体五角锥旋转
// 范例文件:Cubic.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Cubic extends Applet implements Runnable, KeyListener
{
int AppletWidth,AppletHeight;
Image OffScreen;
Graphics drawOffScreen;
Thread pThread;
top p[];
char ctrl = 'R';
public void init()
{
setBackground(Color.black);
addKeyListener(this);
AppletWidth = getSize().width;
AppletHeight = getSize().height;
OffScreen = createImage(AppletWidth,AppletHeight);
drawOffScreen = OffScreen.getGraphics();
p = new top[5];
p[0] = new top(0,100,0,AppletWidth,AppletHeight);
p[1] = new top(100,0,0,AppletWidth,AppletHeight);
p[2] = new top(0,0,-100,AppletWidth,AppletHeight);
p[3] = new top(-100,0,0,AppletWidth,AppletHeight);
p[4] = new top(0,0,100,AppletWidth,AppletHeight);
}
public void start()
{
pThread = new Thread(this);
pThread.start();
}
public void stop()
{
pThread = null;
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
g.drawImage(OffScreen,0,0,this);
}
public void run()
{
int i;
while(true)
{
drawOffScreen.clearRect(0,0,AppletWidth,AppletHeight);
drawOffScreen.setColor(Color.yellow);
drawOffScreen.drawLine((int)(AppletWidth/2),
(int)(AppletHeight/2),(int)p[0].xp,(int)p[0].yp);
drawOffScreen.setColor(Color.white);
for(i = 1; i < 5; i++)
drawOffScreen.drawLine((int)p[0].xp,
(int)p[0].yp,(int)p[i].xp,(int)p[i].yp);
for(i = 1; i < 4; i++)
drawOffScreen.drawLine((int)p[i].xp,
(int)p[i].yp,(int)p[i+1].xp,(int)p[i+1].yp);
drawOffScreen.drawLine((int)p[4].xp,(int)p[4].yp,
(int)p[1].xp,(int)p[1].yp);
for(i=0; i < 5; i++)
p[i].rotate(ctrl);
repaint();
try {
Thread.sleep(50);
}
catch (InterruptedException e) { }
}
}
public void keyTyped(KeyEvent e) { }
public void keyPressed(KeyEvent e)
{
int key;
key = e.getKeyCode();
if(key == KeyEvent.VK_RIGHT)
ctrl = 'R';
else if(key == KeyEvent.VK_LEFT)
ctrl = 'L';
else if(key == KeyEvent.VK_UP)
ctrl = 'U';
else if(key == KeyEvent.VK_DOWN)
ctrl = 'D';
else if(key == KeyEvent.VK_PAGE_UP)
ctrl = 'Q';
else if(key == KeyEvent.VK_PAGE_DOWN)
ctrl = 'A';
}
public void keyReleased(KeyEvent e) {}
}
class top
{
double x;
double y;
double z;
double xp;
double yp;
double a, b, c;
int Xo, Yo;
public top(double x, double y, double z, int Xo, int Yo)
{
this.x = x;
this.y = y;
this.z = z;
xp = 0;
yp = 0;
this.Xo = Xo/2;
this.Yo = Yo/2;
a = 0;
b = 0;
c = 0;
}
public void rotate(char ctrl)
{
int i;
double xt, yt, zt;
switch (ctrl)
{
case 'R':
b++;
break;
case 'L':
b--;
break;
case 'U':
a--;
break;
case 'D':
a++;
break;
case 'Q':
c++;
break;
case 'A':
c--;
break;
}
xt = x*Math.cos(Math.PI/180*b) + z*Math.sin(Math.PI/180*b);
yt = y;
zt = -x*Math.sin(Math.PI/180*b) + z*Math.cos(Math.PI/180*b);
yt = yt*Math.cos(Math.PI/180*a) - zt*Math.sin(Math.PI/180*a);
xp = xt*Math.cos(Math.PI/180*c) - yt*Math.sin(Math.PI/180*c) + Xo;
yp = -xt*Math.sin(Math.PI/180*c) - yt*Math.cos(Math.PI/180*c) + Yo;
if(a > 360 || a < -360)
a = 0;
if(b > 360 || b < -360)
b = 0;
if(c > 360 || c < -360)
c = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -