1  /*

  2   * Gary Cornell and Cay S. Horstmann, Core Java (Book/CD-ROM)

  3   * Published By SunSoft Press/Prentice-Hall

  4   * Copyright (C) 1996 Sun Microsystems Inc.

  5   * All Rights Reserved. ISBN 0-13-565755-5

  6   *

  7   * Permission to use, copy, modify, and distribute this 

  8   * software and its documentation for NON-COMMERCIAL purposes

  9   * and without fee is hereby granted provided that this 

 10   * copyright notice appears in all copies. 

 11   * 

 12   * THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR 

 13   * WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER 

 14   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 

 15   * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 

 16   * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS

 17   * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED 

 18   * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING 

 19   * THIS SOFTWARE OR ITS DERIVATIVES.

 20   */

 21   

 22  /**

 23   * @version 1.00 07 Feb 1996 

 24   * @author Cay Horstmann

 25   */

 26  

 27  import java.awt.*;

 28  

 29  public class BounceThread extends Frame

 30  {  public BounceThread()

 31     {  setTitle("BounceThread");

 32        canvas = new Canvas();

 33        add("Center", canvas);

 34        Panel p = new Panel();

 35        p.add(new Button("Start"));

 36        p.add(new Button("Close"));

 37        add("South", p);

 38     }

 39     

 40     public boolean handleEvent(Event evt)

 41     {  if (evt.id == Event.WINDOW_DESTROY) System.exit(0);

 42        return super.handleEvent(evt);

 43     }

 44     

 45     public boolean action(Event evt, Object arg)

 46     {  if (arg.equals("Start"))

 47        {  Ball b = new Ball(canvas);

 48           b.start();

 49        }

 50        else if (arg.equals("Close"))

 51           System.exit(0);

 52        else return super.action(evt, arg);

 53        return true;

 54     }

 55   

 56     public static void main(String[] args)

 57     {  Frame f = new BounceThread();

 58        f.resize(300, 200);

 59        f.show();  

 60     }

 61     

 62     private Canvas canvas;

 63  }

 64  

 65  class Ball extends Thread

 66  {  public Ball(Canvas c) { box = c; }

 67  

 68     public void draw()

 69     {  Graphics g = box.getGraphics();

 70        g.fillOval(x, y, XSIZE, YSIZE);

 71        g.dispose();

 72     }

 73     

 74     public void move()

 75     {  Graphics g = box.getGraphics();

 76        g.setXORMode(box.getBackground());

 77        g.fillOval(x, y, XSIZE, YSIZE);

 78        x += dx;

 79        y += dy;

 80        Dimension d = box.size();

 81        if (x < 0) { x = 0; dx = -dx; }

 82        if (x + XSIZE >= d.width) { x = d.width - XSIZE; dx = -dx; }

 83        if (y < 0) { y = 0; dy = -dy; }

 84        if (y + YSIZE >= d.height) { y = d.height - YSIZE; dy = -dy; }

 85        g.fillOval(x, y, XSIZE, YSIZE);

 86        g.dispose();

 87     }

 88     

 89     public void run()

 90     {  draw();

 91        for (int i = 1; i <= 1000; i++)

 92        {  move();

 93           try { Thread.sleep(5); } catch(InterruptedException e) {}

 94        }

 95     }

 96     

 97     private Canvas box;

 98     private static final int XSIZE = 10;

 99     private static final int YSIZE = 10;

100     private int x = 0;

101     private int y = 0;

102     private int dx = 2;

103     private int dy = 2;   

104  }

105  

106