Skip to Content

Generate Bouncing Lines Using Applet Example

  • strict warning: Non-static method view::load() should not be called statically in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_argument.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter_boolean_operator.inc on line 0.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_style_default.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.

Submitted By: Sreenandu

  1. /*
  2.         Generate Bouncing Lines Using Applet Example
  3.         This Java example shows how to create bouncing lines using java Applet
  4.         example.
  5. */
  6.  
  7. import java.awt.*;
  8. import java.applet.Applet;
  9.  
  10. public class BouncingLines extends Applet implements Runnable {
  11.  
  12.     Thread runner = null;
  13.  
  14.     final static int WIDTH  = 200;
  15.     final static int HEIGHT = 100;
  16.  
  17.     Image    image;
  18.     Graphics graphics;
  19.  
  20.     // bouncing lines member variables
  21.     int[] x1;
  22.     int[] y1;
  23.     int[] x2;
  24.     int[] y2;
  25.  
  26.     int dx1 = 2 + (int)( 3 * Math.random() );
  27.     int dy1 = 2 + (int)( 3 * Math.random() );
  28.     int dx2 = 2 + (int)( 3 * Math.random() );
  29.     int dy2 = 2 + (int)( 3 * Math.random() );
  30.     static int first = 0;
  31.     final static int LINES = 50;
  32.  
  33.     public void init() {
  34.  
  35.         // create arrays to hold the line coordinates
  36.         x1 = new int[LINES];
  37.         y1 = new int[LINES];
  38.         x2 = new int[LINES];
  39.         y2 = new int[LINES];
  40.  
  41.         // initialise the first line
  42.         x1[0] = (int)( WIDTH  * Math.random() );
  43.         y1[0] = (int)( HEIGHT * Math.random() );
  44.         x2[0] = (int)( WIDTH  * Math.random() );
  45.         y2[0] = (int)( HEIGHT * Math.random() );
  46.  
  47.         // initialise all the other lines
  48.        for ( int i = 1; i < LINES; i++ ) {
  49.  
  50.             x1[i] = x1[0];
  51.             y1[i] = y1[0];
  52.             x2[i] = x2[0];
  53.             y2[i] = y2[0];
  54.         }
  55.         image    = createImage( WIDTH, HEIGHT );
  56.         graphics = image.getGraphics();
  57.     }
  58.  
  59.  
  60.     public void start() {
  61.  
  62.         // user visits the page, create a new thread
  63.        if ( runner == null ) {
  64.  
  65.             runner = new Thread( this );
  66.             runner.start();
  67.         }
  68.     }
  69.  
  70.  
  71.     public void stop() {
  72.  
  73.         // user leaves the page, stop the thread
  74.        if ( runner != null && runner.isAlive() )
  75.             runner.stop();
  76.  
  77.         runner = null;
  78.     }
  79.  
  80.  
  81.     public void run() {
  82.  
  83.         while (runner != null) {
  84.  
  85.             repaint();
  86.  
  87.             try {
  88.  
  89.                 Thread.sleep( 20 );
  90.  
  91.             } catch ( InterruptedException e ) {
  92.  
  93.                 // do nothing
  94.             }
  95.         }
  96.     }
  97.  
  98.  
  99.     public void paint( Graphics g ) {
  100.  
  101.         update( g );
  102.     }
  103.  
  104.  
  105.     public void update( Graphics g ) {
  106.  
  107.         // clear the background to white
  108.  
  109.         graphics.setColor( Color.black );
  110.         graphics.fillRect( 0, 0, WIDTH, HEIGHT );
  111.  
  112.         // draw the lines
  113.         for(int r=4;r<=9;r++)
  114.         {
  115.                
  116.                 graphics.setColor( Color.green );
  117.        
  118.                 int line = first;
  119.        
  120.                 for ( int i = 0; i < LINES; i++ ) {
  121.        
  122.                     graphics.drawLine( x1[line], y1[line],
  123.                                        x2[line], y2[line] );
  124.                     line++;
  125.                     if ( line == LINES ) line = 0;
  126.                 }
  127.        
  128.                 line = first;
  129.        
  130.                 first--;
  131.        
  132.                 if ( first < 0 ) first = LINES - 1;
  133.        
  134.                 x1[first] = x1[line];
  135.                 y1[first] = y1[line];
  136.                 x2[first] = x2[line];
  137.                 y2[first] = y2[line];
  138.        
  139.                 // move the "first" line
  140.        
  141.                 if (x1[first] + dx2 < WIDTH)
  142.                     x1[first] += dx1;
  143.                 else
  144.                     dx1 = -(2 + (int)( 3 * Math.random() ));
  145.        
  146.                 if (x1[first] + dx1 >= 0)
  147.                     x1[first] += dx1;
  148.                 else
  149.                     dx1 = 2 + (int)( 3 * Math.random() );
  150.        
  151.                 if (y1[first] + dy1 < HEIGHT)
  152.                     y1[first] += dy1;
  153.                 else
  154.                     dy1 = -(2 + (int)( 3 * Math.random() ));
  155.        
  156.                 if (y1[first] + dy1 >= 0)
  157.                     y1[first] += dy1;
  158.                 else
  159.                     dy1 = 2 + (int)( 3 * Math.random() );
  160.        
  161.                 if (x2[first] + dx2 < WIDTH)
  162.                     x2[first] += dx2;
  163.                 else
  164.                     dx2 = -(2 + (int)( 3 * Math.random() ));
  165.        
  166.                 if (x2[first] + dx2 >= 0)
  167.                     x2[first] += dx2;
  168.                 else
  169.                     dx2 = 2 + (int)( 3 * Math.random() );
  170.        
  171.                 if (y2[first] + dy2 < HEIGHT)
  172.                     y2[first] += dy2;
  173.                 else
  174.                     dy2 = -(2 + (int)( 3 * Math.random() ));
  175.        
  176.                 if (y2[first] + dy2 >= 0)
  177.                     y2[first] += dy2;
  178.                 else
  179.                     dy2 = 2 + (int)( 3 * Math.random() );
  180.        
  181.                 // copy buffer to screen
  182.                 g.drawImage( image, 0, 0, this );
  183.             }
  184.     }
  185. }