home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / TURN.ZIP / Turn.java < prev   
Encoding:
Text File  |  1997-02-17  |  18.3 KB  |  406 lines

  1. // *********************************************************
  2. // * Turn.class                                            *
  3. // *********************************************************
  4. // *********************************************************
  5. // * Beschreibung:                                         *
  6. // * Ein beliebiges Bild (JPG oder GIF (auch transparent)) *
  7. // * wird vor einem Hintergrund um die Y-Achse rotiert     *
  8. // * wird. Der Hintergrund wird entweder mit Farbe         *
  9. // * oder einem Texturbild (das nicht genau den Massen des *
  10. // * Applets entsprechen muss) ausgefuellt.                *
  11. // * Alle Animationsphasen werden im Voraus berechnet,     *
  12. // * das Applet hinreichend schnell ablaeuft.              *
  13. // * Bevor man das Applet in eine Seite einbindet, kann    *
  14. // * man das Offset der Textur (nur auf Wunsch) mit den    *
  15. // * Cursortasten steuern, damit sich die Textur genau mit *
  16. // * einer eventuellen Textur der eigentlichen HTML-Seite  *
  17. // * deckt.                                                *
  18. // *********************************************************
  19. // *********************************************************
  20. // * Autor: Sascha Schneider                               *
  21. // * email: st000991@hrz1.hrz.th-darmstadt.de              *
  22. // * Homepage: http://www.th-darmstadt.de/~st000991        *
  23. // *********************************************************
  24. // ************************************************************
  25. // * Parameter:                                               *
  26. // * offsetcheck (optional)    - wenn dieser Parameter auf    *
  27. // *                             "true" gesetzt wird kann     *
  28. // *                             man die optimale Position    *
  29. // *                             seiner Applet-Hintergrund-   *
  30. // *                             Textur ermittlen             *
  31. // * speed (optional)          - Die Pause zwischen den ein-  *
  32. // *                             zelnen Animationsphasen in   *
  33. // *                             ms.                          *
  34. // * bgimage (optional)        - Das Bild, mit dem der        *
  35. // *                             Hintergrund des Applets      *
  36. // *                             ausgefuellt wird (Textur)    *
  37. // * bgcolor (optional)        - Die Farbe, mit der der       *
  38. // *                             Hintergrund ausgefuellt      *
  39. // *                             werden soll (anstatt Textur) *
  40. // * turnimage                 - Das Bild, das gedreht werden *
  41. // *                             soll.                        *
  42. // * xoffset (optional)        - das Xoffset des Hintergrund- *
  43. // *                             Bildes (negativ!).           *
  44. // * yoffset (optional)        - das Yoffset des Hintergrund- *
  45. // *                             Bildes (negativ!).           *    
  46. // * einzelschritte (optional) - Die Anzahl der einzelnen     *
  47. // *                             Animationsphasen (wird aus   *
  48. // *                             Java-Speicherverwaltungs-    *
  49. // *                             gruenden) automatisch auf 75 *
  50. // *                             beschraenkt.                 *
  51. // ************************************************************
  52. // ************************************************************
  53. // * Fragen und Anregungen bitte per email an mich schicken   *
  54. // * st000991@hrz1.hrz.th-darmstadt.de (Sascha Schneider)     *
  55. // ************************************************************
  56. // ************************************************************
  57. // * Description:                                           *
  58. // * A picture (JPG or GIF (even transparent)) is rotatet   *
  59. // * along its y-axis.    The background of the applet is   *
  60. // * either filled up with a specified image (which doesn┤t *
  61. // * need to fit exactly into the applet window) or with    *
  62. // * a selected Color. All phases of the animation are      *
  63. // * calculated before the Applet starts to maximize        *
  64. // * animation speed. Before putting the applet on a        *
  65. // * special place on the html-page, you case choose the    *
  66. // * x- and y-offset of the backgroundimage (if specified)  *
  67. // * to get the backgroundimage fit with an texture         *
  68. // * putted on the applet-including html-page.              *
  69. // **********************************************************
  70. // **********************************************************
  71. // * Author: Sascha Schneider                               *
  72. // *  email: st000991@hrz1.hrz.th-darmstadt.de              *
  73. // * Homepage: http://www.th-darmstadt.de/~st000991         *
  74. // **********************************************************
  75. // *************************************************************
  76. // * Parameters:                                               *
  77. // * offsetcheck (optional)    - if this parameter is set      *
  78. // *                             to "true", you can change     *
  79. // *                             the position of the back-     *
  80. // *                              groundimage to get the       *
  81. // *                             optimal x- and yoffset        *
  82. // * speed (optional)          - the pause between the         *
  83. // *                             particual phases of animation *
  84. // *                             in ms                         *
  85. // * bgimage (optional)        - the picture for the           *
  86. // *                             backgroundtexture             *
  87. // * bgcolor (optional)        - the desired color for the     *
  88. // *                             background                    *
  89. // * turnimage                 - the picture which will be     *
  90. // *                             rotated                       *
  91. // * xoffset (optional)        - the xoffset of the background *
  92. // *                             picture                       *
  93. // * yoffset (optional)        - the yoffset of the background *
  94. // *                             picture                       *
  95. // * einzelschritte (optional) - the numbers of steps for the  *
  96. // *                             animation to form a half turn *
  97. // *                             this parameter will be        *
  98. // *                             limited to 75 (for java       *
  99. // *                             memory-management reasons)    *
  100. // *************************************************************    
  101. // *************************************************************
  102. // * Pleas give questions and comments via email to            *
  103. // * st000991@hrz1.hrz.th-darmstadt.de (Sascha Schneider)      *
  104. // *************************************************************
  105. // *************************************************************
  106.  
  107. import java.awt.*;
  108. import java.awt.image.*;
  109. import java.applet.*;
  110. import java.lang.*;
  111.  
  112. public class Turn extends Applet implements Runnable {
  113.   boolean offsetcheck;
  114.   Image bufferbild=null;
  115.   Image bgimage=null;
  116.   int bgw, bgh;
  117.   int turnw, turnh;
  118.   int w,h;
  119.   int xoffset;
  120.   int yoffset;
  121.   int xmiddle, ymiddle;
  122.   int speed;
  123.   Thread t=null;
  124.   int[] tpixelarray = null;
  125.   Image bg=null;
  126.   int einzelschritte;
  127.   boolean bgimageangegeben = false;
  128.   boolean bgimagepasst = false;
  129.   Color bgcolor=null;
  130.   int bgcolorint;
  131.   int[][] rpixelarray = null;
  132.   int counter=0;
  133.   
  134.   public void init () {
  135.     Dimension d=size();
  136.     w=d.width;
  137.     h=d.height;
  138.     Image turnimage=null;
  139.     try {this.offsetcheck = (new Boolean(getParameter("offsetcheck"))).booleanValue();}
  140.       catch (NumberFormatException e) { this.offsetcheck = false; } // falls Typumwandlung scheitert setze offsetcheck auf false
  141.     try {this.xoffset = (new Integer(getParameter("xoffset"))).intValue();}
  142.       catch (NumberFormatException e) { this.xoffset = 0; } // falls Typumwandlung scheitert setze xoffset auf 0
  143.     try {this.yoffset = (new Integer(getParameter("yoffset"))).intValue();}
  144.       catch (NumberFormatException e) { this.yoffset = 0; } // falls Typumwandlung scheitert setze yoffset auf 0
  145.     try {this.einzelschritte = (new Integer(getParameter("einzelschritte"))).intValue();}
  146.       catch (NumberFormatException e) { this.einzelschritte = 10; } // falls Typumwandlung scheitert setze einzelschritte auf 10
  147.     if (einzelschritte > 75) einzelschritte = 75;
  148.     try {this.speed = (new Integer(getParameter("speed"))).intValue();}
  149.       catch (NumberFormatException e) { this.speed = 250; } // falls Typumwandlung scheitert setze speed auf 250
  150.     try {this.bgcolor = new Color(Integer.valueOf(getParameter("bgcolor"),16).intValue());}
  151.       catch (NumberFormatException e) { bgcolor = Color.green; } // falls Typumwandlung scheitert setze speed auf gruen
  152.     this.bgcolorint = (255 << 24) | (bgcolor.getRed() << 16) | (bgcolor.getGreen() << 8) | bgcolor.getBlue();
  153.     if (getParameter("bgimage") != null) bgimageangegeben = true;
  154.     int[] bgpixelarray;
  155.     try {
  156.       if (bgimageangegeben) { bgimage=getImage(getDocumentBase(),getParameter("bgimage"));}
  157.       else { bgimage=createImage(w,h); };
  158.       turnimage=getImage(getDocumentBase(),getParameter("turnimage"));
  159.       MediaTracker m = new MediaTracker(this);
  160.       m.addImage(bgimage,0);
  161.       m.addImage(turnimage,1);
  162.       m.waitForID(0);
  163.       m.waitForID(1);
  164.       bgw=bgimage.getWidth(this);
  165.       bgh=bgimage.getHeight(this);
  166.       turnw=turnimage.getWidth(this);
  167.       turnh=turnimage.getHeight(this);
  168.       xoffset%=bgw; // xoffsets in erlaubten Bereich holen
  169.       yoffset%=bgh;
  170.       if (xoffset > 0) xoffset-=bgw;
  171.       if (yoffset > 0) yoffset-=bgh;
  172.       tpixelarray = new int[turnw*turnh];
  173.       PixelGrabber pg = new PixelGrabber(turnimage,0,0,turnw,turnh,tpixelarray,0,turnw);
  174.       pg.grabPixels();
  175.       bgpixelarray = new int[bgw*bgh];                                             
  176.       pg = new PixelGrabber(bgimage,0,0,bgw,bgh,bgpixelarray,0,bgw);
  177.       pg.grabPixels();
  178.     if (!offsetcheck) {
  179.       int[] completebgpixelarray = new int [w*h];
  180.       int i,j,k,l;
  181.       if (bgimageangegeben == false) { // falls kein Hintergrundbild angegeben, fuelle mit Hintergrundfarbe
  182.         for (i=0; (i < w*h); i += 1) { completebgpixelarray[i]=bgcolorint; }
  183.         bg=createImage(new MemoryImageSource(w,h,completebgpixelarray,0,w));
  184.       }
  185.       else {
  186.       // mache mir das hintergrundbild
  187.       if (h >= bgh) {
  188.         //0000 wenns mehr als eine gibt!
  189.         //xxxx 1. Zeile
  190.           if (w >= bgw) {
  191.             bgimagepasst = true;
  192.             //xxxx wenns mehr als eine Spalte gibt
  193.             // mache das erste bild in der ersten zeile (2fach abgeschnitten 1x,1y)
  194.             for (i=-yoffset; (i < bgh); i += 1) {
  195.               for (j=-xoffset; (j < bgw); j += 1) {
  196.                 completebgpixelarray[(i+yoffset)*w+(j+xoffset)]=bgpixelarray[i*bgw+j];
  197.               }
  198.             }
  199.             // mache alle anderen ganzen bilder in der zeile (1fach abgeschnitten - y)
  200.             int xa, ya;
  201.             xa = w - (bgw+xoffset);
  202.             ya = h - (bgh+yoffset);
  203.             xa /= bgw; // restliche anzahl ganze bilder in der Zeile
  204.             ya /= bgh; // restliche anzahl ganze bilder in der Spalte
  205.             for (k = 1; k <= xa; k +=1) {
  206.               for (i = -yoffset; (i < bgh); i +=1) {
  207.                 for (j = 0; (j < bgw); j +=1) {
  208.                   completebgpixelarray[(bgw+xoffset)+(k-1)*bgw+w*(i+yoffset)+j]=bgpixelarray[i*bgw+j];
  209.                 }
  210.               }
  211.             } // for k
  212.           // mache das letzte bild in der ersten zeile (2fach abgeschnitten 1x,1y)
  213.           for (i=-yoffset; (i < bgh); i += 1) {
  214.             for (j=0; (j < (w-(bgw+xoffset+xa*bgw))); j += 1) {
  215.               completebgpixelarray[(bgw+xoffset+xa*bgw)+w*(i+yoffset)+j]=bgpixelarray[i*bgw+j];
  216.             }
  217.           }
  218.           //xxxx mache mir alle dazwischenliegenden zeilen (gibts die?)
  219.             for (l = 1; l <= ya; l +=1) {
  220.             // 1. bild (1fach abgeschnitten - x)
  221.               for (i=0; (i < bgh); i += 1) {
  222.                 for (j=-xoffset; (j < bgw); j += 1) {
  223.                   completebgpixelarray[((l-1)*w*bgh)+(bgh+yoffset)*w+(j+xoffset)+i*w]=bgpixelarray[i*bgw+j];
  224.                 }
  225.               }
  226.             // alle anderen (nicht abgeschnitten)
  227.               for (k = 1; k <= xa; k +=1) {
  228.                 for (i = 0; (i < bgh); i +=1) {
  229.                   for (j = 0; (j < bgw); j +=1) {
  230.                       completebgpixelarray[((l-1)*w*bgh)+(bgh+yoffset)*w+(bgw+xoffset)+(k-1)*bgw+w*i+j]=bgpixelarray[i*bgw+j];
  231.                   }
  232.                 }
  233.               } // for k
  234.             // letztes bild (1fach abgeschnitten - x)
  235.               for (i=0; (i < bgh); i += 1) {
  236.                 for (j=0; (j < (w-(bgw+xoffset+xa*bgw))); j += 1) {
  237.                   completebgpixelarray[((l-1)*w*bgh)+(bgh+yoffset)*w+(bgw+xoffset+xa*bgw)+w*i+j]=bgpixelarray[i*bgw+j];
  238.                 }
  239.               }
  240.             } // for l
  241.           //xxxx mache die letzte zeile 
  242.             // 1. bild (2fach 1x,1y)
  243.                 for (i = 0; (i < (h-(bgh+yoffset+ya*bgh))); i +=1) {
  244.                   for (j = -xoffset; (j < bgw); j +=1) {
  245.                     completebgpixelarray[(bgh+yoffset)*w+(ya*bgh)*w+i*w+(j+xoffset)]=bgpixelarray[i*bgw+j];
  246.                   }
  247.                 }
  248.             // alle anderen (1fach y)
  249.                 for (k = 1; k <= xa; k +=1) {
  250.                   for (i = 0; (i < (h-(bgh+yoffset+ya*bgh))); i +=1) {
  251.                     for (j = 0; (j < bgw); j +=1) {
  252.                       completebgpixelarray[(bgh+yoffset)*w+(ya*bgh)*w+(bgw+xoffset)+(k-1)*bgw+w*i+j]=bgpixelarray[i*bgw+j];
  253.                     }
  254.                   }
  255.                 } // for k
  256.             // letztes bild (2fach 1x,1y)
  257.                 for (i = 0; (i < (h-(bgh+yoffset+ya*bgh))); i +=1) {
  258.                   for (j = 0; (j < (w-(bgw+xoffset+xa*bgw))); j +=1) {
  259.                     completebgpixelarray[(bgh+yoffset)*w+(ya*bgh)*w+(xa*bgw)+(bgw+xoffset)+i*w+j]=bgpixelarray[i*bgw+j];
  260.                   }
  261.                 }
  262.           }
  263.       }
  264.       bg=createImage(new MemoryImageSource(w,h,completebgpixelarray,0,w));
  265.     } 
  266.     }
  267.     } catch (InterruptedException e) {};
  268.     xmiddle=w/2;
  269.     ymiddle=h/2;
  270.     bufferbild=createImage(w,h);
  271.     if (!offsetcheck) { 
  272.       double phi, phistep;
  273.       phi=0;
  274.       phistep=Math.PI/einzelschritte;
  275.       // *******************************************************
  276.       // * Jetzt werden erst mal alle einzelnen Bilder erzeugt *
  277.       // *******************************************************
  278.       rpixelarray= new int[einzelschritte][turnw*turnh];
  279.       int a, b, c;
  280.       double m;
  281.       for    (c = 0; c < einzelschritte; c +=1) {
  282.         m = Math.sin(phi);
  283.         showStatus("Turn Applet: Berechne Frame "+c+"/"+einzelschritte);
  284.         for (a=0; (a < turnw*turnh); a += 1) { rpixelarray[c][a]=(0 << 24) | (0 << 16) | (0 << 8) | 0; }; // erst mal alles transparent ausfuellen
  285.         for (a = 0;(a < turnh); a += 1) {
  286.           for (b = turnw/2;(b >= 0); b -=1) {
  287.             rpixelarray[c][(int)(Math.abs(a*turnw+turnw/2-(m*b)))]=tpixelarray[a*turnw+turnw/2-b];
  288.           }
  289.         }
  290.         for (a = 0;(a < turnh); a += 1) {
  291.           for (b = 0;(b < turnw/2); b +=1) {
  292.             rpixelarray[c][(int)(Math.abs(a*turnw+turnw/2+(m*b)))]=tpixelarray[a*turnw+turnw/2+b];
  293.           }
  294.         }
  295.         phi+=phistep;
  296.         if (phi > (Math.PI)) phi %= Math.PI;
  297.       }
  298.       showStatus("");
  299.       counter=0;
  300.       t = new Thread(this);
  301.       t.start();
  302.       t.suspend();
  303.     }
  304.   }
  305.  
  306.   public void start() {
  307.     if (offsetcheck == false) {
  308.       t.resume();
  309.     }
  310.   }
  311.  
  312.   public void run() {
  313.     if (offsetcheck == false) {
  314.       for ( ; ; ) {
  315.           try {
  316.           repaint();
  317.           Thread.sleep(speed);
  318.           counter+=1;
  319.           if (counter >= 2*einzelschritte-1) counter=0;
  320.         } catch (InterruptedException e) {}
  321.       }
  322.     }
  323.   }
  324.  
  325.   public void stop () {
  326.     if (offsetcheck == false) {
  327.       t.suspend();
  328.     }
  329.   }
  330.  
  331.   public void destroy () {
  332.     if (offsetcheck == false) {
  333.       t.stop();
  334.       t = null;
  335.     }
  336.   }
  337.  
  338.   public void paint (Graphics g) {
  339.     //if (offsetcheck == false) t.suspend();
  340.     Graphics screenretter = null;
  341.     screenretter = g; // Zeiger auf ScreenContext retten
  342.     g=bufferbild.getGraphics(); // male ab jetzt im bufferbild
  343.     if (offsetcheck) {
  344.       g.setColor(Color.white);
  345.       g.fillRect(0,0,w,h);
  346.       g.drawImage(bgimage,xoffset,yoffset,this);
  347.       g.setColor(Color.black);
  348.       g.drawString("xoffset:"+xoffset,10,h/2);
  349.       g.drawString("yoffset:"+yoffset,10,h/2+10);
  350.       g.drawString("offsetcheck:"+offsetcheck,10,h/2+20);
  351.       g.drawString("bgw,bgh:"+bgw+","+bgh,10,h/2+30);
  352.       showStatus("Turn Applet: Offsetcheck aktiviert. Steuere Textur mit Cursortasten.");
  353.     }
  354.     else {
  355.       if ((bgimageangegeben) & (bgimagepasst == false)) { // falls Hintergrundbild zu gross war, muss es eben von Hand gemacht werden
  356.         int a, b;
  357.         for (a=0; a <= 1; a+=1) {
  358.           for (b=0; b <= 1; b+=1) {
  359.             g.drawImage(bgimage,xoffset+a*bgw,yoffset+b*bgh,this);
  360.           }
  361.         }
  362.       } else {
  363.         g.drawImage(bg,0,0,this); // male das fertige hintergrundbild in den ScreenContext
  364.         }
  365.       int helpimagearray[] = new int[turnw*turnh];
  366.       if (counter < einzelschritte)
  367.         for (int s=0; s < (turnw*turnh); s += 1)
  368.           helpimagearray[s] = rpixelarray[counter][s];
  369.       else
  370.         for (int s=0; s < turnh; s += 1)
  371.           for (int u=0; u < turnw; u += 1)
  372.             helpimagearray[s*turnw+u] = rpixelarray[counter-einzelschritte][s*turnw+turnw-u-1];
  373.       Image rimage = createImage(new MemoryImageSource(turnw,turnh,helpimagearray,0,turnw));
  374.       g.drawImage(rimage,xmiddle-turnw/2,ymiddle-turnh/2,this);
  375.     }
  376.     screenretter.drawImage(bufferbild,0,0,this); // male das fertige bufferbild in den ScreenContext
  377.   //if (offsetcheck == false) t.resume();
  378. }
  379.  
  380.   public void update(Graphics g) {
  381.     paint(g);
  382.   }
  383.  
  384.   public boolean keyDown(Event evtObj, int key){
  385.     if (offsetcheck) {
  386.       switch(key) {
  387.         case Event.LEFT : xoffset-=1; break;
  388.         case Event.RIGHT: xoffset+=1; break;
  389.         case Event.DOWN : yoffset+=1; break;
  390.         case Event.UP   : yoffset-=1; break;
  391.         case ' '        : xoffset=0; yoffset=0; break;
  392.       }
  393.       xoffset%=bgw;
  394.       yoffset%=bgh;
  395.       if (xoffset > 0) xoffset-=bgw;
  396.       if (yoffset > 0) yoffset-=bgh;
  397.       repaint();
  398.     }
  399.     return true;
  400.   }
  401.  
  402.   public boolean keyUp(Event evtObj, int key) {
  403.     return true;
  404.   }
  405.  
  406. }