// mas964 ps2 // draw triangles on mouse click that demonstrate variety import java.applet.*; import java.awt.*; import java.lang.Math; import DoubleBufferApplet; // this code really only works for 300x300 applet windows. // it should be fixed. public class TriangleVariety extends DoubleBufferApplet { protected int leftMargin, topMargin; protected int numTriangles; protected int triangleSize = 26; // should be even protected double c[][][]; // all these constants are used to randomize the triangle drawing public void randomizeConstants() { int i, j, k; for (i = 0; i < numTriangles; i++) for (j = 0; j < numTriangles - i; j++) // only generate a triangle for (k = 0; k < 6; k++) c[i][j][k] = Math.random(); } // draw a bunch of triangles in a triangular shape // the triangles are randomly varied from their centers depending // on the mouseclick. Further right == more X variation, and futher // down is more Y variation. // randomization is precalculated so state is preserved (for drags) public void redrawTriangles(int mouseX, int mouseY) { Graphics g = imageBuffer.getGraphics(); this.clearImageBuffer(); g.setColor(this.getForeground()); int x, y; for (y = 0; y < numTriangles; y++) for (x = 0; x < numTriangles - y; x++) { int screenX, screenY; screenY = topMargin + y * triangleSize; screenX = leftMargin + y * triangleSize / 2 + x * triangleSize; int a = triangleSize * mouseX / imageBufferSize.width; // variety in x int b = triangleSize * mouseY / imageBufferSize.height; // variety in y fillTriangle(g, screenX + (int)(c[y][x][0] * 2*a - a), screenY + (int)(c[y][x][1] * 2*b - b), screenX + triangleSize + (int)(c[y][x][2] * 2*a - a), screenY + (int)(c[y][x][3] * 2*b - b), screenX + triangleSize / 2 + (int)(c[y][x][4] * 2*a - a), screenY + triangleSize + (int)(c[y][x][5] * 2*b - b)); } repaint(); } // should really be an extension to Graphics, but how do you do that? public void drawTriangle(Graphics g, int x1, int y1, int x2, int y2, int x3, int y3) { int x[] = { x1, x2, x3, x1 }; int y[] = { y1, y2, y3, y1 }; g.drawPolygon(x, y, 4); // should be 3 points?! } public void fillTriangle(Graphics g, int x1, int y1, int x2, int y2, int x3, int y3) { int x[] = { x1, x2, x3 }; int y[] = { y1, y2, y3 }; g.fillPolygon(x, y, 3); } // figure out how many triangles will fit. public void reshape(int x, int y, int width, int height) { int minDimension = Math.min(height, width); numTriangles = (minDimension / triangleSize) - 1; topMargin = (height - numTriangles * triangleSize) / 2; leftMargin = (width - numTriangles * triangleSize) / 2; // System.out.println("Drawing " + numTriangles + " triangles starting at " + topMargin + "," + leftMargin); c = new double[numTriangles][numTriangles][6]; super.reshape(x, y, width, height); } public boolean mouseDown(Event e, int mouseX, int mouseY) { randomizeConstants(); redrawTriangles(mouseX, mouseY); return true; } public boolean mouseDrag(Event e, int mouseX, int mouseY) { redrawTriangles(mouseX, mouseY); return true; } public void init() { this.setBackground(Color.white); this.setForeground(Color.black); super.init(); } }