import java.awt.*; import java.awt.geom.*; import java.util.Vector; import physics2D.*; public class PoolTable extends Plane2D { protected int width; protected int height; protected Image image; protected Graphics2D graphics2D; protected Color backgroundColor = new Color(0,0,0); public PoolTable(int width, int height, Image image) { super(); g = 0; this.width = width; this.height = height; this.image = image; graphics2D = (Graphics2D) image.getGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); placeBalls(450,300,20); Ball cueBall = new Ball(new Vector2D(105, 300, true), 20); cueBall.setVelocity(new Vector2D(300, 0, true)); add(cueBall); /* cueBall = new Ball(new Vector2D(445, 300, true), 20); cueBall.setVelocity(new Vector2D(0, 0, true)); add(cueBall); cueBall = new Ball(new Vector2D(365, 300, true), 20); cueBall.setVelocity(new Vector2D(0, 0, true)); add(cueBall); cueBall = new Ball(new Vector2D(405, 300, true), 20); cueBall.setVelocity(new Vector2D(0, 0, true)); add(cueBall); */ } protected void placeBalls(int x, int y, int r) { for (int i = 0; i < 8; i++) { for (int j = 0; j <= i; j++) { add(new Ball(new Vector2D(x + i * r * Math.pow(3, .5) * 1.0001, y - r * i * 1.0001 + r * 2 * j * 1.0001, true), r)); } } } public void advanceTime(int increment) { super.advanceTime(increment); Object2D x; Vector2D p; Vector2D v; for (int i = 0; i < objects.size(); i++) { x = (Object2D) objects.get(i); p = x.getPosition(); v = x.getVelocity(); if (p.getXComponent() + x.getRadius() > width) { x.setVelocity(new Vector2D(-v.getXComponent(), v.getYComponent(), true)); x.move(new Vector2D(2 * (width - p.getXComponent() - x.getRadius()), 0, true)); } else if (p.getXComponent() - x.getRadius() < 0) { x.setVelocity(new Vector2D(-v.getXComponent(), v.getYComponent(), true)); x.move(new Vector2D(2 * (x.getRadius() - p.getXComponent()), 0, true)); } p = x.getPosition(); v = x.getVelocity(); if (p.getYComponent() + x.getRadius() > height) { x.setVelocity(new Vector2D(v.getXComponent(), -v.getYComponent(), true)); x.move(new Vector2D(0, 2 * (height - p.getYComponent() - x.getRadius()), true)); } else if (p.getYComponent() - x.getRadius() < 0) { x.setVelocity(new Vector2D(v.getXComponent(), -v.getYComponent(), true)); x.move(new Vector2D(0, 2 * (x.getRadius() - p.getYComponent()), true)); } } } public Image getImage() { graphics2D.setPaint(backgroundColor); graphics2D.fill(new Rectangle(width, height)); Drawable d; for (int i = 0; i < objects.size(); i++) { d = (Drawable) objects.get(i); Shape[] shapes = d.getShapes(); Color[] colors = d.getColors(); for (int j = shapes.length - 1; j >= 0; j--) { graphics2D.setPaint(colors[j]); graphics2D.fill(shapes[j]); } } return image; } }