/** * Faces - draws Chernoff faces * * Based on Faces.c, by Dave Johnson, who got it from Clifford * Pickover's Chernoff face routine, in the book "Computers, * Pattern, Chaos, and Beauty." Inspired by Brad Mohr's Faces * plug-in for AfterDark screen saver. * * 0 head eccentricity * 1 eye eccentricity * 2 pupil size * 3 eyebrow slope * 4 nose size * 5 mouth vert. offset * 6 eye spacing * 7 eye size * 8 mouth width * 9 mouth "openness" */ import java.awt.*; import java.applet.Applet; import Face; public class Faces extends Applet implements Runnable { int[] params = {0,0,0,0,0,0,0,0,0,0}; Point thePt; Face f = new Face(); Thread kicker; Rectangle Chernoff = new Rectangle( 0, 0, 0, 0 ); int paused; int scaleFactor; int speedFactor; int AppletHeight, AppletWidth; int xDim = 0; // x dimension of new rectangle int yDim = 0; // y dimension of new rectangle int wDim = 0; // width of new rectangle int hDim = 0; // height of new rectangle boolean useColor; boolean useRightBrainStyle; boolean useScrolls; // used only in interactive mode Scrollbar s0, s1, s2, s3, s4, s5, s6, s7, s8, s9; Label l0, l1, l2, l3, l4, l5, l6, l7, l8, l9; // TextField t0, t1, t2, t3, t4, t5, t6, t7, t8, t9; Panel scrollPanel, facePanel; public void init() { // set layout setLayout(new BorderLayout(100,100)); // set the background to a nice black setBackground(Color.black); setForeground(Color.white); // get whether or not to use color String color = getParameter("color"); if( (color == null) || (color.equalsIgnoreCase("true")) ) { useColor = true; System.out.println("Using color."); } else { useColor = false; System.out.println("Not using color."); } // should we be using a scrollbar (interactive mode)? String mode = getParameter("mode"); if( (mode == null) || (mode.equalsIgnoreCase("auto")) ) { useScrolls = false; System.out.println("Using automatic mode..."); // get the style String style = getParameter("style"); if( (style == null) || (style.equalsIgnoreCase("right")) ) { useRightBrainStyle = true; System.out.println("Using right brain style."); } else { useRightBrainStyle = false; System.out.println("Using left brain style."); } // get the average size to use String size = getParameter("size"); if( (size == null) || (size.equalsIgnoreCase("random")) ) scaleFactor = 0; else if( size.equalsIgnoreCase("small") ) scaleFactor = 1; else if( size.equalsIgnoreCase("medium") ) scaleFactor = 2; else if( size.equalsIgnoreCase("large") ) scaleFactor = 3; System.out.println("The faces will be " + size); System.out.println("scaleFactor: " + scaleFactor); // get the speed at which to refresh the faces String speed = getParameter("speed"); if( (speed == null) || (speed.equalsIgnoreCase("random"))) speedFactor = 0; else if( speed.equalsIgnoreCase("slow") ) speedFactor = 1; else if( speed.equalsIgnoreCase("medium") ) speedFactor = 2; else if( speed.equalsIgnoreCase("fast") ) speedFactor = 3; System.out.println("The speed will be " + speed); System.out.println("speedFactor: " + speedFactor); } else { useScrolls = true; System.out.println("Using interactive mode..."); // set up our layout manager, panels Canvas faceArea = new Canvas(); scrollPanel = new Panel(); facePanel = new Panel(); scrollPanel.setLayout(new GridLayout(30,8)); facePanel.setLayout(new GridLayout(1,1)); // initialize scrollbars, labels, and textfields s0 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 64, -127, 128); l0 = new Label("Head Eccentricity"); // t0 = new TextField("0",5); s1 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 64, -127, 128); l1 = new Label("Eye Eccentricity"); // t1 = new TextField("0",5); s2 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 64, -127, 128); l2 = new Label("Pupil Size"); // t2 = new TextField("0",5); s3 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 64, -127, 128); l3 = new Label("Eyebrow Slope"); // t3 = new TextField("0",5); s4 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 64, -127, 128); l4 = new Label("Nose Size"); // t4 = new TextField("0",5); s5 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 64, -127, 128); l5 = new Label("Mouth Vertical Offset"); // t5 = new TextField("0",5); s6 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 64, -127, 128); l6 = new Label("Eye Spacing"); // t6 = new TextField("0",5); s7 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 64, -127, 128); l7 = new Label("Eye Size"); // t7 = new TextField("0",5); s8 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 64, -127, 128); l8 = new Label("Mouth Width"); // t8 = new TextField("0",5); s9 = new Scrollbar(Scrollbar.HORIZONTAL, 0, 64, -127, 128); l9 = new Label("Mouth Openness"); // t9 = new TextField("0",5); // add them to our layout scrollPanel.add(l0); scrollPanel.add(s0);// scrollPanel.add(t0); scrollPanel.add(l1); scrollPanel.add(s1);// scrollPanel.add(t1); scrollPanel.add(l2); scrollPanel.add(s2);// scrollPanel.add(t2); scrollPanel.add(l3); scrollPanel.add(s3);// scrollPanel.add(t3); scrollPanel.add(l4); scrollPanel.add(s4);// scrollPanel.add(t4); scrollPanel.add(l5); scrollPanel.add(s5);// scrollPanel.add(t5); scrollPanel.add(l6); scrollPanel.add(s6);// scrollPanel.add(t6); scrollPanel.add(l7); scrollPanel.add(s7);// scrollPanel.add(t7); scrollPanel.add(l8); scrollPanel.add(s8);// scrollPanel.add(t8); scrollPanel.add(l9); scrollPanel.add(s9);// scrollPanel.add(t9); // resize canvas, add to the facePanel faceArea.reshape(1,1, (AppletWidth/2), (AppletHeight/2)); facePanel.add(faceArea); // and add the panels to our applet add("East",scrollPanel); add("South",facePanel); } // how big should we make the applet? String height = getParameter("height"); String width = getParameter("width"); AppletHeight = Integer.parseInt(height); AppletWidth = Integer.parseInt(width); resize( AppletHeight, AppletWidth ); } public void Paint(Graphics g) { update(g); } public void update(Graphics g) { // are we in interactive mode? if( useScrolls == true ) { // set Chernoff rectangle to something good Chernoff.reshape(1,1,(AppletWidth/2),(AppletHeight/2)); } else { // Right Brain Mode? if( useRightBrainStyle == true ) { // Randomly size the rectangle xDim = (int) (Math.random() * AppletWidth); yDim = (int) (Math.random() * AppletHeight); wDim = ((int) (Math.random() * (AppletWidth/4)) + (AppletWidth/8)); hDim = wDim; Chernoff.reshape(xDim, yDim, wDim, hDim); } else { // Left Brain Mode // Pick somewhere to put the next face (8x8 grid) int tmp; // pick from 8, ensure not zero tmp = ((int) (Math.random() * 8)) + 1; // lay out along grid, including first row and column xDim = ((tmp * (AppletWidth / 8)) - (AppletWidth / 8)); // ... and do it again for the y dimension tmp = ((int) (Math.random() * 8)) + 1; yDim = ((tmp * (AppletHeight / 8)) - (AppletHeight / 8)); Chernoff.reshape(xDim, yDim, (AppletHeight/8), (AppletWidth/8)); } } // } else { // Grand Mal Seizure ;) // xDim = (int) (Math.random() * AppletWidth); // yDim = (int) (Math.random() * AppletHeight); // Chernoff.reshape((AppletWidth - xDim), // (AppletHeight - yDim), // (xDim/4), (yDim/4)); // Randomize the parameters if we're not using scrolls if( useScrolls == false ) { int i; for(i=0; i<=9; i++) { params[i] = ((int)(Math.random() * 255) - 127); } } // either use color or not - if using color, randomize it if( useColor == true ) { // this gives us a nice bright set of colors Color c = new Color((128 + (int)(Math.random() * 127)), (128 + (int)(Math.random() * 127)), (128 + (int)(Math.random() * 127))); g.setColor(c); } else { // use white as the default color g.setColor(Color.white); } // draw our face in the rectangle f.DrawFace( Chernoff, params, g ); } public void start() { if(kicker == null) { kicker=new Thread(this); kicker.start(); } } public void stop() { if (kicker != null && kicker.isAlive()) { kicker.stop(); } kicker=null; } public void run() { for(;;) { repaint(); try { Thread.sleep(500 / (speedFactor + 1)); } catch(InterruptedException e) {} } } public boolean handleEvent(Event evt) { if (evt.id == Event.MOUSE_DOWN) { if (kicker.isAlive() && paused==0) { kicker.suspend(); paused=1; System.out.println("suspending Faces..."); } else { if (kicker.isAlive() && paused==1) { kicker.resume(); paused=0; System.out.println("resuming Faces..."); } } } else if (evt.target instanceof Scrollbar) { int value = ((Scrollbar)evt.target).getValue(); Integer Value = new Integer(value); if( evt.target == s0 ) { params[0] = value; System.out.println("The value is: " + value); // t0.setText(Value.toString()); } if( evt.target == s1 ) { params[1] = value; System.out.println("The value is: " + value); // t1.setText(Value.toString()); } if( evt.target == s2 ) { params[2] = value; System.out.println("The value is: " + value); // t2.setText(Value.toString()); } if( evt.target == s3 ) { params[3] = value; System.out.println("The value is: " + value); // t3.setText(Value.toString()); } if( evt.target == s4 ) { params[4] = value; System.out.println("The value is: " + value); // t4.setText(Value.toString()); } if( evt.target == s5 ) { params[5] = value; System.out.println("The value is: " + value); // t5.setText(Value.toString()); } if( evt.target == s6 ) { params[6] = value; System.out.println("The value is: " + value); // t6.setText(Value.toString()); } if( evt.target == s7 ) { params[7] = value; System.out.println("The value is: " + value); // t7.setText(Value.toString()); } if( evt.target == s8 ) { params[8] = value; System.out.println("The value is: " + value); // t8.setText(Value.toString()); } if( evt.target == s9 ) { params[9] = value; System.out.println("The value is: " + value); // t9.setText(Value.toString()); } return true; } return super.handleEvent(evt); } }