1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @summary A utility class for animations -- bounces a bunch of points around in a box
  26  * @summary com.apple.junit.utils
  27  */
  28 
  29 package test.java.awt.regtesthelpers;
  30 
  31 import java.awt.*;
  32 import java.util.Random;
  33 
  34 public class MovingPoints {
  35     private static final Random rand = new Random( 6251966 );
  36     static final int kDefaultSpeed = 15;
  37     private Rectangle box = null;
  38 
  39     int numVertices = 0;
  40     private int[] dx = null;
  41     private int[] dy = null;
  42     private int[] x = null;
  43     private int[] y = null;
  44 
  45     public MovingPoints(Rectangle r, int count) {
  46         this(r, count, kDefaultSpeed);
  47     }
  48 
  49     public MovingPoints(Rectangle r, int count, int speed) {
  50         box = r;
  51         numVertices = count;
  52 
  53         dx = new int[numVertices];
  54         dy = new int[numVertices];
  55         x = new int[numVertices];
  56         y = new int[numVertices];
  57 
  58         for (int i = 0; i < numVertices; i += 1) {
  59             x[i] = rand.nextInt(box.width);
  60             y[i] = rand.nextInt(box.height);
  61             while (dx[i] == 0) {
  62                 dx[i] = rand.nextInt(1 + speed * 2) - speed;
  63             }
  64             while (dy[i] == 0) {
  65                 dy[i] = rand.nextInt(1 + speed * 2) - speed;
  66             }
  67         }
  68     }
  69 
  70     // Bounce the points around
  71     public void move() {
  72         Dimension dim = box.getSize();
  73         for (int i = 0; i < x.length; i += 1) {
  74             x[i] += dx[i];
  75             y[i] += dy[i];
  76             if (x[i] < box.x) {
  77                 x[i] = box.x;
  78                 dx[i] = -dx[i];
  79             }
  80             if (x[i] > dim.width) {
  81                 x[i] = dim.width;
  82                 dx[i] = -dx[i];
  83             }
  84             if (y[i] < box.y) {
  85                 y[i] = box.y;
  86                 dy[i] = -dy[i];
  87             }
  88             if (y[i] > dim.height) {
  89                 y[i] = dim.height;
  90                 dy[i] = -dy[i];
  91             }
  92         }
  93     }
  94 
  95     public int[] getXs() {
  96         return x;
  97     }
  98 
  99     public int[] getYs() {
 100         return y;
 101     }
 102 
 103     public int getNumVertices() {
 104         return numVertices;
 105     }
 106 }