// Draw a sine curve in a box // extra support to have the box change its size in a sinusoidal pattern // also draws in a funky way - rows of dots import java.lang.Math; import java.lang.Object; import java.awt.*; public class SinBox extends Object { double a, p, ph; // amplitude, period, phase public double sizePhase = .75; // 0..1 public double sizeSpeed; int overlaps, overlapSpacing; int numPoints = 50; Dimension maxSize; Point center; public Rectangle box; public SinBox(int x, int y, double phase, double speed) { a = 1.0; p = 4.0; ph = phase; overlaps = 5; overlapSpacing = 4; sizeSpeed = speed; center = new Point(x, y); maxSize = new Dimension(600, 200); box = new Rectangle(); updateCurve(); } // takes a number between 0 and 1. public void setSize(double size) { box.width = (int)(maxSize.width * size); box.height = (int)(maxSize.height * size); box.x = center.x - box.width / 2; box.y = center.y - box.height / 2; } public void updateCurve() { ph += 0.5; sizePhase += sizeSpeed; double theta = (sizePhase * 2 * Math.PI); setSize((1 + Math.sin(theta)) / 2); } public boolean nearMaximum() { return (maxSize.height - box.height) < maxSize.height / 5; } public void render(Graphics g) { g.setColor(Color.black); int screenX, screenY; double x, y; int xshift; int i; for (i = 0; i < numPoints; i++) { x = i / (double)numPoints; y = a * Math.sin(x * 2 * Math.PI * p + ph); screenX = (int)(x * box.width + box.x); screenY = (int)(((y + 1) / 2) * box.height + box.y); for (xshift = -overlaps*overlapSpacing/2; xshift < overlaps*overlapSpacing/2; xshift += overlapSpacing) g.fillRect(screenX - xshift - 1, screenY - 1, 3, 3); } } }