⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cubic.java

📁 java小游戏代码
💻 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();
 
         // 暂停线程50 毫秒
         try {
             Thread.sleep(50);
         }
         catch (InterruptedException e) { }
      }
   }

   //=====实现KeyListener界面========================================
   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)  // 按Page Up键
         ctrl = 'Q';
      else if(key == KeyEvent.VK_PAGE_DOWN)  // 按Page Down键
         ctrl = 'A';
   }

   public void keyReleased(KeyEvent e) {}
}

// 顶点类
class top
{ 
   double x;    // 起始x坐标 
   double y;    // 起始y坐标 
   double z;    // 起始z坐标 
   double xp;   // 旋转后投射于xy平面的x坐标 
   double yp;   // 旋转后投射于xy平面的y坐标 
   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;
        // 按Page Up键
        case 'Q':
            c++;
            break;
        // 按Page Down键
        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);
     // Xo用来平移x坐标
     xp = xt*Math.cos(Math.PI/180*c) - yt*Math.sin(Math.PI/180*c) + Xo;  
     // Yo用来平移y坐标
     yp = -xt*Math.sin(Math.PI/180*c) - yt*Math.cos(Math.PI/180*c) + Yo; 

    // 避免长时间执行a、b、c溢值
    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 + -