?? cubic2.java
字號(hào):
// 程序:具遠(yuǎn)近感的立體對(duì)象
// 范例文件:Cubic2.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Cubic2 extends Applet implements Runnable, KeyListener
{
int AppletWidth,AppletHeight;
Image OffScreen;
Graphics drawOffScreen;
Thread pThread;
top2 p[];
char ctrl = 'R'; // 預(yù)設(shè)向右旋轉(zhuǎn)
public void init()
{
setBackground(Color.black); // 設(shè)定背景為黑色
addKeyListener(this); // 注冊(cè)鍵盤事件處理
// 取得顯像區(qū)域
AppletWidth = getSize().width;
AppletHeight = getSize().height;
// 建立次畫面
OffScreen = createImage(AppletWidth,AppletHeight);
drawOffScreen = OffScreen.getGraphics();
// 建立并初始化頂點(diǎn)
p = new top2[8];
p[0] = new top2(100,100,100,AppletWidth,AppletHeight);
p[1] = new top2(100,100,-100,AppletWidth,AppletHeight);
p[2] = new top2(-100,100,-100,AppletWidth,AppletHeight);
p[3] = new top2(-100,100,100,AppletWidth,AppletHeight);
p[4] = new top2(100,-100,100,AppletWidth,AppletHeight);
p[5] = new top2(100,-100,-100,AppletWidth,AppletHeight);
p[6] = new top2(-100,-100,-100,AppletWidth,AppletHeight);
p[7] = new top2(-100,-100,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=0; i<=2; 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[i+4].xp,(int)p[i+4].yp,
(int)p[i+5].xp,(int)p[i+5].yp);
drawOffScreen.drawLine((int)p[i].xp,(int)p[i].yp,
(int)p[i+4].xp,(int)p[i+4].yp);
}
drawOffScreen.drawLine((int)p[3].xp,(int)p[3].yp,
(int)p[0].xp,(int)p[0].yp);
drawOffScreen.drawLine((int)p[7].xp,(int)p[7].yp,
(int)p[4].xp,(int)p[4].yp);
drawOffScreen.drawLine((int)p[3].xp,(int)p[3].yp,
(int)p[7].xp,(int)p[7].yp);
for(i=0; i < 8; i++)
p[i].rotate(ctrl); // 旋轉(zhuǎn)頂點(diǎn)
// 重繪畫面
repaint();
// 暫停線程50 毫秒
try {
Thread.sleep(50);
}
catch (InterruptedException e) { }
}
}
//=====實(shí)現(xiàn)KeyListener界面========================================
public void keyTyped(KeyEvent e) { }
public void keyPressed(KeyEvent e)
{
int key;
key = e.getKeyCode();
if(key == KeyEvent.VK_RIGHT) // 按向右鍵,向右旋轉(zhuǎn)
ctrl = 'R';
else if(key == KeyEvent.VK_LEFT) // 按向左鍵,向左旋轉(zhuǎn)
ctrl = 'L';
else if(key == KeyEvent.VK_UP) // 按向上鍵,拉遠(yuǎn)
for(int i=0; i<8; i++)
p[i].n++;
else if(key == KeyEvent.VK_DOWN) // 按向下鍵,拉近
for(int i=0; i<8; i++)
p[i].n--;
else if(key == KeyEvent.VK_PAGE_UP) // 按Page Up鍵,往上拉
for(int i=0; i<8; i++)
p[i].m++;
else if(key == KeyEvent.VK_PAGE_DOWN) // 按Page Down鍵,往下拉
for(int i=0; i<8; i++)
p[i].m--;
}
public void keyReleased(KeyEvent e) {}
}
// 頂點(diǎn)類
class top2
{
double x; // 起始x坐標(biāo)
double y; // 起始y坐標(biāo)
double z; // 起始z坐標(biāo)
double xp; // 旋轉(zhuǎn)后投射于xy平面的x坐標(biāo)
double yp; // 旋轉(zhuǎn)后投射于xy平面的y坐標(biāo)
double b; // 旋轉(zhuǎn)角度
double l; // 攝影機(jī)x坐標(biāo)
double m; // 攝影機(jī)y坐標(biāo)
double n; // 攝影機(jī)z坐標(biāo)
double p; // 消失點(diǎn)
int Xo, Yo;
public top2(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;
// 預(yù)設(shè)觀察點(diǎn)
l = 2;
m = 50;
n = 50;
// 預(yù)設(shè)消失點(diǎn)
p = 100;
b = 0; // 預(yù)設(shè)角度0
}
public void rotate(char ctrl)
{
int i;
double xt, yt, h;
switch (ctrl)
{
// 按向右鍵
case 'R':
b++;
break;
// 按向左鍵
case 'L':
b--;
break;
}
// 套用公式
xt = x * Math.cos(b*Math.PI/180) +
z * Math.sin(b*Math.PI/180) + 1;
yt = y + m;
h = -x * Math.sin(b*Math.PI/180) / p +
z * Math.cos(b*Math.PI/180) / p + n / p + l;
xp = xt / h + Xo;
yp = -yt / h + Yo;
// 避免長時(shí)間執(zhí)行b溢值
if(b > 360 || b < -360)
b = 0;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -