package bouncing; import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.util.Random; public class Ball { private static final int MAX_X = 750; private static final int MAX_Y = 750; private static final int MAX_RADIUS = 30; private static final int MIN_RADIUS = 5; private static final int dt = 1; private Color kleur; private int x, y; private int radius; private int vx, vy; public Ball() { Random rnd = new Random(); radius = rnd.nextInt(MIN_RADIUS, MAX_RADIUS); x = rnd.nextInt(0, MAX_X-2*radius); y = rnd.nextInt(0, MAX_Y-2*radius); double v = rnd.nextDouble(0, 5); double angle = rnd.nextDouble(0, 2*Math.PI); vx = (int)(v*Math.cos(angle)); vy = (int)(v*Math.sin(angle)); float r = rnd.nextFloat(); float g = rnd.nextFloat(); float b = rnd.nextFloat(); kleur = new Color(r, g, b); System.out.println("nieuwe bal " + (radius*2 + x)); } public Ball(int myX, int myY) { this(); x = myX; y = myY; radius = 1; } public void draw(Graphics g) { g.setColor(kleur); g.drawOval(x, y, radius*2, radius*2); g.fillOval(x, y, radius*2, radius*2); } public void update() { if (x+radius*2+vx*dt>MAX_X || x+vx*dt<0 ) { vx*=-1; } if (y+radius*2+vy*dt>MAX_Y || y+vy*dt<0 ) { vy*=-1; } x += vx*dt; y += vy*dt; } public boolean inArea(Point p) { if (p.x>=x && p.x<=x+radius*2 && p.y>=y && p.y<=y+radius*2) { return true; } else { return false; } } public void grow() { radius += 1; } public void freeze() { vx = 0; vy = 0; } public void unfreeze() { Random rnd = new Random(); double v = rnd.nextDouble(0, 5); double angle = rnd.nextDouble(0, 2*Math.PI); vx = (int)(v*Math.cos(angle)); vy = (int)(v*Math.sin(angle)); } }