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

📄 cubic2.java

📁 JAVA2游戏程序设计源码 可以帮助初学者或者只想改改程序就能用的人
💻 JAVA
字号:
// 程序:具远近感的立体对象 
// 范例文件: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'; 

   public void init()
   {
      setBackground(Color.black); 
 
      addKeyListener(this); 

      AppletWidth = getSize().width;
      AppletHeight = getSize().height;

      OffScreen     = createImage(AppletWidth,AppletHeight);
      drawOffScreen = OffScreen.getGraphics();

      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);  

         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) 
         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) 
         for(int i=0; i<8; i++)
            p[i].m++;
      else if(key == KeyEvent.VK_PAGE_DOWN) 
         for(int i=0; i<8; i++)
            p[i].m--;
   }

   public void keyReleased(KeyEvent e) {}
}

class top2
{ 
   double x;  
   double y;   
   double z;  
   double xp; 
   double yp;  
   double b; 
   double l;  
   double m;  
   double n;  
   double p; 

   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;

      l = 2; 
      m = 50;
      n = 50; 

      p = 100;
      b = 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;


    if(b > 360 || b < -360)
        b = 0;
   }    
}
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -