home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-12-07 | 2.2 KB | 93 lines |
- /*
- This class is a simple extention of Canvas that runs as it's own
- thread. It runs a simple animation.
- */
-
- import java.awt.*;
-
- class PolygonAnimation extends Canvas implements Runnable
- {
- private Thread myThread;
- private Polygon thePolygon;
- private int polyX[] = new int[9], //The polygons X coordinates
- polyY[] = new int[9], //The polygons Y coordinates
- position, //The point on the polygon the line is drawn to
- delay = 100;
-
- public PolygonAnimation()
- {
- resize(130, 130);
- setupPolygon();
- thePolygon = new Polygon(polyX, polyY, 9);
- position = 0;
-
- myThread = new Thread(this);
- myThread.start();
- }
-
- public PolygonAnimation(int _delay)
- {
- this();
- if(_delay < 50) delay = 50;
- else if(_delay > 300) delay = 300;
- else delay = _delay;
- }
-
- public void reset() {position = 0;}
-
- public void resume() { myThread.resume(); }
-
- public void run()
- {
- while (true)
- {
- repaint();
- try{ Thread.sleep(delay); } catch(InterruptedException e) { this.stop(); }
- advance();
- }
- }
-
- public void stop() { myThread.suspend(); }
-
- public void destroy() { this.stop(); }
-
- //Draw the polygon and a line from the center to the
- //specified position.
- public void paint(Graphics g)
- {
- g.drawPolygon(thePolygon);
- g.drawLine(65, 65, polyX[position], polyY[position]);
- }
-
- private void advance()
- {
- if(position < 6) position++;
- else position = 0;
- }
-
- //Set up the two arrays with the X and Y coordinate values
- private void setupPolygon()
- {
- polyX[0] = 37;
- polyX[1] = 97;
- polyX[2] = 129;
- polyX[3] = 129;
- polyX[4] = 97;
- polyX[5] = 37;
- polyX[6] = 1;
- polyX[7] = 1;
- polyX[8] = 37;
-
- polyY[0] = 1;
- polyY[1] = 1;
- polyY[2] = 37;
- polyY[3] = 97;
- polyY[4] = 129;
- polyY[5] = 129;
- polyY[6] = 97;
- polyY[7] = 37;
- polyY[8] = 1;
- }
- }
-
-