// mas964 ps2 // draw lines on mouse click that demonstrate rhythm import java.applet.*; import java.awt.*; import java.lang.Math; import DoubleBufferApplet; public class LineRhythm extends DoubleBufferApplet { protected int a1, b1, a2, b2, a3, b3, a4, b4; // moduli for lines public void randomizeConstants() { a1 = (int)(Math.random() * 9) + 2; // 2..10 b1 = (int)(Math.random() * (a1 - 1)) + 1; // 1..a-1 a2 = (int)(Math.random() * 9) + 2; // 2..10 b2 = (int)(Math.random() * (a2 - 1)) + 1; // 1..a-1 a3 = (int)(Math.random() * 9) + 2; // 2..10 b3 = (int)(Math.random() * (a3 - 1)) + 1; // 1..a-1 a4 = (int)(Math.random() * 9) + 2; // 2..10 b4 = (int)(Math.random() * (a4 - 1)) + 1; // 1..a-1 } // redraw the lines converging on a given x,y. // The lines are drawn according to the constants a# and b#. // Lines are interpolated from edges of the screen to the x,y // coordinate - getting b/a closer each time. // a# and b# are set elsewhere. public void redrawLines(int x, int y) { Graphics g = imageBuffer.getGraphics(); this.clearImageBuffer(); g.setColor(this.getForeground()); int drawX, drawY, oldX, oldY; oldX = -100; drawX = 0; while (oldX < drawX - 1) { oldX = drawX; drawX += b1 * (x - drawX) / a1; g.drawLine(drawX, 0, drawX, imageBufferSize.height); } drawX = imageBufferSize.height; oldX = drawX + 100; while (oldX > drawX + 1) { oldX = drawX; drawX += b2 * (x - drawX) / a2; g.drawLine(drawX, 0, drawX, imageBufferSize.height); } oldY = -100; drawY = 0; while (oldY < drawY - 1) { oldY = drawY; drawY += b3 * (y - drawY) / a3; g.drawLine(0, drawY, imageBufferSize.width, drawY); } drawY = imageBufferSize.width; oldY = drawY + 100; while (oldY > drawY + 1) { oldY = drawY; drawY += b4 * (y - drawY) / a4; g.drawLine(0, drawY, imageBufferSize.width, drawY); } repaint(); } public boolean mouseDown(Event e, int x, int y) { randomizeConstants(); redrawLines(x, y); return true; } public boolean mouseDrag(Event e, int x, int y) { redrawLines(x, y); return true; } public void init() { this.setBackground(Color.white); this.setForeground(Color.black); randomizeConstants(); super.init(); } }