/* reed kram mas962 problem set 6 disco beta */ import java.applet.*; import java.awt.*; import java.util.Vector; class myPoint extends java.lang.Object { int x; int y; public myPoint(int theX, int theY) { x = theX; y = theY; } } public class discoBeta extends Applet implements Runnable { final double SIN45 = 0.70710678; int gW = 540; int gH = 382; int editOriginX = 230; int editOriginY = 250; int shadowStartX = gW/2; int shadowStartY = gH/2; Thread animatorThread; Button button; Canvas canvas; Image offImage; Graphics offGraphics; int startLetterSizes[] = { 11, 13, 9, 13, 16, 12, 13, 12, 12, 9, 11, 9, 13, 13, 15, 13, 14, 15, 16, 9, 12, 9, 12, 9, 7, 12}; int startLetterPoints[][][] = {{{0,0},{0,-30},{0,-90},{0,-60},{0,-120},{30,-30},{60,-30},{90,-30},{30,-90},{60,-60},{120,0}}, {{0,-120},{0,-90},{0,-60},{0,-30},{0,0},{30,-120},{60,-120},{90,-90},{30,-60},{60,-60},{90,-30}, {60,0},{30,0}}, {{0,-120},{30,-120},{60,-120},{90,-120},{0,-90},{0,-60},{30,-30},{60,0},{90,0}}, {{0,0},{0,-30},{0,-60},{0,-90},{0,-120},{30,-120},{60,-90},{90,-60},{120,-30},{120,0},{90,0}, {60,0},{30,0}}, {{0,-120},{30,-120},{60,-120},{90,-120},{0,-90},{0,-60},{0,-30},{60,0},{30,0},{90,0},{120,0}, {120,-120},{30,-60},{60,-60},{90,-60},{120,-60}}, {{120,-120},{90,-120},{60,-120},{30,-120},{0,-120},{0,-90},{0,-30},{0,0},{30,-60},{60,-60},{90,-60}, {120,-60}}, {{120,-120},{90,-120},{60,-120},{30,-120},{0,-120},{0,-90},{0,-60},{30,-30},{60,0},{120,0},{90,0}, {120,-60},{120,-30}}, {{0,-60},{0,-30},{0,0},{120,-120},{120,-90},{120,-60},{120,-30},{120,0},{30,-90},{60,-90},{90,-90}, {0,-120}}, {{30,-120},{90,-120},{60,-120},{60,-90},{60,-60},{60,-30},{60,0},{30,0},{90,0},{120,0},{120,-120}, {0,0}}, {{120,-120},{120,-90},{120,-60},{120,-30},{120,0},{90,0},{60,0},{30,-30},{0,-60}}, {{0,-120},{0,-90},{0,-60},{0,-30},{0,0},{30,-30},{60,-60},{90,-90},{120,-120},{120,0},{90,-30}}, {{0,-120},{0,-90},{0,-60},{0,-30},{0,0},{30,0},{60,0},{90,0},{120,0}}, {{0,-90},{0,-60},{0,-30},{0,0},{120,0},{120,-30},{120,-90},{120,-60},{120,-120},{90,-90},{60,-60}, {30,-90},{0,-120}}, {{0,0},{0,-30},{0,-60},{0,-90},{0,-120},{30,-90},{60,-60},{90,-30},{120,0},{120,-30},{120,-90}, {120,-60},{120,-120}}, {{0,0},{0,-60},{0,-30},{0,-90},{30,-120},{90,-120},{60,-120},{120,-120},{120,-90},{120,-60},{120,-30}, {120,0},{90,0},{60,0},{30,0}}, {{0,-90},{0,-60},{0,-30},{0,0},{30,-120},{60,-120},{90,-120},{120,-120},{120,-90},{120,-60},{90,-60}, {60,-60},{30,-60}}, {{60,-120},{30,-120},{90,-120},{120,-120},{0,-90},{0,-60},{0,-30},{0,0},{90,-30},{120,0},{60,0}, {120,-60},{120,-90},{30,0}}, {{0,-90},{0,-60},{0,-30},{0,0},{30,-120},{60,-120},{90,-120},{120,-120},{30,-60},{90,-60},{120,-60}, {120,-90},{60,-60},{90,-30},{120,0}}, {{30,-120},{60,-120},{90,-120},{120,-120},{0,-90},{0,-60},{30,-60},{90,-60},{60,-60},{120,-60},{120,-30}, {120,0},{90,0},{30,0},{0,0},{60,0}}, {{0,-120},{30,-120},{90,-120},{120,-120},{60,-120},{30,-90},{30,0},{30,-60},{30,-30}}, {{0,-120},{0,-60},{0,-90},{0,-30},{30,0},{60,0},{90,0},{120,0},{120,-30},{120,-60},{120,-90}, {120,-120}}, {{0,-120},{60,-60},{120,-120},{0,-90},{0,-30},{0,-60},{0,0},{30,-30},{90,-90}}, {{0,-120},{60,-60},{120,0},{120,-30},{120,-90},{120,-120},{120,-60},{60,-90},{60,-120},{0,-90},{30,-60}, {90,-30}}, {{0,-120},{30,-90},{60,-60},{90,-30},{120,0},{0,0},{30,-30},{90,-90},{120,-120}}, {{0,-120},{30,-90},{60,-60},{90,-90},{120,-120},{60,-30},{60,0}}, {{0,-120},{30,-120},{90,-120},{60,-120},{90,-90},{60,-60},{30,-30},{30,0},{90,0},{120,0},{60,0}, {120,-120}}}; Vector alphabet[] = new Vector[26]; boolean editing = true; int editLetter = 0; StringBuffer curEnteredText = new StringBuffer(); Vector charPositions = new Vector(30); int circleDiameter = 50; double circleSpread = 1.0; double shadowAngle = 0.0; int shadowLength = 0; boolean emphasizeSerifs = false; public void init() { super.init(); for(int i=0; i<26; i++) { alphabet[i] = new Vector(100); for (int j=0; j0) System.out.println("{" + point.x + "," + point.y + "},"); else System.out.print("{" + point.x + "," + point.y + "},"); } point = (myPoint)alphabet[i].elementAt(alphabet[i].size()-1); System.out.println("{" + point.x + "," + point.y + "}},"); } } } public boolean handleEvent(Event event) { switch (event.id) { case Event.KEY_PRESS: if (editing) { if (event.key >= 'A' && event.key <= 'Z') { editLetter = event.key - (int)'A'; } else if (event.key >= 'a' && event.key <= 'z') { editLetter = event.key - (int)'a'; } else if (event.key == 0x0008 || event.key == 0x007F) { alphabet[editLetter].removeAllElements(); } } else { if (event.key >= 'A' && event.key <= 'Z') { curEnteredText.append((char)event.key); charPositions.addElement(new particle(2, 0.7, 600, 220)); } else if (event.key >= 'a' && event.key <= 'z') { curEnteredText.append((char)(event.key - 'a' + 'A')); charPositions.addElement(new particle(2, 0.7, 600, 220)); } else if ((event.key == 0x0008 || event.key == 0x007F) && curEnteredText.length() > 0) { curEnteredText.setLength(curEnteredText.length() - 1); charPositions.removeElementAt(charPositions.size() - 1); } else if (event.key == '+') { emphasizeSerifs = !emphasizeSerifs; } } if (event.key == '!') { outputAlphabet(); } return true; //case Event.MOUSE_MOVE: case Event.MOUSE_DOWN: //case Event.MOUSE_UP: if (editing) { if (event.x > editOriginX-15 && event.y < editOriginY+15) { int newX = (event.x - editOriginX + 15) / 30 * 30; int newY = (event.y - editOriginY - 15) / 30 * 30; //alphabet[editLetter].addElement(new myPoint(event.x - editOriginX, event.y - editOriginY)); int searchIndex = 0; boolean found = false; while (searchIndex < alphabet[editLetter].size() && !found) { myPoint point; point = (myPoint)alphabet[editLetter].elementAt(searchIndex); if (point.x == newX && point.y == newY) found = true; else searchIndex++; } if (searchIndex < alphabet[editLetter].size()) alphabet[editLetter].removeElementAt(searchIndex); else alphabet[editLetter].addElement(new myPoint(newX, newY)); } } else { shadowStartX = event.x; shadowStartY = event.y; } return true; case Event.MOUSE_DRAG: if (!editing) { //circleDiameter = (int)(event.x * 100 / (double)gW); //circleSpread = event.y * 3 / (double)gH; if (event.x == shadowStartX) { if (event.y > shadowStartY) { shadowAngle = 90.0 * Math.PI/180.0; } else { shadowAngle = 270.0 * Math.PI/180.0; } } else if (event.x < shadowStartX) { shadowAngle = Math.PI + Math.atan((double)(event.y - shadowStartY)/(double)(event.x - shadowStartX)); } else{ shadowAngle = Math.atan((double)(event.y - shadowStartY)/(double)(event.x - shadowStartX)); } shadowLength = (int)Math.sqrt((event.y - shadowStartY)*(event.y - shadowStartY)+(event.x - shadowStartX)*(event.x - shadowStartX)); } return true; } return super.handleEvent(event); } public void run() { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); Dimension d = canvas.size(); gW = d.width; gH = d.height; //This is the animation loop. while (Thread.currentThread() == animatorThread) { try { Thread.sleep(20); } catch (InterruptedException e) {}; repaint(); } } public void paint(Graphics g) { update(g); } public void drawCircle(Graphics g, int x, int y, int radius) { g.drawArc(x-radius, y-radius, 2*radius, 2*radius, 0, 360); g.drawLine(x, y-radius, x, y+radius); g.drawLine(x-radius, y, x+radius, y); int lineOffset = (int)(SIN45*radius); g.drawLine(x-lineOffset, y-lineOffset, x+lineOffset, y+lineOffset); g.drawLine(x-lineOffset, y+lineOffset, x+lineOffset, y-lineOffset); } public void drawShadowElement(Graphics g, int x, int y, int radius, int length, double angle) { //g.fillArc(x-radius, y-radius, 2*radius, 2*radius, 0, 360); Polygon poly = new Polygon(); poly.addPoint((int)(x + radius*Math.sin(angle)), (int)(y - radius*Math.cos(angle))); poly.addPoint((int)(x + length*Math.cos(angle) + radius*Math.sin(angle)), (int)(y + length*Math.sin(angle) - radius*Math.cos(angle))); poly.addPoint((int)(x + length*Math.cos(angle) - radius*Math.sin(angle)), (int)(y + length*Math.sin(angle) + radius*Math.cos(angle))); poly.addPoint((int)(x - radius*Math.sin(angle)), (int)(y + radius*Math.cos(angle))); poly.addPoint((int)(x + radius*Math.sin(angle)), (int)(y - radius*Math.cos(angle))); g.fillPolygon(poly); g.fillArc((int)(x-radius + length*Math.cos(angle)), (int)(y-radius + length*Math.sin(angle)), 2*radius, 2*radius, 0, 360); } public void update(Graphics g) { if (editing) { offGraphics.setColor(new Color(0, 0, 0)); offGraphics.fillRect(0, 0, gW, gH); offGraphics.setColor(new Color(100, 100, 100)); offGraphics.drawLine(editOriginX-15, editOriginY+15, editOriginX-15 + 30, editOriginY+15); offGraphics.drawLine(editOriginX-15, editOriginY+15-30, editOriginX-15, editOriginY+15); StringBuffer tempBuf = new StringBuffer(); tempBuf.append((char)('A' + editLetter)); offGraphics.drawString(new String(tempBuf), 10, editOriginY + 15); offGraphics.setColor(new Color(255, 255, 255)); for (int i=0; i