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