Diet3DApplet.java.in 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* $Id: Diet3DApplet.java.in,v 1.4 2008-10-24 05:44:04 rzr Exp $ */
  2. #include "HackJavaCpp.java.in"
  3. import java.awt.*;
  4. import java.applet.Applet;
  5. /**
  6. * @author www.Philippe.COVAL.free.fr
  7. * Copyright and License : http://rzr.online.fr/license.htm
  8. * $Id: Diet3DApplet.java.in,v 1.4 2008-10-24 05:44:04 rzr Exp $
  9. **/
  10. public final class Diet3DApplet
  11. extends Applet
  12. implements Runnable
  13. {
  14. public static Diet3D mRender = null;
  15. int h_ = 200;
  16. int w_ = h_;
  17. Image offi_ = null;
  18. Thread thread_ = null;
  19. final long mspf = 40;// 1 / fps
  20. public Diet3DApplet()
  21. {
  22. //w_ = getSize().width;
  23. //h_ = getHeight(); //.height;
  24. //trace("Applet size="+w_+"*"+h_);
  25. mRender = new Diet3D(w_,h_);
  26. if ( offi_ == null ) offi_ = createImage( w_, h_ );
  27. }
  28. #ifdef FULL
  29. #ifdef WANT_TRACE
  30. public String trace(Object o)
  31. {
  32. //if ( true) return ""; //
  33. String s= (o!=null) ? o.toString() : "null";
  34. MACRO_println(s);
  35. //showStatus(s);
  36. return s;
  37. }
  38. #endif
  39. #endif
  40. public void init()
  41. {
  42. if ( offi_ == null ) offi_ = createImage( w_, h_ );
  43. //try { wait(); } catch (Exception e) { } //!!sync
  44. super.init();
  45. //trace("#{ init");
  46. }
  47. public void start()
  48. {
  49. if ( thread_ == null ) thread_ = new Thread(this);
  50. thread_.start();
  51. }
  52. public void stop() {
  53. thread_.stop();
  54. super.stop();
  55. }
  56. public void destroy() {
  57. if ( ( thread_ != null ) && ( thread_.isAlive()) ) {
  58. thread_.stop();
  59. thread_ = null ;
  60. }
  61. }
  62. public void paint(Graphics g)
  63. {
  64. if ( g != null) { update(g); } //WORKAROUND: possible rim bug?
  65. }
  66. // cant explain why but this shall be update(Graphics)
  67. // and nothing else
  68. public void update(Graphics g)
  69. {
  70. //trace("#{update");
  71. if ( offi_ == null ) offi_ = createImage( w_, h_ );// application
  72. //offi_.getGraphics().fillRect( 0 , 0 , w_ , h_ );
  73. mRender.paint( offi_.getGraphics() );
  74. g.drawImage( offi_ ,0,0 , this);
  75. }
  76. public void run()
  77. {
  78. long time = 0;
  79. //trace("#{run");
  80. //if ( off_ == null ) init();
  81. while( thread_ != null ) {
  82. time = System.currentTimeMillis();
  83. repaint();
  84. mRender.update();
  85. //try { wait(); } catch (Exception e) { } //!!sync
  86. time = ( System.currentTimeMillis() - time );
  87. time = mspf - time ;
  88. if ( time < 0 ) time = 0;
  89. //time = 100;
  90. //trace(""+time);
  91. try {
  92. Thread.currentThread();
  93. Thread.sleep( time ) ; }
  94. catch ( InterruptedException e ) {
  95. //trace("Exeption");
  96. }
  97. }
  98. }
  99. //-------------------------------------------------------------------------
  100. //
  101. // Event Handling - Java 1.0
  102. //
  103. public boolean handleEvent(Event evt)
  104. {
  105. switch ( evt.id ) {
  106. case Event.KEY_PRESS : {
  107. try { handleKeyPressed ( evt.key); }
  108. catch ( Exception e) { }
  109. break ;
  110. }
  111. case Event.KEY_ACTION : {
  112. try { handleKeyPressed ( evt.key); }
  113. catch ( Exception e) { }
  114. break ;
  115. }
  116. }
  117. return super.handleEvent(evt);
  118. }
  119. public void handleKeyPressed(int i) throws Exception
  120. {
  121. switch (i) {
  122. default :
  123. mRender.toggleMode();
  124. }
  125. }
  126. public boolean mouseDrag(Event e, int x , int y)
  127. {
  128. return true;
  129. }
  130. public boolean mouseDown(Event e, int x , int y)
  131. {
  132. mRender.toggleMode();
  133. return true;
  134. }
  135. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  136. public static void main( String[] arg)
  137. {
  138. Diet3DApplet o = new Diet3DApplet();
  139. Frame f = new Frame();
  140. f.resize( o.w_ , o.h_ );
  141. f.add(o);
  142. o.init();
  143. o.start();
  144. o.validate();
  145. o.repaint();
  146. f.show();
  147. //o.stop();
  148. }
  149. }
  150. //#eof "$Id:$"